Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python venv安装模块NotFoundError:没有名为';sqlalchemy';尽管验证了它在虚拟环境中可用_Python_Flask_Sqlalchemy_Setuptools_Python Venv - Fatal编程技术网

python venv安装模块NotFoundError:没有名为';sqlalchemy';尽管验证了它在虚拟环境中可用

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

好吧,我有点不知所措。我是Python新手,想用Flask和SQLAlchemy做一个简单的REST应用程序

很明显,我错过了一些步骤,但我看不到,有人能看看我错过了什么吗

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

  • 为我的项目创建文件夹

  • 使用以下内容在我的项目文件夹中创建setup.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',
      ],
    )
    
  • 使用
    python-m venv-venv-venv
    创建一个venv,并使用
    source venv/bin/active

  • 使用
    pip安装-e安装软件包。

  • 使用导出烧瓶\应用程序=pyrecipe和导出烧瓶\环境=dev设置烧瓶环境

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

  • 错误:导入“pyrecipe”时,引发了一个导入错误:

    回溯(最近一次呼叫最后一次):

    文件“/usr/lib/python3.6/site packages/flask/cli.py”,第235行,在locate_应用程序中

    __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
    确认它正在尝试使用全局二进制文件,而不是虚拟环境二进制文件。