【问题标题】:Couldn't run the code that contains sqlalchemy无法运行包含 sqlalchemy 的代码
【发布时间】:2020-06-11 01:58:22
【问题描述】:

我正在使用 Postgresql,我试图在 cmd (py list.py) 终端中运行以下代码,但我不能,它显示:

错误信息:

  File "list.py", line 3, in <module>
    from sqlalchemy import create_engine
ModuleNotFoundError: No module named 'sqlalchemy'

我需要帮助,因为我试图在虚拟环境目录中运行它(pip install sqlalchemy),然后仍然显示相同的错误消息!

代码:

import os

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))

def main():
    flights = db.execute("SELECT origin, destination, duration FROM flights").fetchall()
    for flight in flights:
        print(f"{flight.origin} to {flight.destination}, {flight.duration} minutes.")

if __name__ == "__main__":
    main()

【问题讨论】:

  • 您能否通过 pip3 freeze 命令检查是否安装了 sqlalchemy。并且请确保在运行文件时激活虚拟环境,如果您使用的是虚拟环境。如果您使用的是 python3,安装使用 pip3 install SQLAlchemy
  • 嗨,我已经安装了它,但仍然显示相同。

标签: python sql postgresql flask sqlalchemy


【解决方案1】:

终于!!,我在这个有用的页面中找到了答案:https://cs50.stackexchange.com/questions/30610/cs50w-list-py-attributeerror-nonetype-object-has-no-attribute-instantia,之后你需要输入这个命令:

pip3 install Psycopg2 

您将成功连接到数据库。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多