python venv安装模块NotFoundError:没有名为';sqlalchemy';尽管验证了它在虚拟环境中可用
好吧,我有点不知所措。我是Python新手,想用Flask和SQLAlchemy做一个简单的REST应用程序 很明显,我错过了一些步骤,但我看不到,有人能看看我错过了什么吗 到目前为止,我已经完成了以下步骤:python venv安装模块NotFoundError:没有名为';sqlalchemy';尽管验证了它在虚拟环境中可用,python,flask,sqlalchemy,setuptools,python-venv,Python,Flask,Sqlalchemy,Setuptools,Python Venv,好吧,我有点不知所措。我是Python新手,想用Flask和SQLAlchemy做一个简单的REST应用程序 很明显,我错过了一些步骤,但我看不到,有人能看看我错过了什么吗 到目前为止,我已经完成了以下步骤: 为我的项目创建文件夹 使用以下内容在我的项目文件夹中创建setup.py 从设置工具导入设置 setup( name='pyrecipe', version='0.0.1', packages=['pyrecipe'], include_package_data=True
setup(
name='pyrecipe',
version='0.0.1',
packages=['pyrecipe'],
include_package_data=True,
install_requires=[
'click',
'flask',
'sqlalchemy',
'flask-sqlalchemy',
'gunicorn',
'itsdangerous',
'jinja2',
'markupsafe',
'werkzeug',
],
)
python-m venv-venv-venv
创建一个venv,并使用source venv/bin/active
pip安装-e安装软件包。
flask run运行应用程序
我得到以下输出
__import__(module_name)
文件“/home/cvelin/code/python/pyricpe/pyricpe/init.py”,第4行,在
from pyrecipe.database import init_db
文件“/home/cvelin/code/python/pyrecipe/pyrecipe/database.py”,第1行,在
from sqlalchemy import create_engine
ModuleNotFoundError:没有名为'sqlalchemy'的模块
init.py内容
from flask import Flask
app = Flask(__name__)
from pyrecipe.database import init_db
init_db()
import pyrecipe.views
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('postgresql://postgres:postgres@localhost/postgres', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
# import all modules here that might define models so that
# they will be registered properly on the metadata. Otherwise
# you will have to import them first before calling init_db()
import pyrecipe.models
Base.metadata.create_all(bind=engine)
database.py内容
from flask import Flask
app = Flask(__name__)
from pyrecipe.database import init_db
init_db()
import pyrecipe.views
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('postgresql://postgres:postgres@localhost/postgres', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
# import all modules here that might define models so that
# they will be registered properly on the metadata. Otherwise
# you will have to import them first before calling init_db()
import pyrecipe.models
Base.metadata.create_all(bind=engine)
运行pip list
显示所有模块都已安装:
包版本位置
单击6.7
烧瓶1.0.2
《炼金术》第2.3.2节
gunicorn 19.9.0
这是危险的0.24
金甲2.10
MarkupSafe 1.0
pip 18.0
pyrecipe 0.0.1/home/cvelin/code/python/pyrecipe setuptools 39.0.1
SQLAlchemy 1.2.10
Werkzeug 0.14.1 使用python交互式解释器的工作原理与预期一样 >>>从sqlalchemy导入创建引擎 >>>
您是否也使用virtualenv安装了flask?看起来flask是从
/usr/lib/python3.6/site packages/flask/cli.py
加载的,而不是/home/$user/.virtualenvs/$your\u virtual\u env/packages/
,所以它可能无法在虚拟环境中找到包,因为它不在虚拟环境中?您可以尝试使用virtualenv--no site packages target directory
从头开始创建env,以确保pythonYes的系统安装中没有使用包。我使用了venv
模块,而不是使用virtualenv
命令,但是,即使我删除虚拟环境并添加--clear
和--no-site-packages
并使用virtualenv
运行,它仍然会给出相同的结果。此外,使用确认哪个,使用的python二进制文件和pip二进制文件是我虚拟环境中bin文件夹中的文件。实际上,我不想说了,它似乎与flask dev服务器或我的设置有关。使用gunicorn,这个项目实际上运行得很好,我会坚持下去。实际的解决方案是在安装了所需的软件包之后,停用激活脚本,然后再次获取激活脚本的源代码。使用哪个flask
确认它正在尝试使用全局二进制文件,而不是虚拟环境二进制文件。