Python Mac + virtualenv + pip + postgresql = 错误:找不到 pg_config 可执行文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20170895/
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
Mac + virtualenv + pip + postgresql = Error: pg_config executable not found
提问by KGo
I was trying to install postgres for a tutorial, but pipgives me error:
我试图为教程安装 postgres,但pip给了我错误:
pip install psycopg
A snip of error I get:
我得到的错误片段:
Error: pg_config executable not found.
Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:
python setup.py build_ext --pg-config /path/to/pg_config build ...
or with the pg_config option in 'setup.cfg'.
Where is pg_configin my virtualenv? How to configure it? I'm using virtualenv because I do not want a system-wide installation of postgres.
pg_config我的 virtualenv在哪里?如何配置它?我正在使用 virtualenv,因为我不想在系统范围内安装 postgres。
回答by Holy Mackerel
virtualenv is for python packages. I don't think you'll be able to contain postgres inside a virtualenv. The error message you're seeing is presumably because you haven't yet installed postgres. The psycopg2 install script is looking for postgres files (in this case pg_config) and not finding them because it is not installed. postgres can't be installed using pip or virtualenv.
virtualenv 用于 python 包。我认为您无法在 virtualenv 中包含 postgres。您看到的错误消息可能是因为您尚未安装 postgres。psycopg2 安装脚本正在寻找 postgres 文件(在本例中为 pg_config)并且没有找到它们,因为它没有安装。无法使用 pip 或 virtualenv 安装 postgres。
回答by l'L'l
This error is caused when the build tools can't find the Postgresql libraries.
当构建工具找不到 Postgresql 库时会导致此错误。
Often it's required to instruct psycopg2 how to find the pg_config binary, you can:
通常需要指示 psycopg2 如何找到 pg_config 二进制文件,您可以:
add the path to pg_config in your shell path (/usr/local/pgsql/bin/)
or edit the setup.cfg file in the psycopg2 source folder and provide the full path to pg_config on the line that starts with pg_config=
在 shell 路径 (/usr/local/pgsql/bin/) 中添加 pg_config 的路径
或编辑 psycopg2 源文件夹中的 setup.cfg 文件,并在以 pg_config= 开头的行上提供 pg_config 的完整路径
pg_config=/usr/local/pgsql/bin/pg_config
pg_config=/usr/local/pgsql/bin/pg_config
- the above is an example, you can do
locate pg_configto find out where it resides, or simply typewhich pg_configand it should tell you the path.
- 上面是一个例子,你可以
locate pg_config找出它所在的位置,或者简单地输入which pg_config它应该告诉你路径。
Less often the error comes from not having postgresql installed on your system. If so, download and build postgres, or download a pre-built psycopg2 binary for OS X.
不太常见的错误是由于您的系统上没有安装 postgresql。如果是这样,请下载并构建 postgres,或为 OS X 下载预构建的 psycopg2 二进制文件。
回答by Mingyu
On Mac, the solution is to install postgresql:
在 Mac 上,解决方案是安装postgresql:
brew install postgresql
On CentOS, the solution is to install postgresql-devel:
在 CentOS 上,解决方案是安装postgresql-devel:
sudo yum install postgresql-devel
pg_configis in postgresql-develpackage
pg_config在postgresql-devel包裹里
回答by Kevin Dalias
Don't forget that your $PATH variable in the virtual environment != your global $PATH variable. You can confirm this with 'echo $PATH' in your virtualenv and also in a new shell. So, unless you want to install PostgreSQL as a unique instance inside your virtual environment (not a thing worth doing, imo), you'll need to modify the $PATH variable within the virtualenv to include the path to your global installation (which will solve your missing pg_config error).
不要忘记您在虚拟环境中的 $PATH 变量!=您的全局 $PATH 变量。您可以在 virtualenv 和新 shell 中使用 'echo $PATH' 确认这一点。所以,除非你想在你的虚拟环境中安装 PostgreSQL 作为一个唯一的实例(这不是一件值得做的事,imo),你需要修改 virtualenv 中的 $PATH 变量以包含全局安装的路径(这将解决您丢失的 pg_config 错误)。
Here are the steps:
以下是步骤:
1.) In a new shell, type 'which pg_config'. This will return the path. Copy it. In my case, the path looked like this: /Applications/Postgres.app/Contents/Versions/9.3/bin
1.) 在一个新的 shell 中,输入“which pg_config”。这将返回路径。复制它。就我而言,路径如下所示:/Applications/Postgres.app/Contents/Versions/9.3/bin
2.) Back in your virtualenv shell, type 'export PATH=/your-path-to-pg_config:$PATH'
2.) 回到你的 virtualenv shell,输入'export PATH=/your-path-to-pg_config:$PATH'
3.) Then, still within the virtualenv, 'pip install psycopg2'
3.) 然后,仍然在 virtualenv 中,'pip install psycopg2'
If all goes according to plan, this will install psycopg2 within the virtual environment, but the installation will refer to your Global PostgreSQL installation. In my case, this Global installation was installed via Postgres.App, hence the path. I prefer this method of working with psycopg2 as it means I can use the database easily within any virtualenv rather than only within the defined virtual environment.
如果一切按计划进行,这将在虚拟环境中安装 psycopg2,但安装将参考您的 Global PostgreSQL 安装。就我而言,此全局安装是通过 Postgres.App 安装的,因此是路径。我更喜欢这种使用 psycopg2 的方法,因为这意味着我可以在任何 virtualenv 中轻松使用数据库,而不仅仅是在定义的虚拟环境中。
Hope this helps anyone who arrives here. For Google juice, here's the explicit (and vague) error language returned when you run into this problem:
Command python setup.py egg_info failed with error code 1
希望这对到达这里的任何人都有帮助。对于 Google 果汁,这里是您遇到此问题时返回的明确(和模糊)错误语言:
命令 python setup.py egg_info failed with error code 1
回答by mrgnw
Here's how I was able to solve this problem on my Mac (OSX 10.9):
以下是我在 Mac (OSX 10.9) 上解决此问题的方法:
brew update
brew install --force ossp-uuid
brew install postgresql
pip install psycopg
I got a CLANG error when I tried pip install psycopg(an LLVM 5.1 issue), so I had to install psycopg with this command instead:
我在尝试时遇到了 CLANG 错误pip install psycopg(LLVM 5.1 问题),所以我不得不使用以下命令安装 psycopg:
ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install psycopg
It's similar to Mingyu'ssolution, but there are enough differences that I thought it was worth sharing.
它与明宇的解决方案相似,但有足够的差异,我认为值得分享。
回答by Max
On Windows I installed postgres manually from http://www.enterprisedb.com/products-services-training/pgdownload#windows. After that the same command works.
在 Windows 上,我从http://www.enterprisedb.com/products-services-training/pgdownload#windows手动安装了 postgres 。之后相同的命令起作用。
回答by bkev
On the Mac, if you're using Postgres.app, the pg_config file is in your /Applications/Postgres.app/Contents/Versions/<current_version>/bindirectory. That'll need to be added to your system path to fix this error, like this:
在 Mac 上,如果您使用Postgres.app,则 pg_config 文件位于您的/Applications/Postgres.app/Contents/Versions/<current_version>/bin目录中。需要将其添加到您的系统路径中以修复此错误,如下所示:
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/<current_version>/bin
So for example, if the current Postgres.app version is 9.5, this export line would be:
因此,例如,如果当前的 Postgres.app 版本是 9.5,则此导出行将是:
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.5/bin
With more recent versions of the Postgres.app (> 9.5?), you can simply add "latest" in place of the version number, like so:
使用较新版本的 Postgres.app(> 9.5?),您可以简单地添加“最新”来代替版本号,如下所示:
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin
回答by andilabs
I totally agree with john hightthat most of posted answers are totally offtopic assuming the OP exactly specified need of using virtualenv.
我完全同意john hight 的观点,假设 OP 完全指定了使用virtualenv 的需求,那么大多数发布的答案都是完全离题的。
For me the answer was runing following command in prompt while having activated virtualenv:
对我来说,答案是在激活 virtualenv 的同时在提示符下运行以下命令:
export PATH="/Applications/Postgres.app/Contents/Versions/9.4/bin:$PATH"
(notice that part 9.4 stands for version and may vary)
(请注意,第 9.4 部分代表版本,可能会有所不同)
or if you want to use the latestinstalled version of Postgres:
或者如果您想使用最新安装的 Postgres 版本:
export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH"
and then:
进而:
pip install psycopg2
goes sucesfully assuming you have installed postgres. And if not, then remember that the best and recomended solution is to use: Postgres.app
假设您已安装 postgres,则成功。如果没有,请记住最好和推荐的解决方案是使用:Postgres.app
回答by marcanuy
On UbuntuI just needed the postgres dev package:
在Ubuntu 上,我只需要 postgres 开发包:
sudo apt-get install postgresql-server-dev-all
回答by Turgut Sar??am
In addition to the answers provided by @bkev and @andi, according to the documentationon Postgres.app, you should add the following to your bash_profile on Mac:
除了@bkev 和@andi 提供的答案之外,根据Postgres.app上的文档,您应该将以下内容添加到 Mac 上的 bash_profile 中:
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin
Note that, there is no hard-coded version number. I wanted to add this as a comment to above answers, but I don't have enough rep for this.
请注意,没有硬编码的版本号。我想将此添加为上述答案的评论,但我没有足够的代表。

