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).
This question is related to
python
windows
linux
virtualenv
in my project wsgi.py file i have this code (it works with virtualenv,django,apache2 in windows and python 3.4)
import os
import sys
DJANGO_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)),'..')
sys.path.append(DJANGO_PATH)
sys.path.append('c:/myproject/env/Scripts')
sys.path.append('c:/myproject/env/Lib/site-packages')
activate_this = 'c:/myproject/env/scripts/activate_this.py'
exec(open(activate_this).read())
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
application = get_wsgi_application()
in virtualhost file conf i have
<VirtualHost *:80>
ServerName mysite
WSGIScriptAlias / c:/myproject/myproject/myproject/wsgi.py
DocumentRoot c:/myproject/myproject/
<Directory "c:/myproject/myproject/myproject/">
Options +Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
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
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
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.
Source: Stackoverflow.com