SQLAlchemy import errors in Pydev/Eclipse - python

I tried installing SQLAlchemy for Python 3.2 which I use with Eclipse/Pydev. A simple test script fails
from sqlalchemy.engine import create_engine
engine=create_engine("mysql://user:password#server/database")
If I run it from Eclipse I get
Traceback (most recent call last):
File "...\sqlalchemy.py", line 1, in <module>
from sqlalchemy.engine import create_engine
File "...\sqlalchemy.py", line 1, in <module>
from sqlalchemy.engine import create_engine
ImportError: No module named engine
However I actually generated the import line with Ctrl-Shirt-O, so Eclipse found that automatically and knows about it. Also Pydev does not show any errors in the script.
If I try the same script in the interactive Pydev console I get
from sqlalchemy.engine import create_engine
engine=create_engine("mysql://user:password#server/database")
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python32\lib\site-packages\sqlalchemy-0.7.8-py3.2.egg\sqlalchemy\engine \__init__.py", line 338, in create_engine
return strategy.create(*args, **kwargs)
File "C:\Python32\lib\site-packages\sqlalchemy-0.7.8-py3.2.egg\sqlalchemy\engine\strategies.py", line 64, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "C:\Python32\lib\site-packages\sqlalchemy-0.7.8-py3.2.egg\sqlalchemy\connectors\mysqldb.py", line 52, in dbapi
return __import__('MySQLdb')
ImportError: No module named MySQLdb
Do you have an idea how to get it work?

Answer is simple: your main module is named sqlalchemy.py. This is a trap that was much more easier to fall in python 2 - naming your own module by the same name as a system module.
At startup your sqlalchemy.py is loaded by python as the __main__ module, and when the first line runs, python reloads sqlalchemy.py as the module sqlalchemy; the second time the import line is run the python interpreter already finds sqlalchemy in sys.modules, but it does not contain the variable or module named engine.
For easy fix rename sqlalchemy.py to for example satest.py. For more complete solution, organize your code in packages.

While the first error was an unfortunate mistake as explained by Antti I finally also solved the other issue. I didn't have MySQLdb installed which again requires a MySQL Server. Instead I have mysql-connector for which the correct syntax is
engine=create_engine("mysql+mysqlconnector://...")
I didn't see this while looking for quick test examples.

Related

How do I fix "ModuleNotFoundError: No module named 'sqlalchemy'" when using flask_sqlalchemy?

I am trying to use flask_sqlalchemy, but when I run my script it throws up the following error:
Traceback (most recent call last):
File "C:\files\main.py", line 2, in <module>
from flask_sqlalchemy import SQLAlchemy
File "C:\files\Python\Python310\site-packages\flask_sqlalchemy\__init__.py", line 13, in <module>
import sqlalchemy
ModuleNotFoundError: No module named 'sqlalchemy'
I have both flask_sqlalchemy and sqlalchemy installed and am having trouble figuring out why it is saying this. Also, PyCharm recognises both libraries as being installed and offers its usual predictive features when typing stuff to do with them.
Any help would be much appreciated

Errors importing pyodbc

My background is Java/C++ and I'm very new to python. I'm trying to import pyodbc to my project which works alright if I do it via command line.
import odbc
However, when I try to do the same in pydev/eclipse, i get the following error which I cannot find a solution to. I suspect this may have something to do with Eclipse setup
Traceback (most recent call last):
File "C:\Users\a\workspace\TestPyProject\src\helloworld.py", line 2, in <module>
import pyodbc
File "C:\Users\a\AppData\Local\Continuum\Anaconda3\Lib\site-packages\sqlalchemy\dialects\mssql\pyodbc.py", line 105, in <module>
from .base import MSExecutionContext, MSDialect, VARBINARY
ImportError: attempted relative import with no known parent package
I'm really stuck here and any hints will be appreciated!
An incorrect library was imported into the External Libraries list of the project. After fixing it, the import is working.

