Home > front end >  No module named 'psycopg2' error even after successful installing psycopg2
No module named 'psycopg2' error even after successful installing psycopg2

Time:05-31

I followed the instructions from here when researching on how to install psycopg2 on my new Macbook pro with M1 chip.

brew install libpq --build-from-source
brew install openssl

export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib -L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include -I/opt/homebrew/opt/libpq/include"

pip3 install psycopg2

installation was successful, and this was the output when i tried to run the last line again

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: psycopg2 in /Users/yvonnetan/Library/Python/3.8/lib/python/site-packages (2.9.3)

hence it seems that the installation is successful, but when i run my codes, the same error appears:

code:

DATABASE_URI = f"postgresql://{credentials.user}:{credentials.pwd}@{credentials.host}:{credentials.port}/{credentials.db}"
engine = create_engine(DATABASE_URI)

error:

ModuleNotFoundError                       Traceback (most recent call last)
/Users/yvonnetan/Documents/people-data-science/product-metrics/notebooks/product_metrics_automation.ipynb Cell 3' in <cell line: 10>()
      7 import credentials
      9 DATABASE_URI = f"postgresql://{credentials.user}:{credentials.pwd}@{credentials.host}:{credentials.port}/{credentials.db}"
---> 10 engine = create_engine(DATABASE_URI)

File <string>:2, in create_engine(url, **kwargs)

File ~/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/util/deprecations.py:309, in deprecated_params.<locals>.decorate.<locals>.warned(fn, *args, **kwargs)
    302     if m in kwargs:
    303         _warn_with_version(
    304             messages[m],
    305             versions[m],
    306             version_warnings[m],
    307             stacklevel=3,
    308         )
--> 309 return fn(*args, **kwargs)

File ~/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/engine/create.py:560, in create_engine(url, **kwargs)
    558         if k in kwargs:
    559             dbapi_args[k] = pop_kwarg(k)
--> 560     dbapi = dialect_cls.dbapi(**dbapi_args)
    562 dialect_args["dbapi"] = dbapi
    564 dialect_args.setdefault("compiler_linting", compiler.NO_LINTING)

File ~/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py:782, in PGDialect_psycopg2.dbapi(cls)
    780 @classmethod
    781 def dbapi(cls):
--> 782     import psycopg2
    784     return psycopg2

ModuleNotFoundError: No module named 'psycopg2'
engine = create_engine(DATABASE_URI)

Can anyone help me please?

CodePudding user response:

You can try restart your machine, for me this does the trick.

If not you can try:

(step 1 is only if you are using the app)

  1. run this command after installing postgres app and initialize the server

    sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp

  2. rebuild your env

CodePudding user response:

I think you are mixing between between two different python installations.

You install the psycopg2 at

/Users/yvonnetan/Library/Python/3.8/lib/python/site-packages (2.9.3)
                         ^^^^^^^^^^

but run the code inside the conda environment

~/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py:782
     ^^^^^^^^^^^^^^^^^^^^^^^^

Pick one or the other, to see which python you are running run which python or which python3. To install package with conda run: conda install psycopg2. But since the installation on python3.8 is successful, I suggest you stick with that one.

Read more:

  • Related