gpt4 book ai didi

python - sqlalchemy create_engine 函数在运行文件名 testalchemy.py 时不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:29 25 4
gpt4 key购买 nike

sqlalchemy create_engine 函数在从文件运行时不起作用,但它在 python shell 中运行良好,因此找不到问题。所以请任何人指出这个问题。我是用户 window 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.py 的文件。关注documentation ,在导入模块时,python 在尝试 $PYTHONPATH 中的目录之前先在 CWD 中查找。我猜你在 C:/Users/Lenovo/Desktop/Auto/pythonseltest/Tests/sqlalchemy.py 中没有 create_engine

关于python - sqlalchemy create_engine 函数在运行文件名 testalchemy.py 时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42389724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com