Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlalchemy create_engine function not working while running file name testalchemy.py

sqlalchemy create_engine function not working while running from file but it work fine in python shell so cannot found the issue. So please anybody point out the issue. I am user window 10 and python version 3.5.3.

Here is the error while running script file:

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

here is the actual code

from sqlalchemy import create_engine

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

this code work fine if i type this code line by line in python shell but while try to run the file it shows above error.

like image 784
user7604331 Avatar asked Dec 10 '22 12:12

user7604331


1 Answers

This is because your have a file on your working directory named sqlalchemy.py. Following the documentation, when importing a module, python looks in CWD before trying the directories from the $PYTHONPATH. And I guess your have no create_engine in C:/Users/Lenovo/Desktop/Auto/pythonseltest/Tests/sqlalchemy.py.

like image 81
Nicolas Garnier Avatar answered Dec 13 '22 21:12

Nicolas Garnier