Psycopg fatal error python h no such file or directory

How to install the psycopg2 Python package in WSL 2

This post is a quick note to all about how to install psycopg2 in Linux. I used WSL 2 running Ubuntu 20.04.

Background

I was trying to set up a toy project to learn Django using PostgreSQL. When I tried to install the recommend psycopg2 package from pip, I received two error messages. The first error I received was: error: invalid command ‘bdist_wheel’ . My second error was related to C / C++ and was in my log files: In file included from psycopg/adapter_asis.c:28:

./psycopg/psycopg.h:35:10: fatal error: Python.h: No such file or directory 35 | #include Python.h> | ^~~~~~~~~~ 

The Steps

  1. I installed the following dependencies: python-dev , python3-dev .
  2. I installed wheels using pip3 install wheel .
  3. I installed python[your version]-dev using Ubuntu’s sudo command.

Explanation

The root cause of the problem was that psycopg2 was looking for headers for Python 3.8.5. Since I am using Python 3.9.5, the headers could not be located and psycopg2 could not install. After installing python3.9-dev , psycopg2 successfully installed on my system.

I hope that this short post saves people time and energy.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Installing psycopg2 on custom runners is giving error for python3.11 #551

Installing psycopg2 on custom runners is giving error for python3.11 #551

Comments

Description:

Installing psycopg2 on python3.11 on custom runners gives error

Action version:

Runner type:

Tools version:

Repro steps:

Once this is running on a custom runner, here is what I had to do reproduce the issue

mkdir ~/.x cd ~/.x /home/ubuntu/actions-runner/_work/_tool/Python/3.11.0/x64/bin/python3.11 -m venv venv source venv/bin/activate pip install -U pip wheel pip install psycopg2 

Expected behavior:

psycopg2 should install correctly

Actual behavior:

