In our previous article, we discussed what exactly python
virtualenv is and the situation where it comes in handy. In the world of Python
virtualenv is the most recommended way of working.
virtualenv is a tool to create isolated Python environments.
virtualenv creates a folder which contains all the necessary executable to use the packages that a Python project would need. When it is initiated, it automatically comes with its own Python interpreter – a copy of the one used to create it – alongside its very own pip.
In order to install
virtualenv , we are going to call in pip for help. We will install it as a globally available package for the Python interpreter to run.
The simplest method to install is using pip to search, download and install. This might not provide you the latest stable version.
root@devops# pip install virtualenv
Activate and Using virtualenv
Using this tool consists of getting it to create a folder, containing the Python interpreter and a copy of pip. Afterwards, in order to work with it, we need to either specify the location of that interpreter or
All the applications you install using the interpreter inside the virtual environment will be places within that location.
When you use pip to create a list of them, only the ones inside the folder will be compiled into a file.
Creating / Initiating a virtual environment:
# Let's create an environment called *my_app* root@devops# virtualenv my_app
Creating an environment with a custom Python interpreter:
# Example: virtualenv --python=[loc/to/python/] [env. name] virtualenv --python=/opt/python-3.3/bin/python my_app
Activating a virtual environment:
# Example: source [env. name]/bin/activate # Let's activate the Python environment we just created root@devops# source my_app/bin/activate
Deactivating a virtual environment:
# Example: deactivate # Let's deactivate the environment from earlier root@devops# deactivate