I'm using VirtualEnv on Windows XP. I'm wondering if I have my brain wrapped around it correctly:
I ran virtualenv ENV
and it created C:\WINDOWS\system32\ENV
. I then changed my PATH
variable to include C:\WINDOWS\system32\ENV\Scripts
instead of C:\Python27\Scripts
. Then, I checked out Django into C:\WINDOWS\system32\ENV\Lib\site-packages\django-trunk
, updated my PYTHON_PATH
variable to point the new Django directory, and continued to easy_install
other things (which of course go into my new C:\WINDOWS\system32\ENV\Lib\site-packages
directory).
I understand why I should use VirtualEnv so I can run multiple versions of Django, and other libraries on the same machine, but does this mean that to switch between environments I have to basically change my PATH
and PYTHON_PATH
variable? So, I go from developing one Django project which uses Django 1.2 in an environment called ENV
and then change my PATH
and such so that I can use an environment called ENV2
which has the dev version of Django?
Is that basically it, or is there some better way to automatically do all this (I could update my path in Python code, but that would require me to write machine-specific code in my application)?
Also, how does this process compare to using VirtualEnv on Linux (I'm quite the beginner at Linux).
Check the $VIRTUAL_ENV environment variable. The $VIRTUAL_ENV environment variable contains the virtual environment's directory when in an active virtual environment. Once you run deactivate / leave the virtual environment, the $VIRTUAL_ENV variable will be cleared/empty.
virtualenv is used to manage Python packages for different projects. Using virtualenv allows you to avoid installing Python packages globally which could break system tools or other projects.
A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system.
Normally virtualenv
creates environments in the current directory. Unless you're intending to create virtual environments in C:\Windows\system32
for some reason, I would use a different directory for environments.
You shouldn't need to mess with paths: use the activate
script (in <env>\Scripts
) to ensure that the Python executable and path are environment-specific. Once you've done this, the command prompt changes to indicate the environment. You can then just invoke easy_install and whatever you install this way will be installed into this environment. Use deactivate
to set everything back to how it was before activation.
Example:
c:\Temp>virtualenv myenv New python executable in myenv\Scripts\python.exe Installing setuptools..................done. c:\Temp>myenv\Scripts\activate (myenv) C:\Temp>deactivate C:\Temp>
Notice how I didn't need to specify a path for deactivate
- activate
does that for you, so that when activated "Python" will run the Python in the virtualenv, not your system Python. (Try it - do an import sys; sys.prefix
and it should print the root of your environment.)
You can just activate a new environment to switch between environments/projects, but you'll need to specify the whole path for activate
so it knows which environment to activate. You shouldn't ever need to mess with PATH or PYTHONPATH explicitly.
If you use Windows Powershell then you can take advantage of a wrapper. On Linux, the virtualenvwrapper
(the link points to a port of this to Powershell) makes life with virtualenv
even easier.
Update: Not incorrect, exactly, but perhaps not quite in the spirit of virtualenv
. You could take a different tack: for example, if you install Django and anything else you need for your site in your virtualenv, then you could work in your project directory (where you're developing your site) with the virtualenv activated. Because it was activated, your Python would find Django and anything else you'd easy_installed into the virtual environment: and because you're working in your project directory, your project files would be visible to Python, too.
Further update: You should be able to use pip
, distribute
instead of setuptools
, and just plain python setup.py install
with virtualenv
. Just ensure you've activated an environment before installing something into it.
Yes basically this is what virtualenv do , and this is what the activate
command is for, from the doc here:
activate script
In a newly created virtualenv there will be a bin/activate shell script, or a Scripts/activate.bat batch file on Windows.
This will change your $PATH to point to the virtualenv bin/ directory. Unlike workingenv, this is all it does; it's a convenience. But if you use the complete path like /path/to/env/bin/python script.py you do not need to activate the environment first. You have to use source because it changes the environment in-place. After activating an environment you can use the function deactivate to undo the changes.
The activate script will also modify your shell prompt to indicate which environment is currently active.
so you should just use activate
command which will do all that for you:
> \path\to\env\bin\activate.bat
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With