How to install psycopg2 for python2.6 when there is also 2.7 installed?

柔情痞子 提交于 2019-12-12 05:15:44

问题


I tried to install psycopg2 for python2.6 but my system also has 2.7 installed.

I did

>sudo -E pip install psycopg2
Downloading/unpacking psycopg2
  Downloading psycopg2-2.5.3.tar.gz (690kB): 690kB downloaded
  Running setup.py (path:/private/var/folders/yw/qn50zv_151bfq2vxhc60l8s5vkymm3/T/pip_build_root/psycopg2/setup.py) egg_info for package psycopg2

Installing collected packages: psycopg2
  Running setup.py install for psycopg2
    building 'psycopg2._psycopg' extension
    cc -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -Qunused-arguments -Qunused-arguments -arch x86_64 -arch i386 -pipe -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION="2.5.3 (dt dec pq3 ext)" -DPSYCOPG_EXTENSIONS=1 -DPSYCOPG_NEW_BOOLEAN=1 -DHAVE_PQFREEMEM=1 -DPG_VERSION_HEX=0x09010B -DPSYCOPG_EXTENSIONS=1 -DPSYCOPG_NEW_BOOLEAN=1 -DHAVE_PQFREEMEM=1 -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -I. -I/usr/local/Cellar/postgresql91/9.1.11/include -I/usr/local/Cellar/postgresql91/9.1.11/include/server -c psycopg/psycopgmodule.c -o build/temp.macosx-10.9-intel-2.7/psycopg/psycopgmodule.o
...

You can see that pip is associating psycopg2 with python2.7 during the install process. This pip install completed with a reported success result but when I ran a 2.6 script after that that imports psycopg2, it couldn't find it:

    import psycopg2
ImportError: No module named psycopg2

How do I install psycopg2 for python2.6 when there is also 2.7 installed?


回答1:


As per this answer, I installed pip2.6. Then I installed psycopg2 using the pip2.6 command and not pip like before:

sudo -E pip2.6 install psycopg2

Then it all worked




回答2:


Use python virtualenvs.

Virtualenv is a box for python and bunch of packages. So instead of makins your system a mess by mixing python and system packages you are doing like this.

$ virtualenv my_env26 -p python2.6
$ . my_env26/bin/activate
$ pip install whatever_i_want

$ virtualenv my_env27 -p python2.7
$ . my_env27/bin/activate
$ pip install another_packages

In that way you can have python environment with packages per project/per python version or grouped in any way you want. Profit is that you can remove this folder whenever you want very easly. It's harder when you mix system and python packages.

More about virtualenvs



来源:https://stackoverflow.com/questions/24294015/how-to-install-psycopg2-for-python2-6-when-there-is-also-2-7-installed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!