How to Solve Error Message ERROR: Failed building wheel for psycopg2 when installing psycopg2 using pip command

Posted on

Introduction

This article is focusing on how to solve a specific problem. The problem itself is the occurrence of a certain error message. Furthermore, the appearance of that error message actually stop an important process. That important process is the process for installing a library of python with the name of ‘psycopg2’. Actually, it is an important python library where the main function exist as in the info for that library itself. Below is the information of the python library with the name of ‘psycopg2’ :

(venv) C:\repository\django\venv\Scripts>pip_search psycopg2
🐍 https://pypi.org/search/?q=psycopg2 🐍
┌───────────────────────────────────────────┬─────────────┬────────────┬──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Package                                   │ Version     │ Released   │ Description                                                                                      │
├───────────────────────────────────────────┼─────────────┼────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────┤

│ 📂 psycopg2                                2.9.3 ==      29-12-2021   psycopg2 - Python-PostgreSQL Database Adapter

So, it is a python library for PostgreSQL database adapter for python. This article has the connection with the previous article in this link with the title of ‘How to Solve Error Message Unable to create process using ‘python.exe -V’’. The python virtual environment exist in this article is exactly the same with available in that article. So, the installation process of the ‘psycopg2’ python library exist is using the same one in that article. So, the following is the execution of the psycopg2 python library installation which is triggering the error message :

(env) C:\repository\django\env\bin>pip install psycopg2
Collecting psycopg2
Using cached psycopg2-2.9.3.tar.gz (380 kB)
Preparing metadata (setup.py) ... done
Building wheels for collected packages: psycopg2
Building wheel for psycopg2 (setup.py) ... error
error: subprocess-exited-with-error

× python setup.py bdist_wheel did not run successfully.
│ exit code: 1
╰─> [22 lines of output]
C:\repository\django\env\lib\python3.9\site-packages\setuptools\config\setupcfg.py:463: SetuptoolsDeprecationWarning: The license_file parameter is deprecated, use license_files instead.
warnings.warn(msg, warning_class)
running bdist_wheel
running build
running build_py
creating build
creating build\lib.mingw_x86_64-cpython-39
creating build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\errorcodes.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\errors.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\extensions.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\extras.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\pool.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\sql.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\tz.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\_ipaddress.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\_json.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\_range.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\__init__.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
running build_ext
building 'psycopg2._psycopg' extension
error: --plat-name must be one of ('win32', 'win-amd64', 'win-arm32', 'win-arm64')
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for psycopg2
Running setup.py clean for psycopg2
Failed to build psycopg2
Installing collected packages: psycopg2
Running setup.py install for psycopg2 ... error
error: subprocess-exited-with-error

× Running setup.py install for psycopg2 did not run successfully.
│ exit code: 1
╰─> [24 lines of output]
C:\repository\django\env\lib\python3.9\site-packages\setuptools\config\setupcfg.py:463: SetuptoolsDeprecationWarning: The license_file parameter is deprecated, use license_files instead.
warnings.warn(msg, warning_class)
running install
C:\repository\django\env\lib\python3.9\site-packages\setuptools\command\install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
warnings.warn(
running build
running build_py
creating build
creating build\lib.mingw_x86_64-cpython-39
creating build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\errorcodes.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\errors.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\extensions.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\extras.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\pool.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\sql.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\tz.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\_ipaddress.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\_json.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\_range.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
copying lib\__init__.py -> build\lib.mingw_x86_64-cpython-39\psycopg2
running build_ext
building 'psycopg2._psycopg' extension
error: --plat-name must be one of ('win32', 'win-amd64', 'win-arm32', 'win-arm64')
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> psycopg2

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.

(env) C:\repository\django\env\bin>

How to Solve Error Message ERROR: Failed building wheel for psycopg2

Actually, the solution is very simple. Just switch the version of the python. More important, the base python which is become the main base python in the python virtual environment. In the above case, the command for creating the python virtual environment is using the ‘virtualenv’ command. The python version which exist in the python virtual environment is ‘3.9’. The process for creating the ‘virtualenv’ exist as follow :

 
C:\repository\django>virtualenv env 
created virtual environment CPython3.9.10.final.0-64 in 3264ms 
  creator CPython3Windows(dest=C:\repository\django\env, clear=False, no_vcs_ignore=False, global=False) 
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\Personal User\AppData\Local\pypa\virtualenv) 
    added seed packages: pip==22.1.2, setuptools==62.5.0, wheel==0.37.1 
  activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator 
