This post is based on a Youtube video by teclado, which I belive, is the clearest explanation of virtual environment for python available online.
Table of Contents:
- Why do we need virtual environment for Python projects?
- Python versions on your Mac
- Default python directory
- Install virtual environment for projects on Python 3
- Install virtual environment for projects on Python 2
- Activate and de-activate virtual environment
- Install 3rd party libraries for the virtual environment
- Conclusion
Why do we need virtual environment for Python projects?
Python is a fast evloving language, you can tell its development pace by looking at the versions released almost every month. The proper functioning of Python projects also rely on a lot of 3rd party libraries, or as people call them, depencies, which are updating quickly along the way as well.
Each version upgrade might be small, but accumulative upgrades eventually lead to fundamental changes to some functions or variables, and the project developed on certain version of Python and libraries may not run correctly in the latest version. Since there are so many versions of Python itself and the 3rd party libraries, your projects will inevitably run into conflicts.
A virtual environment is the way to solve this version conflict problem. Simply put, we put the corresponding version of python and all libraries/depencies into the same file folder with the project, creating a mini environment for the project. Whenver the project run, it runs under this very environment, no matter what other versions the computer are installed, or other projects depend on.
Python versions on your Mac
You may have installed different versions of Python on your Mac. To check the versions, you type python
in the Terminal, then press tab
button.
You can see that I have both python 2 and 3 installed on my Mac, and the exact versions are 2.7 and 3.11, respectively.
Default python directory
Now here comes the question, if you key in some python command in the Terminal, which version would the computer use to execute your command?
To find out the answer, we can type echo $PATH$
and press return
:
echo $PATH$
gives you a list of directories, each separated by a colon. In layman’s terms, a path (or the search path) is the list of directories that will be searched for anything that you type on the command line.
You can see that the directory of python 3.11 comes before that of 2.7, so theoretically, whatever python command I key in, the system should search any corresponding word in 3.11 versions.
However, here I have a small problem, for whenever I type python -V
, it pops up the older version as below. But anyway, this example shows that we need to keep an eye on the correct version the system will look for when executing our python command.
Install virtual environment for projects on Python 3
Python 3 comes with a very handy command -m venv
to create a virtual environment.
Let’s create a new folder Project
, and under this folder, we have a hello.py
file only.
Now we want this python project to be run with version 3. We simply open terminal and go to the Project
folder, then type:
In couple of seconds, the system creates a sub-folder named “my-env” under the Project
folder.
Open the sub-folder, we can see the system copied python 3.11 and all native libraries into this folder. In another word, it has created all components of a virtual environment for us.
Install virtual environment for projects on Python 2
The basic idea is the same, but in Python 2 we don’t have a venv
commend, but use virtualenv
instead.
First of all, we use pip
to install virtualenv
package. pip
is a Python pacakge manager, usually comes with any Python versions. In terminal, type in:
pip install virtualenv
Then go to Python 2 project folder, create the components using the following command, and let the system do the remaining job for you:
virtualenv my-env2
Activate and de-activate virtual environment
It’s very straight forward to activate the virtual environment for a specific project. You simply to the components sub-folder under the project main folder, and run the activate
command. Remember, you need to put source
command before it.
Once activated, you can tell it from the environment name in parenthesis before the prompt.
After you finish running the project, or you want to switch to another project, you type deactivate
to switch off the virtual environment. The name before the prompt will go away, meaning the previous virtual environment is off.
Install 3rd party libraries for the virtual environment
Now we want to install some 3rd party libraries that our project is going to use. We use pip
to install libraries, and whenever we run pip
under project folder prompt, we want to install them into the virtual environment of the project.
For example, if we want to install flask
to the project, first we have the virtual environment activated, then
The pip
package manager will install the latest flask
library to project’s virtual environment.
A nice practice to keep 3rd libraries in neat order and easy to track, is to use a text file to record all libraries and their correct versions for the project. As a common practice, we name this text file requirements.txt
, and put this file in the root foler of the project.
The content of the text file usually like this (project sensative);
flask==1.1.3
requests
gunicorn>=2.3,<3.0
With this txt file, we can in fact install all listed libraries using one line of command:
Conclusion
It’s always a good and conflicts free practice to create virtual environment for each Python project. You cannot be wrong to be conservative.
There are also a lot of tools out there to help create and manage virtual environments for you. But we’d like to have a touch on the basic and keep it as simple and straigth forward as possible.