I know that the title is rather a clickbait. It should be. If you are not using virtual environments for Python, you should start. This is really easy and can save you a lot of frustration.
Why do I need to use virtual environments?
In my case, the most common use case is that I want to use a different library version in different projects (to be exact, I don’t want to upgrade the library version globally). In my Machine Learning projects, I want my particular model to work on the particular library version because all the parameters I use are configured for this particular library. By upgrading the library globally, I can break various good working models.
The second use case on my end is to test new libraries without installing them globally. My tests will not interfere with others’ work. Once I finish, I can simply throw away my test directory and that’s all.
How does it work?
The Python itself is not able to create virtual environments. You will have to install the
virtualenv module. This tool handles all required actions for your virtual environment to work. It creates the directory where your project will live and takes care of the executables that are necessary to host your project. Please note that you will have to “switch to the virtual environment” when starting work with your project and “turn it off” afterward. I will cover this later in the text.
Three simple steps
First, you have to install the
$ sudo pip install virtualenv
Second, you create your virtual environment by running the following command:
$ virtualenv my_project
The above command creates the project directory with all necessary elements in it. To start working in your virtual environment, you need only one more step:
$ source my_project/bin/activate
The command above activates your virtual environment and now all your Python actions are performed in this environment. Please note that you have to activate your virtual environment every time you want to work on it. Once you finish, you can (and you should) deactivate it by using:
You may also need…
The three steps above are the most important for you. In most cases, they are also the only thing you will need to successfully use virtual environments.
… to install modules
If you will need to install the additional modules, you can simply activate the environment you want to install them in and use
pip install as always. The modules will be installed in your virtual environment and will not interact with the ones installed in the system or other environments.
… to use a globally installed modules
Instead of installing everything from scratch in your virtual environment, you may want to use the modules already installed globally. In such a case, when creating a virtual environment, use this statement:
$ virtualenv my_project --system-site-packages
This way, when your script reaches for the module, Python is first checking for the module installed in your virtual environment and if the module is not there, it searches in the global repository.
… to use a specific version of Python
In some cases, you may want to use the particular Python version in your virtual environment. To do so, you should add the information about the version to use during the virtual environment creation:
$ virtualenv -p /usr/bin/python3 my_python3_project
This way the created environment will specifically use the Python3 version, even though the default version for your system is Python2.
… to list modules installed in your virtual environment
My virtual environments are getting sometimes a little bit messy. To review modules available (installed) in my virtual environment, I simply use:
$ pip list Package Version --------------- ------- boto3 1.10.36 botocore 1.13.36 docutils 0.15.2 jmespath 0.9.4 pip 19.3.1 python-dateutil 2.8.0 s3transfer 0.2.1 setuptools 42.0.2 six 1.13.0 urllib3 1.25.7 wheel 0.33.6
It gives me a list of installed modules and their versions.