Installing psycopg2 for Python 3.4 on Windows using pip

I did it! Thanks to various people on the internet! But how did I install psycopg2 for python 3.4 on Windows 8.1?

Installation Process

I will walk you trough the installation process in a close to step by step manner, but only comment in depth when I feel the urge it is nessesary. Also keep in mind, that all used software versions are 64bit (amd64)!

Get Python3

Well the first step is to install python 3.4, which is most easily done using the official installer from https://www.python.org/downloads/windows/

I installed python to the default directory at c:\Python34 which was okay for me. It actually doesn’t really matter where python is installed to, but that it is installed. I would recommend to check the add python to path option during the install, so that you can call python from the command line.

Compiler

The next big „problem“ is to install the right C and C++ compiler

C:\Users\Benjamin>python
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:16:31) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

The MSC v.1600 64bit gives us the hint with which version of the Microsoft C/C++ Compiler Python was compiled. As much as I have found out by reading various posts all over the net concerning python and python modules is, that modules have to be compiled with the same compiler version as python has been. I don’t ask why, but just take it as a given. So we need to find the right compiler on the net, download and install it. Tricky!! Very Tricky! So which compiler version do we need? Have a look at http://stackoverflow.com/a/2676904. There MSC v.1600 corresponse with the Visual C++ 2010 version.

There is a help page from Microsoft explaining how to configure a 64bit project for the Visual C++ 2010 compiler. Since we are not using Microsoft Visual Studio only the information concerning the compiler are relevant on this help page. To get a 64bit compiler we should download the Microsoft Windows SDK for Windows 7 and .NET Framework 4 and install the following components

What the help page does not state is, that you should uninstall all previous installed Microsoft Visual C++ 2010 Redistributables x86 and x64 otherwise the installer fails!

PostgreSQL

Well installing PostgreSQL is not a big deal. Just download it from http://www.postgresql.org/download/windows/ and install it.

Installing new python modules

After we installed the compiler we want to use it 🙂 But before we can use commands like pip install <whatever>  we need to activate the compiler.

Setup a command prompt

For all possible settings please have a look into the file at C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd. I used the following command to get a commandline with the right settings: C:\Windows\System32\cmd.exe /E:ON /V:ON /T:0E /K „C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd“ /RELEASE /x64 . You could create a new shortcut on the desktop, so that you do not have to type it when you want to install a new python modules.

Setup virtual environment

Since it is alwys recommended to use virtual environments for your project, let’s have a look how these can be setup to work with the C++ compiler. To do so we need to edit the activate.bat file, which on windows can be found under <venv folder>/Scripts and change it to look like the following snipplet.

set "PATH=%VIRTUAL_ENV%\Scripts;%PATH%"

"C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /RELEASE /x64	

:END

Now you just need to run the activate.bat for the virtual environment and everything is set up. If you use the powershell just edit the file activate.ps1

Install Psycopg2

When opening a command prompt using the above command we will get a windows looking like this

Python3 building psycopg2 cmd prompt

Now we have to set the Pathvariable so that pip will find pg_config.exe which is part of PostgreSQL.

set PATH=%PATH%;C:\Program Files\PostgreSQL\9.3\bin

Now we just need to type

pip install psycopg2

 Sources

http://matthew-brett.github.io/pydagogue/python_msvc.html
https://github.com/cython/cython/wiki/64BitCythonExtensionsOnWindows
http://msdn.microsoft.com/en-us/library/9yb4317s%28v=vs.100%29.aspx

3 Comment

  1. nat says:

    my install of the sdk on win 8.1 failed,and the message refers me to a nonexistent file on my machine for more information. i know that’s not much to go on, but do you have any helpful suggestions? thanks.

  2. Benjamin says:

    Sorry but without an error message or anything there is no way to help you. I never had the problem. Maybe asking @ http://www.stackexchange.com will turn up some helpful comments.

  3. Oliver says:

    YOU DID AN AMAZING JOB.

    THANKS A LOT !!!!!!

Schreibe einen Kommentar

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.