【问题标题】:sqlalchemy create_engine function not working while running file name testalchemy.pysqlalchemy create_engine 函数在运行文件名 testalchemy.py 时不起作用
【发布时间】:2017-02-22 11:16:02
【问题描述】:

sqlalchemy create_engine 函数在从文件运行时不起作用,但它在 python shell 中工作正常,因此找不到问题。所以请大家指出这个问题。我是用户窗口 10 和 python 版本 3.5.3。

这是运行脚本文件时的错误:

C:\python\python.exe C:/Users/Lenovo/Desktop/Auto/pythonseltest/Tests/sqlalchemy.py Traceback (most recent call last):   File "C:/Users/Lenovo/Desktop/Auto/pythonseltest/Tests/sqlalchemy.py", line 2, in <module>
    from sqlalchemy import create_engine   File "C:\Users\Lenovo\Desktop\Auto\pythonseltest\Tests\sqlalchemy.py", line 2, in <module>
    from sqlalchemy import create_engine ImportError: cannot import name 'create_engine'

Process finished with exit code 1

这是实际代码

from sqlalchemy import create_engine

engine =create_engine('mysql+mysqldb://username:password@localhost:3306/databasename')
connection = engine.connect() result = connection.execute('select *
from cuser')

如果我在 python shell 中逐行键入此代码,但在尝试运行文件时它会显示上述错误,则此代码工作正常。

【问题讨论】:

  • 看起来某些版本的 sqlalchemy 需要 from sqlalchemy.orm import create_engine

标签: python sqlalchemy


【解决方案1】:

这是因为您的工作目录中有一个名为 sqlalchemy.py 的文件。在documentation 之后,在导入模块时,python 在尝试来自 $PYTHONPATH 的目录之前先查看 CWD。我猜你在C:/Users/Lenovo/Desktop/Auto/pythonseltest/Tests/sqlalchemy.py 中没有create_engine

【讨论】:

  • 谢谢。我遇到了完全相同的问题。我的要播放的文件具有相同的名称。让我变笨了。
  • 谢谢。我犯了同样的错误。始终尽量避免使用正在使用的模块名称作为文件名。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-06
  • 1970-01-01
  • 1970-01-01
  • 2021-03-11
  • 2013-04-08
  • 1970-01-01
相关资源
最近更新 更多