Abhimanyu Pathania
Abhimanyu Pathania

Reputation: 354

Create separate virtualenv for python27 and python35 on windows

I use python27 but want to port a script to python3. To test it on same machine, I installed Python 3.5.1(64 bit). During setup I didn't add it to PATH(because I want Python27 to be default for programs like appengine). Still, checked the 'for all users' option. I also changed the installation dir to 'C:\Python35' (default was C:\Python35\Program Files). After install I tested it with windows python launcher by typing (in cmd prompt ):

py -3

and it worked fine. Then tried to create a python3 virtualenv (I am using virtualenvwrapper-win):

mkvirtualenv --python=C:\Python35\python.exe py35env

but it fails and I get a dialog box saying:

**python.exe - System Error**

The program can't start because VCRUNTIME140.dll is missing from your computer.
Try reinstalling program to fix this problem.

and the command prompt logs:

Running virtualenv with interpreter C:\Python35\python.exe
Using base prefix 'C:\\Python35'
New python executable in C:\Users\Abhimanyu\Envs\py35env\Scripts\python.exe
ERROR: The executable C:\Users\Abhimanyu\Envs\py35env\Scripts\python.exe is not functioning
ERROR: It thinks sys.prefix is 'c:\\users\\abhimanyu\\envs' (should be 'c:\\users\\abhimanyu\\envs\\py35env')
ERROR: virtualenv is not compatible with this system or executable
Note: some Windows users have reported this error when they installed Python for "Only this user" or have multiple versions of Python instal
led. Copying the appropriate PythonXX.dll to the virtualenv Scripts/ directory may fix this problem.

The normal virtualenv cmd fails with same errors as well:

 virtualenv -p C:\Python35\python.exe py35env

virtualenv is installed on Python2.7.11 - 64bit (Python3.5 was installed after Python2.7). Versions of packages on python27 are:

pip (8.0.2)
setuptools (18.2)
virtualenv (14.0.6)
virtualenvwrapper-win (1.2.1)

My PATH variables are:

**System**
C:\Python27\;C:\Python27\Scripts;C:\Program Files\Broadcom\Broadcom 802.11;;;c:\program files\graphicsmagick-1.3.23-q16;C:\Program Files\ImageMagick-6.9.3-Q16;;;;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\nodejs\;C:\Program Files\Common Files\Autodesk Shared\

**User**
C:\Program Files (x86)\Google\google_appengine\;C:\Users\Abhimanyu\AppData\Roaming\npm

The default folder for virtualenvwrapper is:

C:\Users\Abhimanyu\Envs

I am on Windows7 64bit system.

Can someone please explain what might be wrong or which dlls, as logged in cmd prompt, are supposed to copied in which dir?

Upvotes: 1

Views: 5650

Answers (1)

ixe013
ixe013

Reputation: 10191

There might have been some bugs in previous versions of Python... Here is how I made a Windows side-by-side installation of:

  • Python 2.7.16
  • Python 3.7.3

All without admin rights!

Python 3 and virtualenv

Start by downloading and installing Python 3.7.3. Clear the options to install for all users if you don't have admin rights.

After installation, open a CMD prompt and type

py -3 -m pip install virtualenv
py -3 -m virtualenv venv3

Install Python 3 specific packages to it, for example:

venv3\Scripts\activate.bat
pip install asyncssh
REM The following will NOT throw an error
python -c "import asyncssh"
venv3\Scripts\deactivate.bat

That's it for Python 3

Python 2 and virtualenv

You might be able to skip this part if you have admin rights, lucky you. If not, read along.

Install Python 2.7.16 without admin rights

For Python 2.7.16, open a CMD window and disable the Elevation prompt with:

set __COMPAT_LAYER=RunAsInvoker
msiexec /a python-2.7.16.msi TARGETDIR=c:\dev\my-stuff\Python27-32 ADDLOCAL=Tools /qb+

That will install Python 2.7.16, but Python launcher will not be aware of it. You need to add your install in the registry. Adjust the path to be the same you provided to msiexec (in the command above). Save the following to a file named python2.reg

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Python\PythonCore\2.7]

[HKEY_CURRENT_USER\Software\Python\PythonCore\2.7\InstallPath]
"ExecutablePath"="C:\\dev\\my-stuff\\Python27-32\\python.exe"
"WindowedExecutablePath"="C:\\dev\\my-stuff\\Python27-32\\pythonw.exe"
@="C:\\dev\\my-stuff\\Python27-32\\"

Import the file in the registry:

reg import python2.reg

Setup the Python 2 virtual environment

Open the file in a browser and save it to a file called get-pip.py

https://bootstrap.pypa.io/get-pip.py

With that file handy, run:

py -2 get-pip.py
py -2 -m pip install virtualenv

Almost done! Now let's create the actual virtual environment:

py -2 -m virtualenv venv2
venv2\Scripts\activate.bat
pip install requests
REM The following will NOT throw an error
python -c "import requests"
venv2\Scripts\deactivate.bat

Closing remarks

Now now have your 2 virtual environements side by side. Pick one by running either:

  • Python 3 venv3\Scripts\activate.bat
  • Python 2 venv2\Scripts\activate.bat

Then from there you can run plain old python commands as if nothing were.

Upvotes: 1

Related Questions