【发布时间】: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