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.
You must have the following packages installed on your system.
- Python 3
Install Virtualenv with Python 3
You must have Python 3 and PIP installed on your system. Use pip3 to install virtualenv Python module
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
The Python3 is installed at the standard location. Find the Python 3 binary file location using which command.
which python3 /usr/bin/python3
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.
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.
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.
To activate the new virtual environment, run the following:
The name of the current virtual environment appears to the left of the prompt. For example:
(isoEnv) [email protected]$
To verify the correct Python version, run the following:
(isoEnv) [email protected]$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) [email protected]$pip3 install <module>
You can use ‘nose’ if you’re going to work with openstack. For example:
(isoEnv) [email protected]$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) [email protected]$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