2017-02-22 17 views
0

sqlalchemy create_engine函數在從文件運行時不工作,但它在python shell中正常工作,因此找不到問題。所以請有人指出這個問題。我是用戶窗口10和python版本3.5.3。sqlalchemy create_engine函數在運行文件名時不工作testalchemy.py

以下是錯誤在運行腳本文件:

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:[email protected]:3306/databasename') 
connection = engine.connect() result = connection.execute('select * 
from cuser') 

此代碼的工作很好,如果我通過線在Python Shell鍵入此行代碼,但同時嘗試運行該文件它顯示以上錯誤。

回答

5

這是因爲您的工作目錄中有一個名爲sqlalchemy.py的文件。在documentation之後,當導入一個模塊時,python在從$ PYTHONPATH嘗試目錄之前查找CWD。我猜你沒有create_engineC:/Users/Lenovo/Desktop/Auto/pythonseltest/Tests/sqlalchemy.py

+0

感謝它的工作。 – user7604331

+0

Thx。我遇到了完全相同的問題。我的文件與播放具有相同的名稱。 Stoopid我。 – Semo

相關問題