How To Create A Safe Virtual Environment To Work On Python Projects
Think that as a developer you are working on some Python projects, each one with their dependencies and packages. If in these projects there are different versions of the same packages or dependencies or even if they use different Python versions you may have problems, because all of them will be installed on your local system.
The best way to handle this scenario to avoid problems is by isolating our developing environment, creating a virtual environment.
Our first step will be to install Pyenv. In a summarised way, pyenv lets you easily switch between multiple versions of Python. Using the pyenv, it is possible to install multiple Python versions on the machine without having a conflict between the versions. To install pyenv in Ubuntu run the command:
$ curl https://pyenv.run | bash
After we need to open the .bashrc file.
$ nano ~/.bashrc
Here I’m using the nano editor, but you can choose the one you prefer.
At the end of the file you will add these lines:
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
And now you should restart the Shell.
$ exec "$SHELL"
If you are using another Operational System, instead Ubuntu, or another terminal, instead of Ubuntu default terminal, you can look at the Pyenv Github how to install Pyenv.
After install pyenv you should be able to see the version and some commands to use:
$ pyenv
pyenv 2.4.3
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
<List of commands>
To see the Python versions available to be installed:
$ pyenv install -list
Available versions:
2.1.3
2.2.3
...
Choose one version and install it with the command:
$ pyenv install 3.10.7
In this example, I used version 3.10.7, but you can use the version that you want, and install more versions too. After installed you can go to your project folder and execute:
$ pyenv local 3.10.7
This command defines that in this folder the Python version to be used will be the 3.10.7. Using the command ‘pyenv local‘ you can see if the version was correctly defined.
$ pyenv local
3.10.7
With the Python version defined in your project folder, we will install Virtualenv, a tool for setting up your Python environment.
$ pip install virtualenv
Once the virtualenv is installed, go to your project folder and with the Python version defined run the command:
$ python3.10.7 -m venv venv
A folder called venv is created in your project folder and your virtual environment is created.
To active the virtual environment run:
$ source venv/bin/activate
Now, using this terminal which you activated the virtualenv, you can install your project dependencies in an isolated environment, without affecting the Python in your system.
To deactivete the virtual enviroment run:
$ deactivate
Subscribe to my newsletter
Read articles from Pedro Henrique directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Pedro Henrique
Pedro Henrique
Pedro has a degree in Information Systems and is a Software Engineer with more than 6 years of experience, who wants to share some of the knowledge acquired over that time.