6

I'm trying to deploy my python django rest framework code through render service

Here is my GitHub Code : https://github.com/aza1200/stlib-api

Here I use python version : 3.9.10
Django version : 4.0
poetry version : 1.2.2
django rest frmaework : 3.14.0
psycopg2 = "^2.9.5"
psycopg2-binary = "^2.9.5"

And I've got the message below this.

Dec 8 07:20:11 PM    EnvCommandError
Dec 8 07:20:11 PM  
Dec 8 07:20:11 PM    Command ['/opt/render/project/src/.venv/bin/pip', 'install', '--no-deps', '/opt/render/.cache/.python-poetry/cache/artifacts/77/61/85/97cbac6c92f7c877efbd9c5c3c38e2bbcb447e3f416742c8ffc1b2b2ec/psycopg2-2.9.5.tar.gz'] errored with the following return code 1, and output:
Dec 8 07:20:11 PM    Processing /opt/render/.cache/.python-poetry/cache/artifacts/77/61/85/97cbac6c92f7c877efbd9c5c3c38e2bbcb447e3f416742c8ffc1b2b2ec/psycopg2-2.9.5.tar.gz
Dec 8 07:20:11 PM      Installing build dependencies: started
Dec 8 07:20:11 PM      Installing build dependencies: finished with status 'done'
Dec 8 07:20:11 PM      Getting requirements to build wheel: started
Dec 8 07:20:11 PM      Getting requirements to build wheel: finished with status 'done'
Dec 8 07:20:11 PM      Preparing metadata (pyproject.toml): started
Dec 8 07:20:11 PM      Preparing metadata (pyproject.toml): finished with status 'done'
Dec 8 07:20:11 PM    Building wheels for collected packages: psycopg2
Dec 8 07:20:11 PM      Building wheel for psycopg2 (pyproject.toml): started
Dec 8 07:20:11 PM      Building wheel for psycopg2 (pyproject.toml): finished with status 'error'
Dec 8 07:20:11 PM      error: subprocess-exited-with-error
Dec 8 07:20:11 PM  
Dec 8 07:20:11 PM      × Building wheel for psycopg2 (pyproject.toml) did not run successfully.
Dec 8 07:20:11 PM      │ exit code: 1
Dec 8 07:20:11 PM      ╰─> [40 lines of output]
Dec 8 07:20:11 PM          /tmp/pip-build-env-9rm8i7ap/overlay/lib/python3.9/site-packages/setuptools/config/setupcfg.py:508: SetuptoolsDeprecationWarning: The license_file parameter is deprecated, use license_files instead.
Dec 8 07:20:11 PM            warnings.warn(msg, warning_class)
Dec 8 07:20:11 PM          running bdist_wheel
Dec 8 07:20:11 PM          running build
Dec 8 07:20:11 PM          running build_py
Dec 8 07:20:11 PM          creating build
Dec 8 07:20:11 PM          creating build/lib.linux-x86_64-cpython-39
Dec 8 07:20:11 PM          creating build/lib.linux-x86_64-cpython-39/psycopg2
Dec 8 07:20:11 PM          copying lib/_range.py -> build/lib.linux-x86_64-cpython-39/psycopg2
Dec 8 07:20:11 PM          copying lib/errorcodes.py -> build/lib.linux-x86_64-cpython-39/psycopg2
Dec 8 07:20:11 PM          copying lib/pool.py -> build/lib.linux-x86_64-cpython-39/psycopg2
Dec 8 07:20:11 PM          copying lib/extras.py -> build/lib.linux-x86_64-cpython-39/psycopg2
Dec 8 07:20:11 PM          copying lib/__init__.py -> build/lib.linux-x86_64-cpython-39/psycopg2
Dec 8 07:20:11 PM          copying lib/sql.py -> build/lib.linux-x86_64-cpython-39/psycopg2
Dec 8 07:20:11 PM          copying lib/extensions.py -> build/lib.linux-x86_64-cpython-39/psycopg2
Dec 8 07:20:11 PM          copying lib/errors.py -> build/lib.linux-x86_64-cpython-39/psycopg2
Dec 8 07:20:11 PM          copying lib/_ipaddress.py -> build/lib.linux-x86_64-cpython-39/psycopg2
Dec 8 07:20:11 PM          copying lib/tz.py -> build/lib.linux-x86_64-cpython-39/psycopg2
Dec 8 07:20:11 PM          copying lib/_json.py -> build/lib.linux-x86_64-cpython-39/psycopg2
Dec 8 07:20:11 PM          running build_ext
Dec 8 07:20:11 PM          building 'psycopg2._psycopg' extension
Dec 8 07:20:11 PM          creating build/temp.linux-x86_64-cpython-39
Dec 8 07:20:11 PM          creating build/temp.linux-x86_64-cpython-39/psycopg
Dec 8 07:20:11 PM          gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC "-DPSYCOPG_VERSION=2.9.5 (dt dec pq3 ext lo64)" -DPSYCOPG_DEBUG=1 -DPG_VERSION_NUM=140004 -DHAVE_LO64=1 -DPSYCOPG_DEBUG=1 -I/opt/render/project/src/.venv/include -I/opt/python-installer/Python-3.9.10/include/python3.9 -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-39/psycopg/adapter_asis.o -Wdeclaration-after-statement
Dec 8 07:20:11 PM          In file included from psycopg/adapter_asis.c:28:
Dec 8 07:20:11 PM          ./psycopg/psycopg.h:35:10: fatal error: Python.h: No such file or directory
Dec 8 07:20:11 PM           #include <Python.h>
Dec 8 07:20:11 PM                    ^~~~~~~~~~
Dec 8 07:20:11 PM          compilation terminated.
Dec 8 07:20:11 PM  
Dec 8 07:20:11 PM          It appears you are missing some prerequisite to build the package from source.
Dec 8 07:20:11 PM  
Dec 8 07:20:11 PM          You may install a binary package by installing 'psycopg2-binary' from PyPI.
Dec 8 07:20:11 PM          If you want to install psycopg2 from source, please install the packages
Dec 8 07:20:11 PM          required for the build and try again.
Dec 8 07:20:11 PM  
Dec 8 07:20:11 PM          For further information please check the 'doc/src/install.rst' file (also at
Dec 8 07:20:11 PM          <https://www.psycopg.org/docs/install.html>).
Dec 8 07:20:11 PM  
Dec 8 07:20:11 PM          error: command '/usr/bin/gcc' failed with exit code 1
Dec 8 07:20:11 PM          [end of output]
Dec 8 07:20:11 PM  
Dec 8 07:20:11 PM      note: This error originates from a subprocess, and is likely not a problem with pip.
Dec 8 07:20:11 PM      ERROR: Failed building wheel for psycopg2
Dec 8 07:20:11 PM    Failed to build psycopg2
Dec 8 07:20:11 PM    ERROR: Could not build wheels for psycopg2, which is required to install pyproject.toml-based projects
Dec 8 07:20:11 PM  
Dec 8 07:20:11 PM  
Dec 8 07:20:11 PM    at /home/render/.python-poetry/venv/lib/python3.7/site-packages/poetry/utils/env.py:1195 in _run
Dec 8 07:20:11 PM        1191│                 output = subprocess.check_output(
Dec 8 07:20:11 PM        1192│                     cmd, stderr=subprocess.STDOUT, **kwargs
Dec 8 07:20:11 PM        1193│                 )
Dec 8 07:20:11 PM        1194│         except CalledProcessError as e:
Dec 8 07:20:11 PM      → 1195│             raise EnvCommandError(e, input=input_)
Dec 8 07:20:11 PM        1196│
Dec 8 07:20:11 PM        1197│         return decode(output)
Dec 8 07:20:11 PM        1198│
Dec 8 07:20:11 PM        1199│     def execute(self, bin, *args, **kwargs):
Dec 8 07:20:11 PM  
Dec 8 07:20:11 PM  ==> Build failed 

What should I do for solve this error? Thanks

Umgee
  • 143
  • 1
  • 1
  • 7
  • I think you should only have one or the other of `psycopg2 = "^2.9.5" psycopg2-binary = "^2.9.5"`, not both. Try removing `psycopg2 = "^2.9.5"` from your pyproject.toml file. – Matthias Dec 08 '22 at 13:52
  • Thanks for your Comment. After I remove the psycopg2 and deploy these codes, The Errors remain. – Umgee Dec 08 '22 at 15:29
  • what is the updated stack trace? – Matthias Dec 08 '22 at 15:34
  • I just clarified my error so that stack users can understand my error specifically. – Umgee Dec 08 '22 at 16:16
  • Thank you! I finally solved the problem, After I removed the psycopg2 and In the pyproject.toml file,then I removed the "include " variable. Thanks. – Umgee Dec 09 '22 at 03:04

5 Answers5

12

Make sure you have the necessary system dependencies installed for psycopg2. On Ubuntu or Debian, you can run the following command to install them:

sudo apt-get install libpq-dev python3-dev

Then run the installation again after successful dependency installation.

pip install psycopg2
Nejat Njono
  • 254
  • 4
  • 9
6

You should only have one or the other of

psycopg2 = "^2.9.5"
psycopg2-binary = "^2.9.5"

not both.

Try removing psycopg2 = "^2.9.5" from your pyproject.toml file.

Matthias
  • 3,160
  • 2
  • 24
  • 38
1

The document says that we have two options:

Option 1: Quick Install
Script: pip install psycopg2-binary
Pros: Quick & Easy
Cons: Not suitable for production.

Option 2: Install from the source
Script: pip install psycopg2 with a few prerequisites.
Pros: Recommended for production use
Cons: Quite a few tedious prerequisites

yeiichi
  • 131
  • 2
  • 4
0

If someone has same error on Ubuntu 22.04 with python 3.11 install:

pip install python3.11-dev
Stas Oknedis
  • 156
  • 2
  • 5
0

What solved for me was: apt install libpq-dev gcc

GitHunter0
  • 424
  • 6
  • 10