Anaconda 安装 PSYCOPG2 on windows

来源:互联网 发布:手机淘宝收藏软件 编辑:程序博客网 时间:2024/04/29 18:39

转自http://cecilialiao.com/2015/06/08/installing-psycopg2-for-python-2-7-on-windows-x64/

ATTEMPT 1: CONDA INSTALL PSYCOPG2

Because I was using the Anaconda build of Python, I always give this command line a try (rightly or wrong!). If Continuum, the folks who bring us Anaconda, has the library on their repository, why look anywhere else?
C:\cecilia.liao> conda install psycopg2

Unfortunately, this didn’t work.

ATTEMPT 2: PIP INSTALL PSYCOPG2

I’ve found that using a python package manager is usually the easiest way to install third party libraries.

C:\cecilia.liao> pip install psycopg2

Unfortunately, the installation failed and mentioned something about requiring python-dev and libpq-dev. Trying to install these two libraries via pip was not the quick solution I had hoped for, and the impatient me couldn’t find much details about these two libraries that didn’t mention Linux.

Time to change tack.

ATTEMPT 3: INSTALL VIA EXE

The maker of psycopg2 refers to a Windows port kindly provided by another developer. Bingo, exe files. Double click and away I go!

Correct version downloaded, installation finished, but why won’t psycopg2 import!?

Windows -> Control Panel -> Programs and Features -> Python 2.7 psycopg2 -> Uninstall

ATTEMPT 4: INSTALL VIA GIT HUB

Before opening another can of search worms, I noticed that the chap who provided the exe files mentioned yet another developer who has provided pip packages of psycopg2 for Windows. I suddenly felt reassured that I can’t be the only one struggling with this same problem.

C:\cecilia.liao> pip install git+https://github.com/nwcell/psycopg2-windows.git@win64-py27#egg=psycopg2

Voila, installation successful. Quickly opened an instance of python and import psycopg2 didn’t generate an error message. Fantastic! Time to go back to Spyder 2 to finish my work.

C:\cecilia.liao> python
 
>>> import psycopg2
ImportError: No module named psycopg2

What?! Turns out, despite working in the plain vanilla version of Python, the library wasn’t installed in the Anaconda build of Python. I double checked %PATH% environment variable which didn’t provide an immediately obvious answer. Is it time to give up and use a completely different driver?!

Back to command prompt:
C:\cecilia.liao> pip uninstall git+https://github.com/nwcell/psycopg2-windows.git@win64-py27#egg=psycopg2

ATTEMPT 5: EASY INSTALL OF EXE

In the half hour (maybe more…) I spent investigating this, I remembered skimming through a mention of easy install. Last ditch effort… I used command prompt to navigate to the folder where the exe (from step 2) was saved.

此处的easy_install 应该用anaconda下script文件夹中的easy_install,即在cmd下进入该文件夹,再执行。然后,后面的exe应该用全路径。

PS. 此处的psycopg2的版本号需要与anaconda的版本号对应,即要么都是32位,要么都是64位。

C:\cecilia.liao\downloads> easy_install psycopg2-2.6.0.win-amd64-py2.7-pg9.4.1-release.exe

Installation was successful but by now I was sceptical if I would ever get this library to play ball.

这里的ipython如果没有在path中定义过的话,还是进入到anaconda下的目录,然后直接执行python即可。

C:\cecilia.liao\> ipython #opens Anaconda build of Python
 
>>> import psycopg2 #not going to hold my breath
 
>>>

Huh, what? No error message? Oh, it worked…. it worked!


0 0