ImportError:无法从“ flask_session”(未知位置)导入名称“ Session”

时间:2020-05-21 23:16:26

标签: python-3.x session flask sqlalchemy

我有来自CS50w的以下代码,文件为application.py

import os
from flask import Flask, session
from flask_session import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

app = Flask(__name__)

# Check for environment variable
if not os.getenv("DATABASE_URL"):
    raise RuntimeError("DATABASE_URL is not set")

# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)

# Set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))

但是当我按下python3 application.py时出现以下错误

Traceback (most recent call last):
  File "application.py", line 3, in <module>
    from flask_session import Session
ImportError: cannot import name 'Session' from 'flask_session' (unknown location)

我确定我已经安装了所有软件包

~/Documents/cs50_web/project1 » pip3 install Flask-Session                                                                                                              macone@Macs-MacBook-Pro
Requirement already satisfied: Flask-Session in /usr/local/lib/python3.7/site-packages (0.3.2)
Requirement already satisfied: cachelib in /usr/local/lib/python3.7/site-packages (from Flask-Session) (0.1)
Requirement already satisfied: Flask>=0.8 in /usr/local/lib/python3.7/site-packages (from Flask-Session) (1.1.2)
Requirement already satisfied: Werkzeug>=0.15 in /usr/local/lib/python3.7/site-packages (from Flask>=0.8->Flask-Session) (1.0.1)
Requirement already satisfied: click>=5.1 in /usr/local/lib/python3.7/site-packages (from Flask>=0.8->Flask-Session) (7.1.2)
Requirement already satisfied: Jinja2>=2.10.1 in /usr/local/lib/python3.7/site-packages (from Flask>=0.8->Flask-Session) (2.11.2)
Requirement already satisfied: itsdangerous>=0.24 in /usr/local/lib/python3.7/site-packages (from Flask>=0.8->Flask-Session) (0.24)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.7/site-packages (from Jinja2>=2.10.1->Flask>=0.8->Flask-Session) (1.1.1)

sqlalchemy发生了相同的事情,但情况略有不同

Traceback (most recent call last):
  File "import.py", line 2, in <module>
    from sqlalchemy import create_engine
ModuleNotFoundError: No module named 'sqlalchemy'

但它也已安装

~/Documents/cs50_web/project1 » pip3 install sqlalchemy                                                                                          macone@Macs-MacBook-Pro
Requirement already satisfied: sqlalchemy in /usr/local/lib/python3.7/site-packages (1.3.17)

奇怪的是,flask run确实可以工作。

我正在使用vscode和python3.7.4-64bit。我该如何解决这个问题?

这是我的pip3 freeze

~/Documents/cs50_web/project1 » pip3 freeze                                                                                                  1 ↵ macone@Macs-MacBook-Pro
astroid==2.3.3
attrs==19.3.0
autopep8==1.5.2
cachelib==0.1
certifi==2020.4.5.1
cffi==1.14.0
chardet==3.0.4
click==7.1.2
cryptography==2.9.2
cycler==0.10.0
entrypoints==0.3
flake8==3.7.9
Flask==1.1.2
Flask-Session==0.3.2
idna==2.9
isort==4.3.21
itsdangerous==0.24
jellyfish==0.7.2
Jinja2==2.11.2
lazy-object-proxy==1.4.3
lib50==2.0.8
MarkupSafe==1.1.1
matplotlib==3.1.2
mccabe==0.6.1
numpy==1.17.4
pexpect==4.8.0
psycopg2-binary==2.8.5
ptyprocess==0.6.0
pycodestyle==2.5.0
pycparser==2.20
pydocstyle==4.0.1
pyflakes==2.1.1
pylama==7.7.1
pylint==2.4.4
pyparsing==2.4.5
python-dateutil==2.8.1
PyYAML==5.3.1
requests==2.23.0
six==1.13.0
snowballstemmer==2.0.0
SQLAlchemy==1.3.17
submit50==3.0.2
termcolor==1.1.0
typed-ast==1.4.0
urllib3==1.25.9
Werkzeug==1.0.1
wrapt==1.11.2

setting.json

{
  "kite.showWelcomeNotificationOnStartup": false,
  "workbench.activityBar.visible": false,
  "explorer.confirmDelete": false,
  "workbench.colorTheme": "Expressive Theme",
  "workbench.iconTheme": "vscode-icons",
  "css.remoteStyleSheets": [
    "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css"
  ],
  "git.confirmSync": false,
  "python.jediEnabled": false,
  "markdown-preview-enhanced.liveUpdate": true,
  "window.zoomLevel": 1,
  "C_Cpp.updateChannel": "Insiders",
  "files.associations": {
    "*.rmd": "rmd"
    // "*.html": "jinja-html"
  },
  "files.autoSave": "afterDelay",
  "explorer.confirmDragAndDrop": false,
  "editor.formatOnSave": true,
  "liveServer.settings.donotVerifyTags": true,
  "liveServer.settings.donotShowInfoMsg": true
}

谢谢

0 个答案:

没有答案