Virtualenv is a tool used to create an isolated Python environment. This environment has its own installation directories and environment. This doesn’t share libraries with other environments. It is very helpful for the application required separate environments on the same server. The Virtualenv is the easiest and recommended way to configure a custom Python environment. This tutorial will help you to how to create a virtual environment for your Python application and use this.
Prerequisites
You must have the following packages installed on your system.
- Python 3
- PIP
Install Virtualenv with Python 3
You must have Python 3 and PIP installed on your system. We use virtualenv package to create virtual environment for the Python applications. So first install virtualenv Python module using command:
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-16.7.4
Create Virtual Environment
Search for the Python3 binary location on your system. Use the following command to find the Python 3 binary file location on your system.
which python3 /usr/bin/python3
As per the above command, Python 3 binary is available at /usr/bin/python3.
Next, switch to your Python application directory
cd /var/webapps/
Then, 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.
virtualenv -p /usr/bin/python3 isoEnv Running virtualenv with interpreter /usr/bin/python3 Already using interpreter /usr/bin/python3 Using base prefix '/usr' New python executable in /var/webapps/isoEnv/bin/python3 Also creating executable in /var/webapps/isoEnv/bin/python Installing setuptools, pip, wheel... done.
The above command will creates a separate environment for the Python binary file. You can access it by typing python only.
Activate Virtual Environment
You need to activate virtual environment before using it. This will insure to install all modules and configurations to virtual environment only. To activate the virtual environment, execute:
source isoEnv/bin/activate
The name of the current virtual environment appears to the left of the prompt. Your prompt will be look like (isoEnv) root@tecadmin$
To verify the correct Python version, run the following:
(isoEnv) root@tecadmin$ python -V Python 3.6.8
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:
(isoEnv) root@tecadmin$ pip3 install <module>
You can use ‘nose’ if you’re going to work with openstack. For example:
(isoEnv) root@tecadmin$ pip3 install nose Collecting nose Downloading https://files.pythonhosted.org/packages/15/d8/dd071918c040f50fa1cf80da16423af51ff8ce4a0f2399b7bf8de45ac3d9/nose-1.3.7-py3-none-any.whl (154kB) |████████████████████████████████| 163kB 18.6MB/s Installing collected packages: nose Successfully installed nose-1.3.7
Deactivate virtualenv Environment
After finishing your work inside the virtual environment, simply exit from this by typing deactivate command. You will get the users to default shell.
(isoEnv) root@tecadmin$ deactivate
Delete virtualenv Environment
If your application no more required the virtual environment, You can delete this. To delete the environment simply delete the environment directory.
rm -rf isoEnv
Conclusion
In this tutorial, you have learned about configuration of the Python virtual environment on Linux system.
2 Comments
why not python3 -m venv /path/to/venv ?
Thank you for python 3.8 installation post. I am learning python . I am senior citizen. I hope I can get help from you from time to time.