Setting Up Python and virtualenv on Windows with Cygwin


On Linux, a trivial task, but not so on Windows. Python, virtualenv and Cygwin

Published on by nick

Tags: django, virtualenv, cygwin, python

Admittedly, running Python and Django on Windows is less than fun. For the most part, I use Windows for many things, including developing PHP/HTML websites with Komodo IDE. But for running Apache and Python for web applications, particularly Django ones, I will always choose Linux. Lets face it, it's very easy to set up Python, virtualenv and Django on Ubuntu. I've done this exact set up so many times, I can darn near do it blind folded. The best part is that packages and dependencies are straight forward to configure.

In particular, virtualenv / virtualenvwrapper is a cinch on Linux but a pain on Windows. On Linux, it's as simple as following the instructions from my earlier post on the topic.

Unfortunately, setting up isolated Python environments is less than trivial on Windows, especially if you are running Cygwin as your terminal. Even if you run the DOS command line, setting up virtualenv is not so good. Sure, its possible, but it compared to the breeze that is Linux, its no walk in the park.

For this article, I'm going to write about the process with Cygwin. For other information without Cygwin, there are many writings out there. Just search Google. Basically, there are three steps in this process: install Python; install Cygwin; and, install and configure virtualenv.

The first thing you need to do is make sure you have a working version of Python. Changes are, you already have this requirement fulfilled. if not, basically choose a distribution and install. I like ActiveState's ActivePython, but python.org, obviously, is a great choice as well. Basically, on Windows, this is straight forward: download the EXE or MSI and launch and follow instructions. When finished, open up the command line cmd.exe and type in the command python --version. You should see some output like this:

C:\>python --version
Python 2.7.2

Next thing you need to do, if you haven't done so already, is install Cygwin. You can download the setup file from here. Run it and choose your options: vim, openssh, wget, gcc, etc... Generally, installing the defaults is fine. Go ahead and launch Cygwin and run some commands. In particular, try running Python. Take note here. You might notice some errors or even two different Python versions. When you run the command python --version in Cygwin you are probably actually running the symlinked file located at /usr/bin/python. This is important, because you actually want this symlink to point to the Python executable of your choice, eg, C:\Python27\python.exe. Also, be sure your PATH, PYTHONHOME, PYTHONPATH variables in Windows are set correctly.

In my exploration of this procedure, I experienced several errors, mainly that certain modules were missing. This was obviously because Python couldn't find what it needed and was confused between the two versions installed. The quick fix in this case was to set the symbolic link to the executable located at C:\Python27\python.exe. These commands should do the trick.

remove /usr/bin/python
ln -s /cygdrive/c/Python27/python.exe /usr/bin/python

Now when you run python --version, it will be what you expected.

Often, and in most cases, if you simply install Python with defaults, Windows will take care of everything for you, including establishing your path variables. One thing that made it easier for me was to work in the Windows command line for much of the set up and then test periodically in Cygwin. I found that if I tried to set up everything in Cygwin, it was more difficult.

Finally, we need to install and configure virtualenv. Lucky for you, if you've installed Python from the ActiveState website, you have it already. The only real trick here is to access without too much trouble. For me, I created a few batch files to handle the heavy lifting for me:

Make sure to save these to a directory in your path. virtualenv.bat

C:\Python27\Scripts\virtualenv.exe %1

activate.bat

C:\django\virtenvs\%1\Scripts\activate.bat

deactivate.bat

C:\django\virtenvs\%1\Scripts\deactivate.bat

Then, creating virtual environments is as easy as this (in Cygwin):

virtualenv.bat /path/to/virtenvs/envname

Or, on the Windows command line:

virtualenv.bat C:\path\to\virtenvs\envname

The only real caveat here is that in Cygwin, I have yet to ever see the confirmation that the virtual environment is activated. In Linux and in the DOS command line, I get the classic (envname) before the prompt. Th good news is that there are ways to confirm you are working n your virtual environment.

Basically, that's it. All in all, I actual have grown to prefer working in the Windows command line because I get fewer errors and I see the parenthetical confirmation I'm actually working in my virtual environment.

Some final words are in order here. I struggled with MySQLdb in this current configuration. For some reason, my default GCC compiler wasn't doing the job so I fiddled around with MinGW with no luck. I uninstalled and reinstalled Python 32-bit and 64-bit a few times also to no avail. Since I was using ActiveState's Python, I thought I could also try PyPM (Python Package Manager). This seemed promising, but in the end, I still get errors relating to the darn batch file, vcvarsall.bat. This file was the original reason I tried an alternate compiler in the first place. Ultimately, I retraced my steps, started fresh and was very careful to to do everything in the correct order. I should note that you should definitely create your virtual environments with the --no-site-packages options.

Good luck...

Comments

Comments powered by Disqus

 Blog Search

  Popular Tags

django, ubuntu, mod_wsgi, apache, authentication, python, tls, linux, forms, ssl, virtualenv, dell, uwsgi, bash, nginx, raid, customer-service, centurylink, ux, software-companies, rais, form, centos, password, certificates, tinymce, mdadm, dual-boot, file-server, virtualhost, gluster, IT, blog, get, networking, piplight, distributed-file-system, big companies, bitnami, cygwin, windows, samba, scripting, pygments, post, programming-language, ui, lampstack, outsourcing, isp, security, usabillity, provision, php, shared-hosting, netflix, git, flatpages, syntax-highlighting, virtualbox, hg, redirect, usability, prg, acls, change-password, complex, view tags...

 Questions/Comments?

Drop me a line... [email protected]
Follow me on Twitter... @nicorellius
Share on Facebook...