当开发系统上未安装 PostgreSQL 时,将 psycopg2 安装到 virtualenv 中
是否可以将 psycopg2
安装到 virtualenv
中我的开发系统(配备 OS X 10.6 的 MacBook Pro)上未安装 PostgreSQL?
当我从 virtualenv
中运行 pip install psycopg2
时,我收到了如下所示的错误。
我正在尝试使用 Django 连接到服务器上的旧数据库,如果可能的话,我不想在我的开发系统上安装 PostgreSQL。
为什么不安装 PostgreSQL?
我使用自制程序安装 PostgreSQL 时收到错误。我的 MacBook Pro 上安装了 Xcode4(而且仅安装了 Xcode4),我认为这与缺少 gcc 4.0 有关。然而,这是另一个 StackOverflow 问题的问题。
2011 年 4 月 12 日上午 8:37 更新:我仍然想知道在我的 MacBook Pro 上不安装 PostgreSQL 是否可行。但是,我运行了 brew update
并使用 brew install --force ossp-uuid
强制重新安装了 ossp-uuid,现在 brew install postgresql
可以工作了。成功安装 PostgreSQL 后,我可以从 virtualenv 中pip install psycopg2
。
来自 pip install psycopg2
的错误
$ pip install psycopg2
Downloading/unpacking psycopg2
Running setup.py egg_info for package psycopg2
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'.
Complete output from command python setup.py egg_info:
running egg_info
writing pip-egg-info/psycopg2.egg-info/PKG-INFO
writing top-level names to pip-egg-info/psycopg2.egg-info/top_level.txt
writing dependency_links to pip-egg-info/psycopg2.egg-info/dependency_links.txt
warning: manifest_maker: standard file '-c' not found
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'.
----------------------------------------
Command python setup.py egg_info failed with error code 1
Storing complete log in /Users/matthew/.pip/pip.log
初步研究
以下是我作为初步研究阅读的文章:
Is it possible to install psycopg2
into a virtualenv
when PostgreSQL isn't installed on my development system—MacBook Pro with OS X 10.6?
When I run pip install psycopg2
from within my virtualenv
, I received the error shown below.
I'm trying to connect to a legacy database on a server using Django, and I'd prefer not to install PostgreSQL on my development system if possible.
Why not install PostgreSQL?
I received an error when installing PostgreSQL using homebrew. I have Xcode4—and only Xcode4—installed on my MacBook Pro and am thinking it's related to missing gcc 4.0. However, this is a problem for another StackOverflow question.
Update 8:37 AM on April 12, 2011: I'd still like to know if this is possible without installing PostgreSQL on my MacBook Pro. However, I ran brew update
and forced a reinstallation of ossp-uuid with brew install --force ossp-uuid
and now brew install postgresql
works. With PostgreSQL successfully installed, I was able to pip install psycopg2
from within my virtualenv.
Error from pip install psycopg2
$ pip install psycopg2
Downloading/unpacking psycopg2
Running setup.py egg_info for package psycopg2
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'.
Complete output from command python setup.py egg_info:
running egg_info
writing pip-egg-info/psycopg2.egg-info/PKG-INFO
writing top-level names to pip-egg-info/psycopg2.egg-info/top_level.txt
writing dependency_links to pip-egg-info/psycopg2.egg-info/dependency_links.txt
warning: manifest_maker: standard file '-c' not found
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'.
----------------------------------------
Command python setup.py egg_info failed with error code 1
Storing complete log in /Users/matthew/.pip/pip.log
Preliminary Research
Below are the articles I read as preliminary research:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
帮助我在 Debian 上挤压
来自评论:
helped me on debian squeeze
From comments:
psycopg依赖于
pg_config
命令,如果没有它,则无法安装psycopg。如果系统安装对您来说是个问题,为什么不尝试编译 PostgreSQL 并将生成的 bin 文件包含在 $PATH 中呢?喜欢:
psycopg depends on
pg_config
command, and if you don't have it, you can't install psycopg.If system installation is a problem to you, why don't you try compiling PostgreSQL and including generated bin files in your $PATH? Like:
也帮助我在 Debian 中挤压。之后执行 pip install psycopg2。当我在heroku上设置我的环境时,我遇到了pg_config未找到的问题,现在它工作正常。
helped me in debian squeeze too . After that do pip install psycopg2. I faced problem of pg_config not found problem when i was setting up my environment on heroku , now its working fine .
您需要安装
python-dev
包才能使用psycopg2
等Python扩展。我不知道如何在 mac 中安装软件包,但我运行以下命令在我的 Ubuntu 机器上安装 python 软件包。或者
如果您使用的是Python3.x。
安装完成后,在虚拟环境中运行以下命令。
You need to install the
python-dev
package in order to make use of python extensions such aspsycopg2
. I don't know how to install packages in mac but I run the following commands to install a python package on my Ubuntu machine.Or
if you are using Python3.x.
Once the installation is finished run the following command within your virtual environment.
您不需要安装完整的 PostgreSQL:您只需要客户端库。
You don't need the complete PostgreSQL installed: you only need the client side libraries.
我在 MAC OSX 中使用以下方法解决了这个问题:
I solved it in MAC OSX using :
使用
pip install psycopg2-binary
,当pip install psycopg2
不起作用时它对我有用。Use
pip install psycopg2-binary
, it worked for me whenpip install psycopg2
wasn't working.使用您在 4 月 12 日更新中描述的方法,我能够安装 PostgreSQL(+1)。请注意,我最初运行的是 Python 2.7.1(32 位),自制程序抛出了一些有关使用 32 位版本 Python 的错误和警告。此后我切换了 2.7.1 的 64/32 位版本,它运行得非常好。
关于 pyscopg2,我可以通过编辑 setup.cfg 将其从源代码安装到我的虚拟环境中。我将 pg_config 设置为自制程序 Cellar 内的正确路径(pg_config=/usr/local/Cellar/postgresql/9.0.4/bin/pg_config)。保存更改后,我运行 python setup.py install 时出现了零问题。值得注意的是,我没有将 Mac 的默认 python 设置设置为 32 位。我从头到尾都使用新的 64 位。
在查看了一些文档之后,我认为如果我将 homebrew 的 postgresql 路径添加到系统路径中,我可以使用 pip 来安装它。
参考:
http://favosdream.blogspot.com /2009/09/make-psycopg2-and-readline-work-in-snow.html
2011 年 6 月 8 日更新:
在将 OS X 上编写的项目移植到 Windows 7 时,我发现我还必须在 Windows 上安装 PostgreSQL。这最终在我的启动屏幕上创建了另一个用户以及其他我不喜欢的东西。在进行一些挖掘时,我在此处找到了 PostgreSQL 的 Windows 驱动程序。此后,我卸载了完整的 PostgreSQL 并安装了 ODBC 驱动程序,到目前为止,该驱动程序运行良好。
为了解决最初的问题,经过更多的挖掘,我想我找到了 OS X 的等效 ODBC 在这里。我还没有机会尝试它们,但这个概念在 Windows 7 上运行得很好。当我有机会尝试它们时,我会更新它。在那之前,我希望这会有所帮助。
Using the method you described in your April 12th update, I was able to install PostgreSQL(+1). Note that I originally was running Python 2.7.1 (32bit) and homebrew threw several errors and warnings regarding using a 32bit version of Python. I have since switched the 64/32 bit version of 2.7.1 and it works like a champ.
Regarding the pyscopg2, I was able to install it into my virtual environment from source by editing setup.cfg. I set pg_config to correct path inside homebrew's Cellar (pg_config=/usr/local/Cellar/postgresql/9.0.4/bin/pg_config). After saving the changes, I ran python setup.py install with zero issues. It's worth noting that I did not set the Mac's default python setting to 32 bit. I used the new 64 bit from start to finish.
After looking over some of the documentation, I think if I added homebrew's postgresql path to the system path I could have used pip to install it.
Reference:
http://favosdream.blogspot.com/2009/09/make-psycopg2-and-readline-work-in-snow.html
Update 6-8-2011:
While porting a project written on OS X to Windows 7, I found out that I had to install PostgreSQL on Windows as well. This ended up creating another user on my start up screen and other things that I just didn't like. While doing some digging I found Windows drivers for PostgreSQL here. I have since uninstalled the full PostgreSQL and installed the ODBC drivers which, thus far, work great.
To address the original question, after doing a bit more digging I think I found the equivalent ODBC for OS X here. I have not had a chance to try them out, but the concept works very well on Windows 7. I will update this when I get a chance to try them out. Until then, I hope this helps.
上面的所有内容对我来说都不起作用(OS Catalina 10.15.1)
与经典的
postgres
和homebrew
版本存在冲突。请通过命令删除homebrew版本
,然后从官网安装:
实际是一个简单的
psycopg2
安装或django-heroku
包。All from the above doesn't work for me (OS Catalina 10.15.1)
There was a conflict with classical
postgres
andhomebrew
version.Please delete homebrew version by command
and then install it from the official website:
It is actual for a simple
psycopg2
install ordjango-heroku
package.如果您最近更新了 python 或更改了默认 python(假设从 3.6 更改为 3.8)。以下代码
将为以前的 python 版本安装/工作。
因此,如果您希望此命令适用于最近更新/更改的 python 版本,请尝试在命令中提及特定版本(例如 python3.8),如
上面的尝试,并使用以下命令
if you have recently updated python or changed default python (let's say from 3.6 to 3.8). The following code
will be installing/working for the previous python version.
so if you want this command to work for the recently updated/changed python version try mentioning that specific version like python3.8 in command like
try above with following
由于我从来不需要在此服务器上安装 postgresql 数据库,因此我在同一服务器上运行 pip install psycopg2 之前在 Ubuntu 14_04 版本上安装了以下库
apt-get install libpq-dev python-dev
然后在虚拟环境中执行 pip install psycopg2 。
输出
收集psycopg2
使用缓存的 psycopg2-2.6.1.tar.gz
为收集的包构建轮子:psycopg2
为 psycopg2 运行 setup.py bdist_wheel
存储在目录:/root/.cache/pip/wheels/e2/9a/5e/7b620848bbc7cfb9084aafea077be11618c2b5067bd532f329
成功构建psycopg2
安装收集的软件包:psycopg2
成功安装psycopg2-2.6.1
As I never needed to install postgresql database on this server I installed the following libraries on Ubuntu 14_04 version before running pip install psycopg2 on the same server
apt-get install libpq-dev python-dev
and then executed pip install psycopg2 within virtual env.
Output
Collecting psycopg2
Using cached psycopg2-2.6.1.tar.gz
Building wheels for collected packages: psycopg2
Running setup.py bdist_wheel for psycopg2
Stored in directory: /root/.cache/pip/wheels/e2/9a/5e/7b620848bbc7cfb9084aafea077be11618c2b5067bd532f329
Successfully built psycopg2
Installing collected packages: psycopg2
Successfully installed psycopg2-2.6.1
我知道您要求开发环境,但如果您要部署在服务器上,例如 Heroku。
只需在项目的 requirements.txt 中添加以下行即可。
django-heroku==0.3.1
因为此软件包本身将在服务器部署上安装所需的软件包,例如psycopg2。
I know you are asking for development environment but if you are deploying on server say, Heroku.
Just add below line in the requirements.txt of your project.
django-heroku==0.3.1
As this package itself will install the required packages like psycopg2 on server deployment.
尝试指定版本,它会起作用,在 venv 中执行
Try to specify the version and it'll works, do it inside the venv
根据我的经验,
apt install libpq-dev
然后尝试在 venv 中安装psycopg2
将解决许多问题,但是有关更多信息,您可以参考此链接:https://www.psycopg.org/docs/install.html
based on my experience,
apt install libpq-dev
then try to installpsycopg2
inside venv will fix many issues, however for more information you can refer to this link:https://www.psycopg.org/docs/install.html