Tuesday, January 28, 2014

virtualenv on Windows

A little while ago I suggested to someone using Windows to implement
virtualenv "later". So I thought I'd document my experience here.

In summary, it probably works well if you have Visual Studio. The
process outlined below shows a clumsy workaround if you don't.

Hope it helps someone

Mike

- - - - - -

$pip install virtualenv # first step (requires pip obviously)

$mkdir c:\users\miked\env

$virtualenv --no-site-packages c:\users\miked\env\proj-x
New python executable in c:\users\miked\env\proj-x\Scripts\python.exe
Installing setuptools, pip...done.

Now copy your "myproject-x" into c:\users\miked\env\proj-x\myproject-x

At this point on Linux one would install virtualenv-wrapper but that is
unfortunately not available for Windows so a simple workaround is ...

1. mkdir \users\miked\bin # could be anything eg \users\miked\bat

2. Add C:\users\miked\bin to the path environment variable

3. Create a batch file called C:\users\miked\bin\proj-x.bat containing:

cd \users\miked\env\proj-x\myproject-x
..\Scripts\activate

4. Create another batch file
C:\users\miked\env\proj-x\myproject-x\exit.bat containing:

cd \users\miked\
\users\miked\env\proj-x\Scripts\deactivate

5. Test the workaround by opening a command prompt somewhere and
entering proj-x. You should see:

C:\Documents and Settings\Mike Dewhirst\Desktop>cd
\users\miked\env\proj-x\myproject-x

C:\users\miked\env\proj-x\myproject-x>..\Scripts\activate
(proj-x) C:\users\miked\env\proj-x\myproject-x>

6. Test the exit batch file by entering exit at that command prompt:

C:\users\miked\env\proj-x\myproject-x>..\Scripts\activate
(proj-x) C:\users\miked\env\proj-x\myproject-x>exit

(proj-x) C:\users\miked\env\proj-x\myproject-x>cd \users\miked\

(proj-x) C:\users\miked>\users\miked\env\proj-x\Scripts\deactivate
C:\users\miked>

This should let you get into and out of the virtualenv quite easily.

Now comes the difficult bit for Windows - installing stuff in the new
Python's site-packages. First, though, discover what you already have
installed in your main site-packages:

pip freeze > requirements.txt # lists existing site-packages

Here is mine, most of which I don't want in proj-x

Django==1.5.4
Jinja2==2.7.1
MarkupSafe==0.18
Pygments==1.6
South==0.7.6
Sphinx==1.1.3
coverage==3.7
django-discover-runner==1.0
docutils==0.9.1
filemov==1.1.1-2728-py2.7
pillow==2.3.0
pss==1.38
psycopg2==2.4.5
py2exe==0.6.9
pytz==2012h
pywin32==218
virtualenv==1.11.2

So I edit it down to the following, create a directory and save it as
C:\users\miked\env\proj-x\requirements\requirements.txt

Django==1.5.4
South==0.7.6
coverage==3.7
django-discover-runner==1.0
pillow==2.3.0
psycopg2==2.4.5
pytz==2013.9

pip install -r C:\users\miked\env\proj-x\requirements\requirements.txt

And this where (my) problems start. First pip will download the packages
successfully but I don't have Visual Studio installed so those packages
requiring compilation don't get installed. Here is a snippet of pip
output after the downloads:

Installing collected packages: Django, South, coverage,
django-discover-runner, pillow, psycopg2, pytz
Running setup.py install for Django

... and so on until errors start to appear. In my case the signal is:

error: Unable to find vcvarsall.bat

Afterwards, pip freeze shows:

(proj-x) C:\users\miked\env\proj-x>pip freeze
Django==1.5.4
South==0.7.6
coverage==3.7
django-discover-runner==1.0

... which indicates pillow, psycopg2 and pytz did not install.

But how come they were installed in the main site-packages? That's
because they were originally downloaded from
http://www.lfd.uci.edu/~gohlke/pythonlibs/ and installed manually.

However, even after downloading from that site they cannot be installed
manually into a virtualenv! Only pip can do that. The only workaround I
have found is to manually copy them from the main site-packages to the
new virtualenv site-packages. Clumsy.

My proj-x requirements.txt file now looks like this ...

Django==1.5.4
South==0.7.6
coverage==3.7
django-discover-runner==1.0
# pillow==2.3.0 http://www.lfd.uci.edu/~gohlke/pythonlibs/
# psycopg2==2.4.5 http://www.lfd.uci.edu/~gohlke/pythonlibs/
# pytz==2013.9 http://www.lfd.uci.edu/~gohlke/pythonlibs/

After the above clumsy workaround everything works well and pip freeze
displays ...

Django==1.5.4
South==0.7.6
coverage==3.7
django-discover-runner==1.0
pillow==2.3.0
psycopg2==2.4.5
pytz==2013.9



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/52E86DE2.5010907%40dewhirst.com.au.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment