好吧,我有点不知所措。我对Python相当陌生,想用Flask和SQLAlchemy做一个简单的REST应用程序。在
很明显我漏了一步,但我看不见,有人能看看我缺了什么吗?在
到目前为止,我已经完成了以下步骤:
创建设置.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并用source venv/bin/active
激活它
使用pip install -e .
安装包
使用export FLASK_APP=pyrecipe && export FLASK_ENV=dev
使用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
>>>
目前没有回答
相关问题 更多 >
编程相关推荐