With virtualenv I’ve found getting some packages to work with Windows have been very difficult to install.
$ pip install covers a wide range of items in windows, a few it requires special attention I need a lot are MySQLdb, PIL, and PILLOW. So here is the best way to use virtualenv to install windows binaries.
Most often I get a
vcvarsall.bat compiler error, which possibly has something to do with Microsoft Visual C++ compiler. I tried working around this with MingGW and also toggling VisualStudio Express compilers. I didn’t get it right.
After a week of searching through Google someone presented the best answer. The problem is that I everytime I install a Python package through a binary it’s applied to the global installation, not the virtualenv copy.
So what happens is we activate the Python script to do a temporary registry adjustment and let our installs go to the virtual environment we are actively in.
- Go to your virtual environment
- Activate it so you are using that copy of Python:
$ source /env/Scripts/activate
- Copy the script below into your project directory, save as the_python.py
- Download any binaries from http://www.lfd.uci.edu/~gohlke/pythonlibs/
- Run the Registry activator:
$ python the_python.py
- Now just install the windows binaries as you usually would in the Windows GUI.
# script to register Python 2.0 or later for use with win32all # and other extensions that require Python registry settings # # Adapted by Ned Batchelder from a script # written by Joakim Low for Secret Labs AB / PythonWare # # source: # http://www.pythonware.com/products/works/articles/regpy20.htm import sys from _winreg import * # tweak as necessary version = sys.version[:3] installpath = sys.prefix regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version) installkey = "InstallPath" pythonkey = "PythonPath" pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % ( installpath, installpath, installpath ) def RegisterPy(): try: reg = OpenKey(HKEY_LOCAL_MACHINE, regpath) except EnvironmentError: try: reg = CreateKey(HKEY_LOCAL_MACHINE, regpath) except Exception, e: print "*** Unable to register: %s" % e return SetValue(reg, installkey, REG_SZ, installpath) SetValue(reg, pythonkey, REG_SZ, pythonpath) CloseKey(reg) print "--- Python %s at %s is now registered!" % (version, installpath) if __name__ == "__main__": RegisterPy()
You will still want to keep track of your project dependencies in a
requirements.txt file for when you deploy your application on a Linux server. This solution just makes local development a lot more pleasant.