🦉 It is a wise move to always run your Python projects in a virtual environment.
🎈 Installing the many dependencies that the average Python program requires into your base OS can soon lead to a bloated system.
🍱 Better to run a virtual environment per project and keep everything clean and compartmentalized.
- With that in mind, I had a requirement recently to run a program that required Python 2.7. I was running Python 3.8 on my machine.
- I realized that a virtual environment itself wasn't going to be enough. By default my virtual environments were all using Python 3.8.
Here's how I approached the problem on Windows 10:
Install Python2.7 from python.org
Make sure you take a note of where the .exe file is installed. On my Windows machine the path was:
Install the Python package 'virtualenv' using pip:
pip install virtualenv
Create a new virtual environment using the Python 2.7 executable.
Here the virtual environment is called venv1 and it uses Python2.7.
virtualenv -p C:\Python27\python.exe venv1
Activate the virtual environment.
Now if I enter 'python' I can see that I'm using Python 2.7.18 rather than the default 3.8:
(venv1) PS C:\Users\joe> python Python 2.7.18<snip>
To create a virtual environment with Python 3.8, I can just run virtualenv without the p flag: