Mac + virtualenv + pip + postgresql = Error: pg_co

2019-01-12 21:29发布

I was trying to install postgres for a tutorial, but pip gives me error:

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_config in my virtualenv? How to configure it? I'm using virtualenv because I do not want a system-wide installation of postgres.

14条回答
孤傲高冷的网名
2楼-- · 2019-01-12 22:01

Here's how I was able to solve this problem on my 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:

ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install psycopg

It's similar to Mingyu's solution, but there are enough differences that I thought it was worth sharing.

查看更多
倾城 Initia
3楼-- · 2019-01-12 22:02

On Ubuntu I just needed the postgres dev package:

sudo apt-get install postgresql-server-dev-all
查看更多
我命由我不由天
4楼-- · 2019-01-12 22:04

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.

查看更多
家丑人穷心不美
5楼-- · 2019-01-12 22:09

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).

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

2.) Back in your virtualenv shell, type 'export PATH=/your-path-to-pg_config:$PATH'

3.) Then, still within the 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.

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

查看更多
Fickle 薄情
6楼-- · 2019-01-12 22:11

On Windows I installed postgres manually from http://www.enterprisedb.com/products-services-training/pgdownload#windows. After that the same command works.

查看更多
相关推荐>>
7楼-- · 2019-01-12 22:13

I totally agree with john hight that most of posted answers are totally offtopic assuming the OP exactly specified need of using virtualenv.

For me the answer was runing following command in prompt while having activated virtualenv:

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

(notice that part 9.4 stands for version and may vary)

or if you want to use the latest installed version of 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

查看更多
登录 后发表回答