Always use a Virtual Environment
π¦ 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.
Python 2 and Python 3?
- 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:
C:\Python27\python.exe
-
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.
.\venv1\Scripts\activate
-
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:
virtualenv venv2
β¨πΉβ¨
@joeneville_
Top comments (0)