7

I've looked around on SO, and the answers I have found to my problem haven't allowed me to solve it yet.

I want to use isolated virtualenv environments, but for one reason or another, virtualenv keeps loading global site packages, when in django's shell...

I tried to clean up PATH variables, until only c:\Python26\Scripts and c:\Python26 remain. I then create my environment.

virtualenv --distribute --no-site-packages myproject

I then activate the virtualenv. PATH is now (irrelevant vars scrapped):

PATH=E:\Development\django_projects\myproject\Scripts;C:\Panda3D-1.7.0\python;C:\Panda3D-1.7.0\bin;c:\python26\Scripts;

PYTHONPATH=C:\Panda3D-1.7.0\

So far, so good. I launch python...

>>> import django
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ImportError: No module named django

Let's just try a module I'm sure is in my c:\python site-packages directory.

>>> import BeautifulSoup
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named BeautifulSoup

Yay! No global site packages! On to the next one then. From the command prompt, I type:

django-admin.py

And it works! But wait... I haven't installed Django yet. How is this possible?

After this, it gets even weirder... I first add these to virtualenv's activate.bat script so that Django can find my settings.

set PYTHONPATH=E:\Development\django_projects\myproject\
set DJANGO_SETTINGS_MODULE=settings.development

Now I launch django-admin.py shell and

In [1]: import BeautifulSoup
In [2]: BeautifulSoup.__file__
Out[2]: 'C:\\Python26\\lib\\site-packages\\BeautifulSoup.pyc'

How is this even possible?

Flash of insight

While typing this, I suddenly get it. .py is a file extension coupled with my c:\python26\python.exe executable, instead of the virtualenv one!

python manage.py
Traceback (most recent call last):
  File "manage.py", line 2, in <module>
    from django.core.management import execute_manager
ImportError: No module named django.core.management

Heh. Anyone has any idea of how to couple the .py file extension to my virtualenv's python executable instead of the system defined python executable?

Mathieu Dhondt
  • 8,405
  • 5
  • 37
  • 58
  • *how to couple the .py file extension to my virtualenv's python executable* See [Why doesn't virtualenv on Windows associate .py/.pyw/.pyo/.pyc files with virtualenv's version of Python executables?](http://stackoverflow.com/q/4879624/95735) and [Temporary file association for single cmd.exe session](http://stackoverflow.com/q/5583024/95735) – Piotr Dobrogost May 24 '12 at 17:33

3 Answers3

11

A little bit of extra .bat hackery can easily fix this. My standard additions to activate.bat are:

REM custom venv settings
set PYTHONPATH=%\VIRTUAL_ENV%;%\VIRTUAL_ENV%\conf;%\VIRTUAL_ENV%\apps
set DJANGO_SETTINGS_MODULE=settings

ftype Python.File=%VIRTUAL_ENV%\Scripts\python.exe %1 %*

and to deactivate.bat

REM restore ftype
ftype Python.File=C:\tools\Python27\python.exe %1 %*
m0nonoke
  • 200
  • 2
  • 9
  • Are there some extra \ characters in the following line "set PYTHONPATH=%\VIRTUAL_ENV%;%\VIRTUAL_ENV%\conf;%\VIRTUAL_ENV%\apps"? Shouldn't it read: "set PYTHONPATH=%VIRTUAL_ENV%;%VIRTUAL_ENV%\conf;%VIRTUAL_ENV%\apps"? – Nathan Aug 27 '12 at 15:15
  • 1
    Additionally... In Windows batch files, you'll need to escape the % characters for the ftype commands. "ftype Python.File=C:\tools\Python27\python.exe %%1 %%*" – Nathan Aug 27 '12 at 15:39
  • When I run the ftype line, I get an error "Access is denied. Error occurred while processing: Python.File." – Dan Benamy Nov 02 '12 at 04:41
0

I had the same "Access denied" problems as Dan with m0nonoke's answer on my Windows 7 setup using cmd.exe.

But I found this work around using a replacement shell TCC/LE and a customised startup file...

Under working directory create subdirectory config. In this directory create startup file for TCC/LE called tcstart.btm

@echo off
rem Override system python binding to handle virtualenvironments
set .py;.pyc=python.exe

Now create (copy) TCC/LE shortcut on desktop and rename it appropriately. Open Properties for shortcut and add to Target “C:\django\config\tcstart.btm”. You probably want to set Start in to something useful, like C:\django

Solution found in this guide on installing Django and Windows.

Neil_Alex
  • 11
  • 1
0

You could make a .bat file and modify PATH and PYTHONPATH in there, and then run .py from that .bat file. Something like this i think

set PATH=C:\Python26; python myfile.py

Ofcourse, add anything else to your path that you want.

grizwako
  • 1,563
  • 4
  • 19
  • 23