Subject: Does psycopg2-binary provide the psycopg2 package?

From: Kerrick Staley <[email-masked]>
Date: Mon, 22 May 2023 12:05:43 -0400

If I install psycopg2-binary, and then I try to install a package that
depends on psycopg2, will pip "know" that psycopg2-binary satisfies the
psycopg2 requirement and avoid installing psycopg2?

Empirically the answer seems to be "no". I installed psycopg2-binary, then
ran `python3 -m pip install -e .` in a directory with this setup.py file
<https://gist.github.com/kerrick-js/b0962b74853cc297cf2186d251763262>, and
it tried to install psycopg2.

Is there a way that, in my setup.py file, I can depend on either psycopg2
or psycopg2-binary, whichever is available?

Thanks,
Kerrick

---

From: Daniele Varrazzo <[email-masked]>
Date: Tue, 23 May 2023 13:18:17 +0200

On Tue, 23 May 2023 at 08:52, Kerrick Staley <[email-masked]> wrote:
>
> If I install psycopg2-binary, and then I try to install a package that depends on psycopg2, will pip "know" that psycopg2-binary satisfies the psycopg2 requirement and avoid installing psycopg2?
>
> Empirically the answer seems to be "no". I installed psycopg2-binary, then ran `python3 -m pip install -e .` in a directory with this setup.py file, and it tried to install psycopg2.
>
> Is there a way that, in my setup.py file, I can depend on either psycopg2 or psycopg2-binary, whichever is available?

I don't think there is. You cannot make a package depending on either
one or the other. Python packaging metadata doesn't offer this
flexibility.

In psycopg 3 the only package to depend on is `psycopg`. The
pre-compiled C module (the `psycopg-binary` package on pypi) can be
added as an add-on and it is not necessary for a library to depend on
it. It can be an application requirement instead.

In my projects I use `psycopg` as an abstract dependency (in the
`setup.cfg` of the packages that need psycopg) and
`psycopg-binary==3.x.x` as a concrete dependency (in a
`requirements.txt` file generated by `pip-tools` using `psycopg-binary
in the `requirements.in`)

More details about psycopg 3 versions management are available at
https://www.psycopg.org/psycopg3/docs/basic/install.html#handling-dependencies

-- Daniele