Python 无法通过 pip install 安装 psycopg2 包...这是因为 Sierra 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39767810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:40:43  来源:igfitidea点击:

Can't install psycopg2 package through pip install... Is this because of Sierra?

pythonpippsycopg2

提问by Jome

I am working on a project for one of my lectures and I need to download the package psycopg2 in order to work with the postgresql database in use. Unfortunately, when I try to pip install psycopg2 the following error pops up:

我正在为我的一个讲座开展一个项目,我需要下载包 psycopg2 才能使用正在使用的 postgresql 数据库。不幸的是,当我尝试 pip install psycopg2 时,会弹出以下错误:

ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command '/usr/bin/clang' failed with exit status 1
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command '/usr/bin/clang' failed with exit status 1

Does anyone know why this is happening? Is it because Sierra has not supported some packages? Thanks in advance!

有谁知道为什么会这样?是不是因为 Sierra 不支持某些软件包?提前致谢!

回答by zganger

I fixed this by installing Command Line Tools

我通过安装命令行工具解决了这个问题

xcode-select --install

then installing openssl via Homebrew and manually linking my homebrew-installed openssl to pip:

然后通过 Homebrew 安装 openssl 并手动将我的 homebrew 安装的 openssl 链接到 pip:

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2

on macOS Sierra 10.12.1

在 macOS Sierra 10.12.1 上

回答by Dheeraj Pande

Try installing it with:

尝试安装它:

pip install psycopg2-binary

回答by Ion Gorincioi

brew install postgresql

If postgresql exist then run:

如果 postgresql 存在,则运行:

brew upgrade postgresql
sudo pip install psycopg2

In venv:

在 venv:

env LDFLAGS='-L/usr/local/lib -L/usr/local/opt/openssl/lib -L/usr/local/opt/readline/lib' pip install psycopg2==2.8.4

If you need use only the command pip install psycopg2export the path in macOSX:

如果您只需要使用命令pip install psycopg2export macOSX 中的路径:

export LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib"

回答by Paul Meinshausen

Running into a similar problem using pipenv install psycopg2:

使用 pipenv install psycopg2 遇到类似问题:

ld: library not found for -lssl', ' clang: error: linker command failed with exit code 1 (use -v to see invocation)', " error: command 'clang' failed with exit status 1", '
----------------------------------------',

ld: 找不到用于 -lssl' 的库,'clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)',“错误:命令 'clang' 失败,退出状态为 1”,'
--- -------------------------------------',

I tried all of the recommendations above and none of them worked. I'd faced this problem in another virtualenv a few months ago and remembered reading something about this being a psycopg2 version issue. So I tried installing with 2.7.1 and 2.8.3 and both still failed. Somehow version 2.7.7 worked:

我尝试了上述所有建议,但没有一个奏效。几个月前,我在另一个 virtualenv 中遇到过这个问题,并记得读过一些关于这是psycopg2 版本问题的内容。所以我尝试用 2.7.1 和 2.8.3 安装,但都失败了。不知何故,2.7.7 版有效:

pipenv install psycopg2==2.7.7

Wish I had time to look into this further to understand what's really happening. But for now I don't and this worked.

希望我有时间进一步研究这个问题以了解真正发生的事情。但现在我没有,这奏效了。

回答by einmuya

  1. Install/update Xcode developer tools

    xcode-select --install
    
  2. Query postgres path

    find / -name pg_config  2>/dev/null
    
  3. Install psycopg2, use the path you got in step 2. Mine was '/usr/local/Cellar/postgresql/9.5.0/bin/pg_config'

    PATH=$PATH:/usr/local/Cellar/postgresql/9.5.0/bin/ pip install psycopg2
    
  1. 安装/更新 Xcode 开发者工具

    xcode-select --install
    
  2. 查询 postgres 路径

    find / -name pg_config  2>/dev/null
    
  3. 安装 psycopg2,使用您在第 2 步中获得的路径。我的是'/usr/local/Cellar/postgresql/9.5.0/bin/pg_config'

    PATH=$PATH:/usr/local/Cellar/postgresql/9.5.0/bin/ pip install psycopg2
    

回答by Apurva jain

I fixed it with:

我修复了它:

brew reinstall openssl
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/

回答by Lex Bryan

This works for me (on a terminal):

这对我有用(在终端上):

find / -name pg_config  2>/dev/null

then add the path:

然后添加路径:

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.5/bin/

os version: MacOs Sierra

操作系统版本:MacOs Sierra

I hope this helps.

我希望这有帮助。

回答by mhawke

It looks like the openssl package is not installed. Try installing it and pip installagain. I'm not a macos user, but I believe that brewsimplifies package management on that platform.

看起来好像没有安装 openssl 包。尝试pip install重新安装它。我不是 macos 用户,但我相信这可以brew简化该平台上的包管理。

You might also need to install the Python development and postgresql development packages.

您可能还需要安装 Python 开发和 postgresql 开发包。

回答by Sergey Lukin

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" \
brew upgrade openssl

Then I could install psycopg2

然后我可以安装 psycopg2

回答by Mavrick Laakso

Old, but for folks using pipenv, I resolved this issue by looking at my dependency graph pipenv graph --reverseand noticing it required psycopg2-binary==2.8.2. Running pipenv install psycopg2-binaryresolved the issue.

旧的,但对于使用 pipenv 的人,我通过查看我的依赖关系图pipenv graph --reverse并注意到它需要psycopg2-binary==2.8.2. 运行pipenv install psycopg2-binary解决了这个问题。