Virtual environments in Python: forget about dependency conflicts
When installing Python packages with pip, you may find warning messages about some dependency packages versions not being compatible with other installed packages. You can create isolated containers (virtual environments) in which you can safely install any Python package.
Table of Contents
- Create a virtual environment
- Activate the environment
- Exit from the virtual environment (deactivate)
Create a virtual environment
The process of creating a virtual environment will create a folder inside your working directory. You can name the environment whatever you want. The resulting folder will have that name.
python -m venv <name>
You can also use virtualenv
package. Install it with pip
(pip install virtualenv
) and then run:
virtualenv -p python3 <name>
Activate the environment
Finally, to “enter” into the virtual environment, execute the file named activate
inside <name>/bin/
. You can use source
to do this.
source ./<name>/bin/activate
Your terminal prompt will display the environment name inside parenthesis:
(ocrmypdf) [ricardo@myarch python-environments]$
You’re now inside an isolated container where you can install python packages without worrying about dependency conflicts.
(dooit) [ricardo@myarch dooit]$ python -m pip install git+https://github.com/kraanzu/dooit.git
Collecting git+https://github.com/kraanzu/dooit.git
Cloning https://github.com/kraanzu/dooit.git to /tmp/pip-req-build-va8pmhkt
Running command git clone --filter=blob:none --quiet https://github.com/kraanzu/dooit.git /tmp/pip-req-build-va8pmhkt
...
Exit from the virtual environment (deactivate)
Just run deactivate
and you’ll notice the default terminal prompt will go back (without the environment name inside parenthesis).
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles:
Featured content: