How to install psycopg2 on PTVS?

Jul 15, 2014 at 10:53 PM
psycopg2 is a PostgreSQL driver for Python. If I try to install it with pip or easy_install it downloads the source code and tries to compile it which fails every time on windows. For that it is the best to use the binaries. However, when I run the MSI and install the driver to the global python interpreter/environment, my PTVS environment doesn't recognize I installed that, because of a virtual environment, off course.

How can I add psycopg2 to my virtual environment in Visual Studio?
Coordinator
Jul 15, 2014 at 11:00 PM
Edited Jul 15, 2014 at 11:01 PM
At the page for the binaries that you've linked to, there is a section titled "Virtual Environment Installation Notes" - follow the instructions (Method 1) there. Note that you can use PTVS UI for package installation in the same manner as command line - just select easy_install as installation method, and use a full path to the .exe as package name; we will pass it along to easy_install as is.
Marked as answer by boskicthebrain on 7/15/2014 at 4:51 PM
Jul 15, 2014 at 11:17 PM
Well, I found a workaround, but I would still like to solve this properly.

The workaround would be to install everything I need in the virtual environment, and in the end to edit the pyvenv.cfg so that the line:
include-system-site-packages = false
is changed to:
include-system-site-packages = true
This way all global packages also fit into our local environment. Not pretty, but works...
Coordinator
Jul 15, 2014 at 11:35 PM
I'm not sure I follow...

If you have include-system-site-packages = false (which is the default) in your pyvenv.cfg, then you're requesting isolation of your environment from the global one - so any globally installed modules should not be visible in your environment (and hence PTVS behavior is expected). In this case, if you want the package, then you should install it into the environment.

OTOH, if you do want to see global packages, then you should just set include-system-site-packages = true. PTVS should respect that setting.

Also note that we respect the presence of the no-global-site-packages.txt file (this is regardless of pyvenv.cfg setting)
Jul 15, 2014 at 11:52 PM
I tried the easy_install approach and it worked! (although it failed once, and then succeeded). Thanks!
Mar 4 at 4:10 AM
Hi guys,

I am also trying to use psycopg2 in PTVS but I am quite a newbie and I cannot understand the detailed steps to make it work.
Could someone help me out please?

Thanks heaps
Coordinator
Mar 5 at 5:39 AM
Have a look at this video, it explains the process in a step by step fashion:
http://youtu.be/eKPeC1remt4?t=3m2s

Follow that, and when you get to the dialog where you need to specify the package name and install method, put the full path to the downloaded psycopg2-2.6.0.win32-py2.7-pg9.4.1-release.exe as package name, and flip the method to "easy_install".
Mar 5 at 6:10 AM
I am getting the following error while following these steps (and selecting easy_install):

Any idea what I am doing wrong please?

Installing 'pip' package manager.
Downloading setuptools from https://go.microsoft.com/fwlink/?LinkId=317603
Traceback (most recent call last):
File "C:\Program Files (x86)\IronPython 2.7.1\Lib\urllib.py", line 205, in open
File "C:\Program Files (x86)\IronPython 2.7.1\Lib\urllib.py", line 449, in open_https
File "C:\Program Files (x86)\IronPython 2.7.1\Lib\urllib.py", line 369, in http_error
File "C:\Program Files (x86)\IronPython 2.7.1\Lib\urllib.py", line 631, in http_error_302
File "C:\Program Files (x86)\IronPython 2.7.1\Lib\urllib.py", line 659, in redirect_internal
File "C:\Program Files (x86)\IronPython 2.7.1\Lib\urllib.py", line 205, in open
File "C:\Program Files (x86)\IronPython 2.7.1\Lib\urllib.py", line 435, in open_https
File "C:\Program Files (x86)\IronPython 2.7.1\Lib\httplib.py", line 951, in endheaders
File "C:\Program Files (x86)\IronPython 2.7.1\Lib\httplib.py", line 811, in _send_output
File "C:\Program Files (x86)\IronPython 2.7.1\Lib\httplib.py", line 1158, in connect
File "C:\Program Files (x86)\IronPython 2.7.1\Lib\ssl.py", line 366, in wrap_socket
File "C:\Program Files (x86)\IronPython 2.7.1\Lib\ssl.py", line 133, in init
File "C:\Program Files (x86)\IronPython 2.7.1\Lib\ssl.py", line 295, in do_handshake
File "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\WDExpressExtensions\Microsoft\Python Tools for Visual Studio\2.1\pip_downloader.py", line 47, in <module>
File "C:\Program Files (x86)\IronPython 2.7.1\Lib\urllib.py", line 91, in urlretrieve
File "C:\Program Files (x86)\IronPython 2.7.1\Lib\urllib.py", line 237, in retrieve
File "C:\Program Files (x86)\IronPython 2.7.1\Lib\httplib.py", line 773, in send
IOError: System.IO.IOException: Authentication failed because the remote party has closed the transport stream.
at Microsoft.Scripting.Runtime.LightExceptions.CheckAndThrow(Object value)
at Microsoft.Scripting.Interpreter.FuncCallInstruction2.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.HandleException(InterpretedFrame frame, Exception exception)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
at IronPython.Compiler.PythonCallTargets.OriginalCallTarget3(PythonFunction function, Object arg0, Object arg1, Object arg2)
at IronPython.Runtime.PythonFunction.FunctionCaller
3.Call3(CallSite site, CodeContext context, Object func, T0 arg0, T1 arg1, T2 arg2)
at IronPython.Runtime.Method.MethodBinding2.SelfTarget(CallSite site, CodeContext context, Object target, T0 arg0, T1 arg1)
at System.Dynamic.UpdateDelegates.UpdateAndExecute4[T0,T1,T2,T3,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
at Microsoft.Scripting.Interpreter.FuncCallInstruction
7.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run5[T0,T1,T2,T3,T4,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
at IronPython.Compiler.Ast.CallExpression.Invoke2Instruction.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run6[T0,T1,T2,T3,T4,T5,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
at IronPython.Compiler.PythonCallTargets.OriginalCallTarget5(PythonFunction function, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4)
at IronPython.Runtime.PythonFunction.FunctionCaller5.Call5(CallSite site, CodeContext context, Object func, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
at System.Dynamic.UpdateDelegates.UpdateAndExecute7[T0,T1,T2,T3,T4,T5,T6,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
at IronPython.Runtime.Method.MethodBinding
4.SelfTarget(CallSite site, CodeContext context, Object target, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
at System.Dynamic.UpdateDelegates.UpdateAndExecute6[T0,T1,T2,T3,T4,T5,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
at IronPython.Runtime.Types.PythonType.FastTypeSite4.CallTarget(CallSite site, CodeContext context, Object type, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
at IronPython.Compiler.Ast.CallExpression.Invoke4Instruction.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run5[T0,T1,T2,T3,T4,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
at IronPython.Compiler.PythonCallTargets.OriginalCallTarget4(PythonFunction function, Object arg0, Object arg1, Object arg2, Object arg3)
at IronPython.Runtime.PythonFunction.FunctionCaller
2.Default2Call2(CallSite site, CodeContext context, Object func, T0 arg0, T1 arg1)
at System.Dynamic.UpdateDelegates.UpdateAndExecute4[T0,T1,T2,T3,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`7.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run5[T0,T1,T2,T3,T4,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
at IronPython.Compiler.Ast.CallExpression.Invoke2Instruction.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
at IronPython.Hosting.PythonCommandLine.RunFileWorker(String fileName)
at IronPython.Hosting.PythonCommandLine.RunFile(String fileName)
'pip' failed to install. Exit code: 1
Installing 'C:\Users\jeremie\Downloads\psycopg2-2.6.0.win32-py2.7-pg9.4.1-release.exe'
Coordinator
Mar 5 at 6:15 AM
there are multiple interpreters available for python.

You need to install CPython. Please see the installation page.

If you really definitely need IronPython (.net version of python), then you cant install various pkgs including this.
Mar 5 at 7:06 AM
Does that mean you cannot use psycopg2 with IronPython then?
Coordinator
Mar 5 at 7:34 AM
The problem that you're getting is due to trying to install it with pip (as opposed to easy_install). I'm not sure whether easy_install would work better, though. But even if it did, the package itself won't work, because it includes native modules (written in C). IronPython does not support them, you need to use CPython for that.
Mar 6 at 9:34 AM
I wasted a lot of time trying to install psycopg2 directly. Eventually I gave up and found binaries and installed it globally:
http://www.stickpeople.com/projects/python/win-psycopg/

choose your weapon and kill the problem :)

Didn't have any such problems on Linux.
Coordinator
Mar 6 at 9:46 AM
Didn't easy_install work for you to install it into a virtual env?