Virtualenv
The virtual Python Environment (venv) is a Python environment that helps you install different versions of Python modules on your local directory, so you can develop and test your code without having to install everything in your system.
Knowledge points
- Installation of Virtualenv
- Create a virtual environment
- Activating a virtual environment
- Using multiple Virtual Environments
- Turn off the virtual environment
Experiment Step 1. Installation
Install PIP3 first.
$ sudo apt-get update$ sudo apt-get python3-pip
Install virtualenv with the following command:
$ sudo pip3 install virtualenv
2. Usage
We will create a virtual directory called, in which we will have two different virtual environments.
$ mkdir virtual
The following command creates an environment called VIRT1.
cd virtual$ virtualenv virt1
Now we activate this VIRT1 environment.
source virt1/bin/activate(virt1)[[email protected]]$
The first part of the prompt is the name of the current virtual environment, which helps you to identify which environment you are in when you have multiple environments.
Now we will install redis this Python module.
(virt1)$ sudo pip3 install redis
Use the deactivate command to close the virtual environment.
(virt1)$ deactivate$
Now we will create another virtual environment Virt2, we will install the module in the same redis , but the version is the old version of 2.8.
source virt2/bin/activate(virt2)$ sudo pip3 install redis==2.8
This can have many different environments for all your development needs.
Summarize
Always remember to create a virtual environment when developing new applications, which will help keep your system modules clean.
Python's virtualenv