The Virtualenv is used to create an isolated environment for Python application. It provides separate environment to application that system environment. This tutorial will help you to create a Python virtual environment on Ubuntu 19.10, 18.04 and 16.04 LTS systems.
Before We Start
Step 1 – Install Virtualenv Module
First of all, you need to install Virtualenv Python module on your system. Use pip3 to install virtualenv Python module. This will also install other required modules on your system.
pip3 install virtualenv Collecting virtualenv Downloading https://files.pythonhosted.org/packages/f7/69/9a07/virtualenv-16.7.4-py2.py3-none-any.whl (3.3MB) 100% |████████████████████████████████| 3.3MB 448kB/s Installing collected packages: virtualenv Successfully installed virtualenv-20.0.14
Step 2 – Create Virtual Environment
Once the installation is finished. Let’s create an isolated Python environment for your application.
Find the python binary location required to run for your application. For this tutorial we are using recently installed Python 3.8. Check the Python 3 binary file location using which command.
which python3.8 /usr/local/bin/python3.8
You can choose any other Python version as per your application requirements.
Now, Create a separate environment for your Application. You can change the environment directory name (isoEnv used in below example) as per your choice. You can also define the environment directory on a different location.
Then create Python isolated environment with the below command:
virtualenv -p /usr/local/bin/python3.8 isoEnv
created virtual environment CPython3.8.2.final.0-64 in 368ms creator CPython3Posix(dest=/var/apps/myPythonApp/isoEnv, clear=False, global=False) seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/root/.local/share/virtualenv/seed-app-data/v1.0.1) activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
This command creates a local copy of your environment specific to this website. While working on this website, you should activate the local environment in order to make sure you’re working with the right versions of your tools and packages.
Step 3 – Work on Virtual Environment
Once you create the Python virtual environment on you Ubuntu system. You need to activate it by running the following command.
Now, you will see the Python isolated environment prompt. Check the python binary location with below command:
(isoEnv) [email protected]:/var/apps/myPythonApp#which python /var/apps/myPythonApp/isoEnv/bin/python
You will see that binary location is inside isolated Python environment. Also the binary name is just “python” instead of python3.8.
Let’s check the Python version inside isolated environment.
(isoEnv) [email protected]:/var/apps/myPythonApp#python -V Python 3.8.2
Any package that you install using pip is now placed in the virtual environments project folder, isolated from the global Python installation.
Use pip3 to install a module. You can use ‘nose’ if you’re going to work with openstack. For example:
(isoEnv) [email protected]:/var/apps/myPythonApp#pip3 install nose Collecting nose Downloading nose-1.3.7-py3-none-any.whl (154 kB) |████████████████████████████████| 154 kB 12.2 MB/s Installing collected packages: nose Successfully installed nose-1.3.7
Step 4 – Deactivate Virtual Environment
After finishing your work inside the virtual environment, just run “deactivate” command to exit from isolated environment. Then, You will get the users to default shell.
(isoEnv) [email protected]:/var/apps/myPythonApp#deactivate
Step 5 – Delete Virtual Environment
Delete the Python virtual environment from you application. Simply delete the isoEnv directory from you application folder.
rm -rf isoEnv