Linu
Linu

Reputation: 597

Psycopg2 is not importing in MacOS

i tried to run a python script, but it is showing as "Psycopg2 : Module not found error". Tried to install Psycopg2 in different ways, but nothing seems to work.

I'm using MacOS and Python Version 3,Python Version 2.7 was there by default.

Is it related to a path issue or anything? Any guidance will be of very helpful.It's been couple of days i''m struggling with this.

python 3.7.8 (v3.7.8:4b47a5b6ba, Jun 27 2020, 04:47:50) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
========== RESTART: /Users/interview/Desktop/interview/weather test.py =========
Traceback (most recent call last):
  File "/Users/interview/Desktop/work/weather test.py", line 4, in <module>
    import psycopg2
ModuleNotFoundError: No module named 'psycopg2'

Methods tried:

Last login: Wed Jul  1 17:29:19 on ttys000
interview@Admins-iMac ~ % pip install psycopg2-binary

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: psycopg2-binary in ./Library/Python/2.7/lib/python/site-packages (2.8.5)
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: psycopg2-binary in ./Library/Python/2.7/lib/python/site-packages (2.8.5)

interview@Admins-iMac ~ % pip3 install psycopg2

Collecting psycopg2
  Using cached psycopg2-2.8.5.tar.gz (380 kB)
    ERROR: Command errored out with exit status 1:
     command: /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/nf/vclq69l15yn97ktn6y0z9tn80000gp/T/pip-install-jj4idcef/psycopg2/setup.py'"'"'; __file__='"'"'/private/var/folders/nf/vclq69l15yn97ktn6y0z9tn80000gp/T/pip-install-jj4idcef/psycopg2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/nf/vclq69l15yn97ktn6y0z9tn80000gp/T/pip-pip-egg-info-znracm69
         cwd: /private/var/folders/nf/vclq69l15yn97ktn6y0z9tn80000gp/T/pip-install-jj4idcef/psycopg2/
    Complete output (23 lines):
    running egg_info        
    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
    option:
    
        python setup.py 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
    <https://www.psycopg.org/docs/install.html>).
    
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
interview@Admins-iMac ~ % python -m pip install psycopg2    


DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: psycopg2 in ./Library/Python/2.7/lib/python/site-packages (2.8.5)
interview@Admins-iMac ~ % pip install psycopg2==2.7.5                                                                    

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Collecting psycopg2==2.7.5
  Using cached psycopg2-2.7.5-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (1.5 MB)
Installing collected packages: psycopg2
  Attempting uninstall: psycopg2
    Found existing installation: psycopg2 2.8.5
    Uninstalling psycopg2-2.8.5:
      Successfully uninstalled psycopg2-2.8.5
Successfully installed psycopg2-2.7.5
interview@Admins-iMac ~ % python -m pip install psycopg2-binary                                                 

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: psycopg2-binary in ./Library/Python/2.7/lib/python/site-packages (2.8.5)
interview@Admins-iMac ~ % 

Upvotes: 0

Views: 328

Answers (1)

chepner
chepner

Reputation: 531768

pip3 is the correct command, but the error message is telling you that you are missing a program (pg_config) that is required to build psycopg2 from source.

% pip3 install psycopg2

Collecting psycopg2
    [...]
    
    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
    option:
    
        python setup.py 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.
    
    [...] 

The error message also tells you what to do if you don't want to build from source: install psycopg2-binary instead.

$ pip3 install psycopg2-binary

Upvotes: 4

Related Questions