python venv setup ModuleNotFoundError:没有名为“sqlalchemy”的模块,尽管验证它在虚拟环境中可用

2024-10-05 21:57:05 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我有点不知所措。我对Python相当陌生,想用Flask和SQLAlchemy做一个简单的REST应用程序。在

很明显我漏了一步,但我看不见,有人能看看我缺了什么吗?在

到目前为止,我已经完成了以下步骤:

  1. 为我的项目创建文件夹
  2. 创建设置.py在我的项目文件夹中包含以下内容

    从设置工具导入设置

    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',
      ],
    )
    
  3. python -m venv venv创建一个venv并用source venv/bin/active激活它

  4. 使用pip install -e .安装包

  5. 使用export FLASK_APP=pyrecipe && export FLASK_ENV=dev

  6. 使用flask run运行应用程序 我得到以下输出

Error: While importing "pyrecipe", an ImportError was raised:

Traceback (most recent call last):

File "/usr/lib/python3.6/site-packages/flask/cli.py", line 235, in locate_app

__import__(module_name)

File "/home/cvelin/code/python/pyrecipe/pyrecipe/init.py", line 4, in

from pyrecipe.database import init_db

File "/home/cvelin/code/python/pyrecipe/pyrecipe/database.py", line 1, in

from sqlalchemy import create_engine

ModuleNotFoundError: No module named 'sqlalchemy'

初始化.py内容

^{pr2}$

在数据库.py内容

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显示所有模块都已安装:

Package Version Location


click 6.7
Flask 1.0.2
Flask-SQLAlchemy 2.3.2
gunicorn 19.9.0
itsdangerous 0.24
Jinja2 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和预期一样使用交互式解释器工作

>>> from sqlalchemy import create_engine

>>>


Tags: pipfrompyimportflaskdbvenvsqlalchemy