Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create virtualenv in Python 2.7 on windows 10 while other virtualenv are working in Python 3.8

I'm unable to install a virtual environment in Python 2.7. Whenever I try this command py -m pip install --user virtualenv or py -m pip install --user venv it shows requirements are already satisfied in 3.8. Although I've activated the Python 2.7 and in cmd py simply typing python it shows the Python 2.7.12rc1 (v2.7.12rc1:13912cd1e7e8, Jun 12 2016, 05:51:33) [MSC v.1500 32 bit (Intel)] on win32

Please guide me on how can I install a virtual environment in Python 2.7 and I don't want to uninstall/remove Python3.8 Please lemme know if you need further explanation. Thanks a lot*

like image 650
Muhammad Afzaal Avatar asked Sep 22 '26 19:09

Muhammad Afzaal


1 Answers

When you have multiple version of Python installed on a Windows PC, you can use the specific Python.exe files to control which version you're working with

PS C:\> .\Python27\python.exe --version
Python 2.7.9
PS C:\> .\Python37\python.exe --version
Python 3.7.7

The same rule applies for pip

PS C:\> .\Python27\Scripts\pip.exe --version
pip 20.2.4 from C:\Python27\lib\site-packages\pip (python 2.7)
PS C:\> .\Python37\Scripts\pip.exe --version
pip 20.2.4 from c:\python37\lib\site-packages\pip (python 3.7)

You need to ensure that you've installed virtualenv using the Python 2.7 version of pip, and verify it's using the correct python version by checking the path

PS C:\> .\Python27\Scripts\pip.exe install virtualenv
PS C:\> .\Python27\Scripts\virtualenv.exe --version
virtualenv 20.1.0 from c:\python27\lib\site-packages\virtualenv\__init__.pyc

I have a Python 3.7 version installed as well for this demonstration

PS C:\> .\Python37\Scripts\virtualenv.exe --version
virtualenv 20.1.0 from c:\python37\lib\site-packages\virtualenv\__init__.py

Then create your virtual environment using the version that you want, activate it and it should be the version of Python you want

PS C:\> .\Python27\Scripts\virtualenv.exe py2env
PS C:\> .\py2env\Scripts\activate
(py2env) PS C:\> python
Python 2.7.9 (default, Dec 10 2014, 12:28:03) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

And finally, the Python 3.7 example

PS C:\> .\Python37\Scripts\virtualenv.exe py3env
PS C:\> .\py3env\Scripts\activate
(py3env) PS C:\> python
Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
like image 97
LouieC Avatar answered Sep 25 '26 09:09

LouieC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!