C:\repository\django>

Soon after, just go to the python virtual environment folder with the name of ‘env’. Just enter one directory in it which is the ‘bin’ folder and type ‘activate’ to activate the python virtual environment as follows :

C:\repository\django\env\bin>activate 

(env) C:\repository\django\env\bin>

Next, check the version of the python as it exist below :

(env) C:\repository\django\env\bin>python -V 
Python 3.9.10 

(env) C:\repository\django\env\bin>

So, the solution is just changing the version of the python’s version. In order to do that, just execute the following steps :

  1. First of all, just create the python virtual environment. In this case, in order to use another python’s version, just use another command to create the python virtual environment. The command execution exist as follows :

    <pre>C:\repository\django&gt;python -m venv venv
    
    C:\repository\django&gt;cd venv
    
    (venv) C:\repository\django\venv&gt;dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\repository\django\venv
    
    07/17/2022 03:08 PM &lt;DIR&gt; .
    07/17/2022 03:08 PM &lt;DIR&gt; ..
    07/17/2022 03:08 PM &lt;DIR&gt; Include
    07/17/2022 03:08 PM &lt;DIR&gt; Lib
    07/17/2022 03:08 PM 84 pyvenv.cfg
    07/17/2022 03:08 PM &lt;DIR&gt; Scripts
    1 File(s) 84 bytes
    5 Dir(s) 147,161,825,280 bytes free
    
    C:\repository\django\venv&gt;
    
  2. Next, just access the ‘Scripts’ folder as follows :

    C:\repository\django\venv&gt;cd Scripts
    
    C:\repository\django\venv\Scripts&gt;dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\repository\django\venv\Scripts
    
    07/17/2022 03:08 PM <DIR&gt; .
    07/17/2022 03:08 PM &lt;DIR&gt; ..
    07/17/2022 03:08 PM 2,072 activate
    07/17/2022 03:08 PM 1,001 activate.bat
    07/17/2022 03:08 PM 22,159 Activate.ps1
    07/17/2022 03:08 PM 393 deactivate.bat
    07/17/2022 03:08 PM 106,362 pip.exe
    07/17/2022 03:08 PM 106,362 pip3.10.exe
    07/17/2022 03:08 PM 106,362 pip3.exe
    07/17/2022 03:08 PM 264,176 python.exe
    07/17/2022 03:08 PM 252,912 pythonw.exe
    9 File(s) 861,799 bytes
    2 Dir(s) 147,161,825,280 bytes free
    
    C:\repository\django\venv\Scripts>cd ..
    
  3. In order to activate the python virtual environment, just execute the command ‘activate’ as follows :

    C:\repository\django\venv\Scripts>activate
    
    (venv) C:\repository\django\venv\Scripts>
    
  4. Following after, just execute the following command for showing the version of the python which act as a base python in the python virtual environment :

    (venv) C:\repository\django\venv\Scripts>python -V
    Python 3.10.5
    
    (venv) C:\repository\django\venv\Scripts>
    
  5. Finally, just execute the command for installing ‘psycopg2’ python library once more as follows :

    (venv) C:\repository\django\venv\Scripts>pip install psycopg2
    Collecting psycopg2
    Using cached psycopg2-2.9.3-cp310-cp310-win_amd64.whl (1.2 MB)
    Installing collected packages: psycopg2
    Successfully installed psycopg2-2.9.3
    
    (venv) C:\repository\django\venv\Scripts>
    

    At last, the execution command for installing ‘psycopg2’ python library is a success.

Leave a Reply