Collecting psycopg2 Using cached psycopg2-2.9.5.tar.gz (384 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 ╰─> [40 lines of output] /home/ranjith/.x/venv/lib/python3.11/site-packages/setuptools/config/setupcfg.py:508: 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.linux-x86_64-cpython-311 creating build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/pool.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/tz.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/_range.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/_ipaddress.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/errors.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/errorcodes.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/__init__.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/extras.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/sql.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/extensions.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/_json.py -> build/lib.linux-x86_64-cpython-311/psycopg2 running build_ext building 'psycopg2._psycopg' extension creating build/temp.linux-x86_64-cpython-311 creating build/temp.linux-x86_64-cpython-311/psycopg gcc -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC "-DPSYCOPG_VERSION=2.9.5 (dt dec pq3 ext lo64)" -DPSYCOPG_DEBUG=1 -DPG_VERSION_NUM=140005 -DHAVE_LO64=1 -DPSYCOPG_DEBUG=1 -I/home/ranjith/.x/venv/include -I/opt/hostedtoolcache/Python/3.11.0/x64/include/python3.11 -I. -I/usr/include/postgresql -I/usr/include/postgresql/14/server -I/usr/include/libxml2 -c psycopg/adapter_asis.c -o build/temp.linux-x86_64-cpython-311/psycopg/adapter_asis.o -Wdeclaration-after-statement In file included from psycopg/adapter_asis.c:28: ./psycopg/psycopg.h:35:10: fatal error: Python.h: No such file or directory 35 | #include | ^~~~~~~~~~ compilation terminated. It appears you are missing some prerequisite to build the package from source. You may install a binary package by installing 'psycopg2-binary' from PyPI. If you want to install psycopg2 from source, please install the packages required for the build and try again. For further information please check the 'doc/src/install.rst' file (also at ). error: command '/usr/bin/gcc' failed with exit code 1 [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 ╰─> [42 lines of output] /home/ranjith/.x/venv/lib/python3.11/site-packages/setuptools/config/setupcfg.py:508: SetuptoolsDeprecationWarning: The license_file parameter is deprecated, use license_files instead. warnings.warn(msg, warning_class) running install /home/ranjith/.x/venv/lib/python3.11/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.linux-x86_64-cpython-311 creating build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/pool.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/tz.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/_range.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/_ipaddress.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/errors.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/errorcodes.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/__init__.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/extras.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/sql.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/extensions.py -> build/lib.linux-x86_64-cpython-311/psycopg2 copying lib/_json.py -> build/lib.linux-x86_64-cpython-311/psycopg2 running build_ext building 'psycopg2._psycopg' extension creating build/temp.linux-x86_64-cpython-311 creating build/temp.linux-x86_64-cpython-311/psycopg gcc -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC "-DPSYCOPG_VERSION=2.9.5 (dt dec pq3 ext lo64)" -DPSYCOPG_DEBUG=1 -DPG_VERSION_NUM=140005 -DHAVE_LO64=1 -DPSYCOPG_DEBUG=1 -I/home/ranjith/.x/venv/include -I/opt/hostedtoolcache/Python/3.11.0/x64/include/python3.11 -I. -I/usr/include/postgresql -I/usr/include/postgresql/14/server -I/usr/include/libxml2 -c psycopg/adapter_asis.c -o build/temp.linux-x86_64-cpython-311/psycopg/adapter_asis.o -Wdeclaration-after-statement In file included from psycopg/adapter_asis.c:28: ./psycopg/psycopg.h:35:10: fatal error: Python.h: No such file or directory 35 | #include | ^~~~~~~~~~ compilation terminated. It appears you are missing some prerequisite to build the package from source. You may install a binary package by installing 'psycopg2-binary' from PyPI. If you want to install psycopg2 from source, please install the packages required for the build and try again. For further information please check the 'doc/src/install.rst' file (also at ). error: command '/usr/bin/gcc' failed with exit code 1 [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. 

The text was updated successfully, but these errors were encountered:

Источник

Решение проблемы с ошибкой «fatal error: Python.h: Нет такого файла или каталога»

Если при компиляции программы вы получаете ошибку, что отсутствует файл Python.h, то необходимо установить дополнительный пакет.

Вам нужно обратить внимание, какая версия Python используется для компиляции программы: 2.x или 3.x. Файлы заголовков помещены в различные пакеты для этих версий, поэтому вам нужно установить правильный пакет, в соответствии с используемой при компиляции версией Python. В большинстве популярных дистрибутивов требуемый пакет имеется в стандартном репозитории, поэтому установка выполняется в одну команду.

Текст ошибки может чуть различаться, в зависимости от того, в каком файле она возникла. Примеры сообщений:

url/url.cpp:4:10: fatal error: Python.h: Нет такого файла или каталога #include "Python.h" ^~~~~~~~~~ compilation terminated. error: Setup script exited with error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
utilsmodule.c:1:20: fatal error: Python.h: No such file or directory compilation terminated.

Самой вероятной причиной этого является то, что не установлены файлы заголовка и статичных библиотек для python dev. Для установки их на системном уровне используйте менеджер пакетов для вашего дистрибутива.

Для apt (Ubuntu, Debian, Kali Linux, Linux Mint…):

Если программа компилируется для python2.x, то выполните команду:

sudo apt install python-dev

Если программа компилируется для python3.x, то выполните команду:

sudo apt install python3-dev

Для yum (CentOS, RHEL…):

sudo yum install python-devel
sudo yum install python34-devel

Если вам нужно установить для других версий Python, то замените цифры на нужные, например:

sudo yum install python36u-devel

Для dnf (Fedora…):

sudo dnf install python2-devel
sudo dnf install python3-devel

Для zypper (openSUSE…):

sudo zypper in python-devel
sudo zypper in python3-devel

Если после установки заголовков проблема не исчезла, то возможно, что вы выбрали неверную версию Python.

Связанные статьи:

Источник

Читайте также:  Убрать все html теги php
Оцените статью