2

I have multiple versions of Python. I need to create a virtualenv for my project and make sure that it's using Python 2.7.

I've tried to accomplish this with the combination of this guide for virtualenv on Windows and this SO post on virtualenv with a specific kind of Python.

Unfortunately it's not working, which is probably because the latter resource was written by someone using Linux.

Here's what I did:

C:\Python27\Scripts>pip install virtualenv You are using pip version
6.0.6, however version 9.0.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command. Collecting virtualenv   Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
    100% |################################| 1.8MB 3.7MB/s eta 0:00:01 Installing collected packages: virtualenv

Successfully installed virtualenv-15.1.0

C:\Python27\Scripts>pip install virtualenvwrapper-win You are using pip version 6.0.6, however version 9.0.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command. Collecting virtualenvwrapper-win   Downloading virtualenvwrapper-win-1.2.1.zip Requirement already satisfied (use
--upgrade to upgrade): virtualenv in c:\python27\lib\site-packages (from virtualenvwrapper
-win) Installing collected packages: virtualenvwrapper-win   Running setup.py install for virtualenvwrapper-win Successfully installed virtualenvwrapper-win-1.2.1

C:\Python27\Scripts>mkvirtualenv c:\users\hackr\Desktop\P27_D19  --python=C:\Python27\python.exe
'python.exe' is not recognized as an internal or external command, operable program or batch file.

'virtualenv.exe' is not recognized as an internal or external command, operable program or batch file.

Update: I just got this to work using plain virtualenv instead of the recommended mkvirtualenv command, which I assume is part of the wrapper they had me install. If someone would like to help me understand what happened and if there are any downfalls of not using the wrapper, that would be a good way to write up the answer.

Hack-R
  • 19,705
  • 11
  • 63
  • 110

1 Answers1

1

If having multiple Python versions installed, it's best to always specify full paths (to be sure) and not rely on environment variables (at least it's how I do it):

  • pip install virtualenv: I do it like : "C:\Install\x64\Python\3.5.3\python.exe" -m pip install virtualenv (don't mind the paths) to control the Python installation (same thing for virtualenvwrapper).

I don't have a mkvirtualenv in my "%PYTHON_INSTALLATION_DIR%\Scripts" (e.g. "C:\Install\x64\Python\3.5.3\Scripts") folder, but (this is an example that I "crafted" now):

  • "c:\Install\x64\Python\3.5.3\Scripts\virtualenv.exe" -p "c:\Install\x64\Python\2.7.13\python.exe" "c:\venvs\py2713"

From then on:

  • "c:\venvs\py2713\Scripts\activate.bat"
  • python ......

I know, it's kind of annoying to specify all those funky paths (on Lnx is soooo much easier), but at least it's safe. Anyway, after setting up the virtual environments, you can adjust your environment (%PATH%), so you don't have to specify full paths.

I noticed your comment while writing, and I must say that PyCharm (Professional Edition) is an excellent tool to work with Django (as a matter of fact with Python in general). If on the other hand, you go for Community Edition it's not quite so great (as expected). [SO]: Run/Debug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition? is one of the painful issues that I had to deal with.

@EDIT0:

Just now I installed virtualenvwrapper-win: "C:\Install\x64\Python\3.5.3\python.exe" -m pip install virtualenvwrapper-win. Running mkvirtualenv, either by its full path or by adding its parent folder in %PATH% (mkvirtualenv a), yielded the same error:

'python.exe' is not recognized as an internal or external command, operable program or batch file.

but it created the VEnv (under %USERPROFILE%\Envs). So the error is benign. Anyway it can be fixed by either:

  • Setting %PYTHONHOME%
  • Adding the path to python.exe in %PATH%

An additional step that I did, was setting %WORKON_HOME% to the folder where I want my VEnvs to be located. Note that the environment variables must be persisted (since I only did this for testing purposes I only set them in the cmd console that I used for this task), otherwise they'll have to be set every time you need to use these tools.

After that workon, lsvirtualenv worked like a charm (well, not as great as in Lnx, but close enough).

Note (about mkvirtualenv v1.2.1): It will use the default python.exe (see above), as a base for the new VEnv, it's not as flexible as virtualenv.exe (which accepts the -p/--python argument).

CristiFati
  • 28,721
  • 9
  • 41
  • 63
  • I think what you wrote would work and I am upvoting your answer. Please check the update that I just appended to my question as an edit. I got it to work about 10 minutes ago, but I'm not sure why the wrapper recommended by the tutorial didn't work or if I am causing future problems by not using it. If you have any knowledge on this please add it to your answer and I'll accept it as the solution.Yes, I agree it's a fine IDE, but an IDE doesn't really have anything to do with the question/answer IMHO (and even if it did, being forced into using some specific IDE would be restrictive). – Hack-R May 24 '17 at 18:22
  • Not using _virtualenvwrapper_, should definitely have no negative impact on the future since it's a simple wrapper over _virtualenv_. I remember from the _Lnx_ + _Django_ "era" the `workon` feature which was truly awesome ("navigating" over _VEnv_ names from the commandline using _TAB_ as if they were files/folders). However, I couldn't do the same on _Win_ (I also must say that I didn't try very hard). – CristiFati May 24 '17 at 18:29
  • Many thanks; I have marked your answer as the solution – Hack-R May 24 '17 at 18:39
  • `mkvirtualenv` on `virtualenvwrapper-win` now also takes `-p` parameters to pass the python executable (all the other virtualenv flags are also available). – thebjorn Nov 18 '17 at 01:21
  • @thebjorn Yes, that's true, I am using it in one of the commands from the answer. – CristiFati Nov 18 '17 at 03:16
  • I'm just saying that your final note is no longer accurate (mkvirtualenv now passes "all" flags onto virtualenv, including `-p`/`--python`). – thebjorn Nov 18 '17 at 20:38
  • @thebjorn: Ah, thank you, I added the version in the note. – CristiFati Nov 19 '17 at 23:35