error related to sqlalchemy in python

I am trying to compile a model writte in fortran called pywofost. I followed the steps of compiling the model. however, I receive the following error related to sqlalchemy.exceptions. sqlalchemy version is '1.0.12'. the error message is as follows:
command
import pywofost
error
/usr/lib/python2.7/dist-packages/pkg_resources.py:1031: UserWarning: /home/omar/.python-eggs is writable by group/others and vulnerable to attack when used with get_resource_filename. Consider a more secure location (set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).
warnings.warn(msg, UserWarning)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/omar/pywofost/pywofost.py", line 16, in <module>
from sqlalchemy.exceptions import *
ImportError: No module named exceptions
That's because there is no module in sqlalchemy named exceptions as of version 1.0.12. If you were depending on a third party module it looks like they have a bug in their code
There is sqlalchemy.exc or sqlalchemy.orm.exc if you want to import exceptions from there though. Maybe you can just modify the pywofost code quickly and continue your work?
the problem was solved by using older version of sqlalchemy

SQLAlchemy import error : import _mysql

I am new at Python and SQLAlchemy and I was trying to play with them a little bit, but whenever I run a test it gives me the following error :
Traceback (most recent call last):
File "/home/zakaria/workspace-python/Jerreb/essai/tejriba.py", line 11, in <module>
engine = create_engine("mysql://root:root#localhost/python")
File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.9.7-py2.7-linux-i686.egg/sqlalchemy/engine/__init__.py", line 346, in create_engine
return strategy.create(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.9.7-py2.7-linux-i686.egg/sqlalchemy/engine/strategies.py", line 74, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.9.7-py2.7-linux-i686.egg/sqlalchemy/connectors/mysqldb.py", line 64, in dbapi
return __import__('MySQLdb')
File "/usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.5-py2.7-linux-i686.egg/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: /usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.5-py2.7-linux-i686.egg/_mysql.so: undefined symbol: _Py_ZeroStruct
I am working with PyDev.
This is the project architecture:
And these are the libs I have imported :
What is the problem and how can I fix it?
Thanks!
When I got this message, it turned out that, unbeknownst to me, the code was being run in the Python 3 interpreter, instead of Python 2. (This happened because I had installed Ubuntu's libapache2-mod-wsgi-py3 for a different project.) MySQL-python does not yet support Python 3. :'(
I don't know whether WSGI is involved for you, but try making sure you're not running Python 3.

Python 3 mySQL.connector frozen import lib

I am new to Python 3 and still learning but I need help. The first part of the script is:
import mysql.connector #this is failing as a .py but works in the shell
cnx = mysql.connector.connect(user='root', password='mypassword', host='my_ip_address', database'name_of_database') #this works in the shell
cursor = cnx.cursor()
I have tried the above line by line in the Python shell which works fine: I can import the connector to connect to my database and fetch back data. But when I save the script as a .py the import mysql connector does not work. I do have the Path variable setup and when I installed the mySQL connector it placed the relevant files in my Python install path \Lib\site-pacakges folder.
I get the following error:
>Traceback (most recent call last):
> File "<frozen importlib._bootstrap>", line 1518, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
>Traceback (most recent call last):
> File "C:\Users\Paul\Desktop\scripts\mysql.py", line 2, in <module>
> import mysql.connector
> File "C:\Users\Paul\Desktop\scripts\mysql.py", line 2, in <module>
> import mysql.connector
>ImportError: No module named 'mysql.connector'; mysql is not a package
I have the same question as your, and I found an answer in another website where you asked the same question. I'm pasting the answer here as reference.
The above happens because your script is named 'mysql.py', which is
actually the same name as the 'mysql' package. Python correctly
reports ImportError. Simply rename your 'mysql.py' to something else,
like 'mysqlhacks.py'.
Taken from https://answers.launchpad.net/myconnpy/+question/226992

Categories