Cannot install psycopg2

Hi there! cannot install psycopg2 both manually and automatically

Traceback (most recent call last):
  File "", line 23, in <module>
    import mydb
  File "/home/runner/enraidaiogram/", line 1, in <module>
    import psycopg2
ModuleNotFoundError: No module named 'psycopg2'

Replit Profile:

What command did you run to try to install it, and what was the output of the command?

Through Packages interface

Have you tried doing
pip install psycopg2or
poetry add psycopg2 in the shell?


I would recommend using the poetry command as @QuantumCodes suggested: poetry add psycopg2, then also for good measure and to be safe, try poetry update. I’ve sometimes resolved issues with packages just by running poetry update.

1 Like

This is what I got after using poetry add psycopg2

Updating dependencies
Resolving dependencies... (1.3s)

Package operations: 1 install, 0 updates, 0 removals

  • Installing psycopg2 (2.9.6): Failed


  Command ['/home/runner/enraidaiogram/venv/bin/pip', 'install', '--no-deps', ''] errored with the following return code 1, and output: 
  Looking in indexes:
    Downloading psycopg2-2.9.6.tar.gz (383 kB)
      ERROR: Command errored out with exit status 1:
       command: /home/runner/enraidaiogram/venv/bin/python3 -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-req-build-xgnj5jtn/'"'"'; __file__='"'"'/tmp/pip-req-build-xgnj5jtn/'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code ='"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-2u4_4eh8
           cwd: /tmp/pip-req-build-xgnj5jtn/
      Complete output (25 lines):
      /home/runner/enraidaiogram/venv/lib/python3.10/site-packages/setuptools/config/ SetuptoolsDeprecationWarning: The license_file parameter is deprecated, use license_files instead.
        warnings.warn(msg, warning_class)
      running egg_info
      creating /tmp/pip-pip-egg-info-2u4_4eh8/psycopg2.egg-info
      writing /tmp/pip-pip-egg-info-2u4_4eh8/psycopg2.egg-info/PKG-INFO
      writing dependency_links to /tmp/pip-pip-egg-info-2u4_4eh8/psycopg2.egg-info/dependency_links.txt
      writing top-level names to /tmp/pip-pip-egg-info-2u4_4eh8/psycopg2.egg-info/top_level.txt
      writing manifest file '/tmp/pip-pip-egg-info-2u4_4eh8/psycopg2.egg-info/SOURCES.txt'
      Error: pg_config executable not found.
      pg_config is required to build psycopg2 from source.  Please add the directory
      containing pg_config to the $PATH or specify the full executable path with the
          python build_ext --pg-config /path/to/pg_config build ...
      or with the pg_config option in 'setup.cfg'.
      If you prefer to avoid building psycopg2 from source, please install the PyPI
      'psycopg2-binary' package instead.
      For further information please check the 'doc/src/install.rst' file (also at
  WARNING: Discarding Command errored out with exit status 1: python egg_info Check the logs for full command output.
  ERROR: Command errored out with exit status 1: python egg_info Check the logs for full command output.

  at venv/lib/python3.10/site-packages/poetry/utils/ in _run
      1191│                 output = subprocess.check_output(
      1192│                     cmd, stderr=subprocess.STDOUT, **kwargs
      1193│                 )
      1194│         except CalledProcessError as e:
    → 1195│             raise EnvCommandError(e, input=input_)
      1197│         return decode(output)
      1199│     def execute(self, bin, *args, **kwargs):

Try adding psycopg2-binary instead, or see if there’s a nix pkg for pg_config.

1 Like

Adding psycopg2-binary worked! Thanks a lot!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.