Question

I have python 2.7.6 and 3.4.0 on my machine. The 2.7 version is on my path. I would like to set up a virtualenv using 3.4. There are many postings on SO and elsewhere that suggest I do the following from a command prompt:

virtualenv -p c:\python34 myvirtualenv

but this does not work for me. The console session has administrator privilege and UAC is off, however I get a permissions problem:

F:\virtualenv>virtualenv -p c:\python34 myenv
Running virtualenv with interpreter c:\python34
Traceback (most recent call last):
  File "c:\python27\scripts\virtualenv-script.py", line 9, in <module>
    load_entry_point('virtualenv==1.11', 'console_scripts', 'virtualenv')()
  File "C:\Python27\lib\site-packages\virtualenv.py", line 779, in main
    popen = subprocess.Popen([interpreter, file] + sys.argv[1:], env=env)
  File "C:\Python27\lib\subprocess.py", line 709, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 957, in _execute_child
    startupinfo)
WindowsError: [Error 5] Access is denied

I have also tried it specifically pointing to the 3.4 version of virtualenv, but without changing the path it ends up executing a mixed bag of 2.7 and 3.4 python files.

The only way I could find to set up my virtual environment is to change my path to 3.4, run virtualenv then reset my path to 2.7, which defeats the point of the python switch on virtualenv.

Thanks

Was it helpful?

Solution

Better:

py -3.4 -m venv c:\path\to\wherever\you\want\it

If you don't have the py.exe launcher (but it should be installed) you can replace py -3.4 with c:\Python34\python.exe (assuming the default location)


This works because of the handy-dandy, Windows-versioningest, super nice runtime picker py.exe

By default, py.exe will be present on a Windows install (I think it comes with 2.7, I know it does with 3+). When you run py then it will search for some environment variables or you can override that with a specific verison number (in your case -2.7or -3.4) You can leave off the .4 and it will choose the "biggest" minor version number.

You can also use it to run Python scripts. If you put a hash-bang line at the top of your script #!python3 and call it py myscript.py then it will pick the correct version of Python to start with, by searching the first line of the script and searching for a version number.

This is cool, because you can put something like #!/usr/bin/env python3.4 in the top of your script and run it on Windows with py, or on linux by doing

$ chmod +x myscript.py
$ ./myscript.py

Pretty useful.

OTHER TIPS

Under Windows you need to run:

virtualenv -p c:\python34.exe myvirtualenv

The .exe on the end makes all the difference.

Had to play around with this for a while to get it right. Had Python2.7.9 installed (Windows 7), wanted to take the latest Python3 release for a spin. After installing Python3.4.3 I went to directory bar and created a virtual environment foo with this command:

virtualenv -p c:\Python34\python.exe foo

It took me quite some time to understand that I needed to install the Python3.4.3 interpreter in the 'normal' fashion, I originally thought it would be installed USING virtualenv. That was explained in this answer. I did not touch the pythonpath in Windows after installing Python3.4.3.

If above measures are not working, try this( using venv instead of virtualenv):

python -m venv venvname

(replace python with python.exe path if it is not listed in environment variable path settings)

Using GitBash on Windows, I had some trouble getting this to work as well.

I had Python 3.6 on the Windows path, but was trying to create a Python 2.7 virtual environment for testing an old project.

Eventually got it working via:

1. adding the C:\Python27 path to my Windows environment variables 
2. virtualenv -p c:/python27/python.exe venvname

(and before that I had to buggerize around adding the virtualenv module)

You can use virtualenv command like this

virtualenv --python=C:\python34\python.exe myvirtualenv

Example for Python 3.7

virtualenv --python=C:\Users\hp\AppData\Local\Programs\Python\Python37\python.exe myenv
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top