6

Is there some magic trick to get pipenv to install psycopg2?

My Pipfile looks like this:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
django = "==2.1.3"
psycopg2 = "*"

[dev-packages]

[requires]
python_version = "3.6"

I've tried adding and taking away an install of pyscopg2-binary, but it makes no difference. I am on a Mac running OSX 10.14.4.

Here is the output I get after running pipenv install:

An error occurred while installing psycopg2==2.8.3 --hash=sha256:128d0fa910ada0157bba1cb74a9c5f92bb8a1dca77cf91a31eb274d1f889e001 --hash=sha256:227fd46cf9b7255f07687e5bde454d7d67ae39ca77e170097cdef8ebfc30c323 --hash=sha256:2315e7f104681d498ccf6fd70b0dba5bce65d60ac92171492bfe228e21dcc242 --hash=sha256:4b5417dcd2999db0f5a891d54717cfaee33acc64f4772c4bc574d4ff95ed9d80 --hash=sha256:640113ddc943522aaf71294e3f2d24013b0edd659b7820621492c9ebd3a2fb0b --hash=sha256:897a6e838319b4bf648a574afb6cabcb17d0488f8c7195100d48d872419f4457 --hash=sha256:8dceca81409898c870e011c71179454962dec152a1a6b86a347f4be74b16d864 --hash=sha256:b1b8e41da09a0c3ef0b3d4bb72da0dde2abebe583c1e8462973233fd5ad0235f --hash=sha256:cb407fccc12fc29dc331f2b934913405fa49b9b75af4f3a72d0f50f57ad2ca23 --hash=sha256:d3a27550a8185e53b244ad7e79e307594b92fede8617d80200a8cce1fba2c60f --hash=sha256:f0e6b697a975d9d3ccd04135316c947dd82d841067c7800ccf622a8717e98df1! Will try again.
  🐍   β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰ 7/7 β€” 00:00:45
Installing initially failed dependencies…
[pipenv.exceptions.InstallError]:   File "/usr/local/Cellar/pipenv/2018.11.26_2/libexec/lib/python3.7/site-packages/pipenv/core.py", line 1874, in do_install
[pipenv.exceptions.InstallError]:       keep_outdated=keep_outdated
[pipenv.exceptions.InstallError]:   File "/usr/local/Cellar/pipenv/

............
...........

6m-darwin.so', '    ld: library not found for -lssl', '    clang: error: linker command failed with exit code 1 (use -v to see invocation)', "    error: command 'gcc' failed with exit status 1", '    ----------------------------------------', 'ERROR: Command "/Users/mdd/.local/share/virtualenvs/justfacts-2tIl3QYb/bin/python3 -u -c \'import setuptools, tokenize;__file__=\'"\'"\'/private/var/folders/jz/s0hyvf2n5yd8glg82gphcn9h0000gp/T/pip-install-f6uqfqa9/psycopg2/setup.py\'"\'"\';f=getattr(tokenize, \'"\'"\'open\'"\'"\', open)(__file__);code=f.read().replace(\'"\'"\'\\r\\n\'"\'"\', \'"\'"\'\\n\'"\'"\');f.close();exec(compile(code, __file__, \'"\'"\'exec\'"\'"\'))\' install --record /private/var/folders/jz/s0hyvf2n5yd8glg82gphcn9h0000gp/T/pip-record-k9xlsee0/install-record.txt --single-version-externally-managed --compile --install-headers /Users/mdd/.local/share/virtualenvs/justfacts-2tIl3QYb/bin/../include/site/python3.6/psycopg2" failed with error code 1 in /private/var/folders/jz/s0hyvf2n5yd8glg82gphcn9h0000gp/T/pip-install-f6uqfqa9/psycopg2/']

I've tried the recommended commands, such as pipenv clean, even tried everything from scratch in new directory and no Pipfile, but no luck. When I attempt to install psycopg2 directly via pipenv install pyscopg2, same result.

3
  • The ssl library is missing. Try brew install openssl and then run the pipenv command again.
    – heemayl
    Commented Jun 27, 2019 at 19:13
  • @heemayl just confirmed that openssl is already installed. Commented Jun 27, 2019 at 19:23
  • 1
    Try brew unlink openssl && brew link openssl --force
    – heemayl
    Commented Jun 27, 2019 at 19:25

4 Answers 4

8

Expanding on heemayl's comment, I ultimately needed to run:

brew unlink openssl && brew link openssl --force

Then setup a bunch of environment variables:

echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

Then delete the Pipfile.lock and reinstall everything again, but initially skipping the lock file creation via pipenv install --skip-lock and then recreate the lock file with pipenv lock

1
5

For me, running Ubuntu 20.04 running under WSL2 I had to follow the instructions here

sudo apt install python3-dev libpq-dev

then I ran pipenv shell to start up my environment.

Then running: pip install psycopg2 ran without issue.

2
  • Interesting, so did you have any reference to psycopg2 in your Pipfile or is it left as a silent dependency? Commented Apr 6, 2023 at 18:23
  • @combinatorist - later I added it to my requirements.txt as psycopg2-binary
    – JoeManFoo
    Commented May 19, 2023 at 4:19
2

You are unable to build it. Try installing this instead: https://pypi.org/project/psycopg2-binary/

2
  • How do you suggest installing? With pipenv? Commented Jun 27, 2019 at 19:03
  • Yes. Set it as a dependency as you would any other package.
    – Javier
    Commented Jun 28, 2019 at 7:14
2

I just ran into this exact problem. I had to remove my current virtualenv and rebuild it.

pipenv --rm
rm Pipfile.lock
pipenv install

Your Answer

By clicking β€œPost Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.