Python virtualenv questions
Solution 1
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.
Solution 2
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
Solution 3
on Windows I have python 3.7 installed and I still couldn't activate virtualenv
from Gitbash with ./Scripts/activate
although it worked from Powershell after running Set-ExecutionPolicy Unrestricted
in Powershell and changing the setting to "Yes To All".
I don't like Powershell and I like to use Gitbash, so to activate virtualenv
in Gitbash first navigate to your project folder, use ls
to list the contents of the folder and be sure you see "Scripts". Change directory to "Scripts" using cd Scripts
, once you're in the "Scripts" path use . activate
to activate virtualenv
. Don't forget the space after the dot.
Solution 4
After creating virtual environment copy the activate.bat file from Script folder of python and paste to it your environment and open cmd from your virtual environment and run activate.bat file.enter image description here
orokusaki
I'm Michael Angeletti. I am a Python / Django developer, specializing in SaaS applications.
Updated on July 08, 2022Comments
-
orokusaki almost 2 years
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 createdC:\WINDOWS\system32\ENV
. I then changed myPATH
variable to includeC:\WINDOWS\system32\ENV\Scripts
instead ofC:\Python27\Scripts
. Then, I checked out Django intoC:\WINDOWS\system32\ENV\Lib\site-packages\django-trunk
, updated myPYTHON_PATH
variable to point the new Django directory, and continued toeasy_install
other things (which of course go into my newC:\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
andPYTHON_PATH
variable? So, I go from developing one Django project which uses Django 1.2 in an environment calledENV
and then change myPATH
and such so that I can use an environment calledENV2
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).