The first python project I completed was not a great one but it did what it was supposed to. A few days ago, I needed to use the project. And when I tried to run it, there were a lot of bugs now. A few years ago, it ran perfectly. It took me a few hours to get it working again.
The reason this happened is because I had not used a virtual environment when I completed it.
What is Virtual Environment?
When python and its tools (including libraries) are installed, they are installed in a standard location (for eg. /usr/bin) by the system (might be an OS) and this is where the python interpreter and libraries are searched for when you run python scripts. Virtual environment is an isolated setup of python executable and libraries, isolated as in separated from the standard locations mentioned above. When you run a python script inside virtual environment, python interpreter and libraries are searched in the virtual environment.
Why use Virtual Environment?
Lets look at the advantages of using virtual environments.
- When the system is updated, python and its libraries in the standard locations are also updated. Projects which use older versions of Python or its libraries may behave unexpected. This is what caused my problems.
- The versions of Python and its libraries can be chosen inside the virtual environment and these do not get updated when the system is updated. So, even the older versions can be used as isolated run-time environments.
- You can use multiple versions of Python for multiple projects using separate virtual environments. Using standard system version can only allow use of one Python version.
- The requirements of the projects with their individual versions can be easily saved in a file from a virtual environment which helps to recreate the same run-time environment on other systems too.
How to start Virtual Environment?
First you need to have Python installed on your system. If you need help on this, take a look here.
Virtual Environment can be started with Pipenv or Virtualenv.
virtualenv if not installed:
pip install virtualenv
Then start a virtual environment with this command:
You can specify which python version to use like this:
virtualenv -p /usr/bin/python3 my_venv
This will install the specified python interpreter and standard libraries for this python version in the folder my_venv as follows:
Now, you have to activate this virtual environment to instruct the system to use the interpreter from this environment instead of standard one. This can be done like this:
If you are on Windows, the activation script should be on the
Scripts folder instead of
A clear indication of whether the virtual environment has been activated or not will appear in the command line. On some system it may appear as:
pip can be used to install or uninstall the required dependencies in the virtual environment.
(my_venv) $ pip install flask
(my_venv) $ pip uninstall django
To see, what dependencies are install in the virtual environment, following can be used:
(my_venv) $ pip freeze
Execute this command to save the dependencies status of virtual environment to a file.
(my_venv) $ pip freeze > requirements.txt
This will create a file requirements.txt with dependencies and their versions. This file can be used to recreate virtual environment elsewhere too. This can be done by:
(my_venv) $ pip install -r requirements.txt
When you’re done working in the virtual environment and want to use the system’s default python, you need to deactivate the virtual environment.
(my_venv) $ deactivate
This can be verified as the virtual environment indicator will dissappear now.
pipenv is higher level abstraction and uses
virtualenv in the background.
pip install pipenv
pipenv on the system. To install packages to the project, change directory to the project and start pipenv shell.
pipenv install <package_name>
pipenv install numpy [Installs latest version]
pipenv install flask==0.12.1 [Installs specified version]
To uninstall a package:
<package_name>pipenv uninstall numpy
pipenv uninstall flask==0.12.1</package_name>
When pipenv install command is used for the first time, two new files will be created:
Pipfile.lock file. The dependencies you install in the virtual environment and their versions are stored in these files which can be used to recreate this environment later using this command:
pipenv has a lot more functionalities which lie outside the scope of this article.
So, don’t make the mistakes that I made, always use virtual environments.