Describe the bug
create_engine throws an error
Expected behavior
expected create_engine to work
To Reproduce
import os
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
db_name = 'stss_development'
user_pass = os.environ['PG_STSS_DB_USER'] +':' + os.environ['PG_STSS_DBX']
engine_string = 'postgresql+psycopg2://' + user_pass + '@localhost/' + db_name
engine = create_engine(engine_string)
Error
Traceback (most recent call last):
File "random.py", line 3, in <module>
from sqlalchemy import create_engine
File "/usr/lib/python3/dist-packages/sqlalchemy/__init__.py", line 146, in <module>
__go(locals())
File "/usr/lib/python3/dist-packages/sqlalchemy/__init__.py", line 143, in __go
_sa_util.dependencies.resolve_all("sqlalchemy")
File "/usr/lib/python3/dist-packages/sqlalchemy/util/langhelpers.py", line 1024, in resolve_all
m._resolve()
File "/usr/lib/python3/dist-packages/sqlalchemy/util/langhelpers.py", line 1064, in _resolve
self._initial_import = compat.import_(
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/default.py", line 17, in <module>
import random
File "/opt/Markets/programming/python/random.py", line 12, in <module>
engine = create_engine(engine_string)
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/__init__.py", line 479, in create_engine
return strategy.create(*args, **kwargs)
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/strategies.py", line 61, in create
entrypoint = u._get_entrypoint()
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/url.py", line 172, in _get_entrypoint
cls = registry.load(name)
File "/usr/lib/python3/dist-packages/sqlalchemy/util/langhelpers.py", line 225, in load
loader = self.auto_fn(name)
File "/usr/lib/python3/dist-packages/sqlalchemy/dialects/__init__.py", line 45, in _auto_fn
module = __import__("sqlalchemy.dialects.%s" % (dialect,)).dialects
File "/usr/lib/python3/dist-packages/sqlalchemy/dialects/postgresql/__init__.py", line 8, in <module>
from . import base
File "/usr/lib/python3/dist-packages/sqlalchemy/dialects/postgresql/base.py", line 2317, in <module>
class PGExecutionContext(default.DefaultExecutionContext):
AttributeError: partially initialized module 'sqlalchemy.engine.default' has no attribute 'DefaultExecutionContext' (most likely due to a circular import)
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 72, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 12, in <module>
import subprocess, tempfile, os.path, re, pwd, grp, os, time, io
File "/usr/lib/python3.8/tempfile.py", line 184, in <module>
from random import Random as _Random
File "/opt/Markets/programming/python/random.py", line 12, in <module>
engine = create_engine(engine_string)
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/__init__.py", line 479, in create_engine
return strategy.create(*args, **kwargs)
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/strategies.py", line 61, in create
entrypoint = u._get_entrypoint()
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/url.py", line 172, in _get_entrypoint
cls = registry.load(name)
File "/usr/lib/python3/dist-packages/sqlalchemy/util/langhelpers.py", line 225, in load
loader = self.auto_fn(name)
File "/usr/lib/python3/dist-packages/sqlalchemy/dialects/__init__.py", line 45, in _auto_fn
module = __import__("sqlalchemy.dialects.%s" % (dialect,)).dialects
AttributeError: module 'sqlalchemy' has no attribute 'dialects'
Original exception was:
Traceback (most recent call last):
File "random.py", line 3, in <module>
from sqlalchemy import create_engine
File "/usr/lib/python3/dist-packages/sqlalchemy/__init__.py", line 146, in <module>
__go(locals())
File "/usr/lib/python3/dist-packages/sqlalchemy/__init__.py", line 143, in __go
_sa_util.dependencies.resolve_all("sqlalchemy")
File "/usr/lib/python3/dist-packages/sqlalchemy/util/langhelpers.py", line 1024, in resolve_all
m._resolve()
File "/usr/lib/python3/dist-packages/sqlalchemy/util/langhelpers.py", line 1064, in _resolve
self._initial_import = compat.import_(
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/default.py", line 17, in <module>
import random
File "/opt/Markets/programming/python/random.py", line 12, in <module>
engine = create_engine(engine_string)
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/__init__.py", line 479, in create_engine
return strategy.create(*args, **kwargs)
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/strategies.py", line 61, in create
entrypoint = u._get_entrypoint()
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/url.py", line 172, in _get_entrypoint
cls = registry.load(name)
File "/usr/lib/python3/dist-packages/sqlalchemy/util/langhelpers.py", line 225, in load
loader = self.auto_fn(name)
File "/usr/lib/python3/dist-packages/sqlalchemy/dialects/__init__.py", line 45, in _auto_fn
module = __import__("sqlalchemy.dialects.%s" % (dialect,)).dialects
File "/usr/lib/python3/dist-packages/sqlalchemy/dialects/postgresql/__init__.py", line 8, in <module>
from . import base
File "/usr/lib/python3/dist-packages/sqlalchemy/dialects/postgresql/base.py", line 2317, in <module>
class PGExecutionContext(default.DefaultExecutionContext):
AttributeError: partially initialized module 'sqlalchemy.engine.default' has no attribute 'DefaultExecutionContext' (most likely due to a circular import)
Versions.
Additional context
Code worked 2020-07-17
Code failed 2020-07-18
Have a nice day!
It seems that the you are using an ubuntu version of sqlalchemy.
Could you try installing sqlalchemy from pypi to see if it solves the problem?
(Also being discussed on Stack Overflow here.)
On checking the Ubuntu repositories I found that 20.04 (focal) seems to be distributing SQLAlchemy 1.3.12 whereas 18.04 (bionic) distributes 1.3.11. So, I tried to reproduce your issue under 20.04 but I couldn't. This test worked fine for me:
gord@vbox-Xubu-20-04-a:~$ python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlalchemy as sa
>>> sa.__version__
'1.3.12'
>>> import psycopg2
>>> psycopg2.__version__
'2.8.4 (dt dec pq3 ext lo64)'
>>> engine = sa.create_engine("postgresql+psycopg2://scott:[email protected]/mydb")
>>> with engine.begin() as conn:
... conn.execute(sa.text("SELECT 1 AS foo")).fetchall()
...
[(1,)]
hey there -
the problem is that the application in use contains a module called "random" that is not within any package. We can see this here:
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/default.py", line 17, in <module>
import random
File "/opt/Markets/programming/python/random.py", line 12, in <module>
engine = create_engine(engine_string)
File "/usr/lib/python3/dist-packages/sqlalchemy/engine/__init__.py", line 479, in create_engine
return strategy.create(*args, **kwargs)
SQLAlchemy uses the Python built-in random library which it uses to generate tokens for two phase transactions. but then there is a file "random.py" that is not within any kind of package and is right on the PYTHONPATH:
/opt/Markets/programming/python/random.py
this then tries to also import SQLAlchemy and you get an import error.
Solution:
ensure "random.py" is part of a proper Python package with its own name and its own "__init__.py" file. it should not be importable as 'import random" but instead as "from myapplication import random"
see https://docs.python.org/3/tutorial/modules.html#packages for background.
zzzeek - Thanks. That fixed that error. Now dealing with something completely different.
Most helpful comment
hey there -
the problem is that the application in use contains a module called "random" that is not within any package. We can see this here:
SQLAlchemy uses the Python built-in random library which it uses to generate tokens for two phase transactions. but then there is a file "random.py" that is not within any kind of package and is right on the PYTHONPATH:
this then tries to also import SQLAlchemy and you get an import error.
Solution:
ensure "random.py" is part of a proper Python package with its own name and its own "
__init__.py" file. it should not be importable as 'import random" but instead as "from myapplication import random"see https://docs.python.org/3/tutorial/modules.html#packages for background.