Virtualenv is a useful tool to create an isolated environment for your Python application. This environment has its own installation directories and environment to keep it separate from other Python application. This doesn’t share libraries with other environments. 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 2 application and use this.

Advertisement

Prerequisites

You must have the following packages installed on your system.

  • Python 2.7
  • PIP

Install Virtualenv with Python 2

You must have Python 2 and PIP installed on your system. Use pip2 to install virtualenv Python module

pip2 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 2 binary file location using which command.

which python2

/usr/bin/python2

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/python2 isoEnv


Running virtualenv with interpreter /usr/bin/python2
Already using interpreter /usr/bin/python2
Using base prefix '/usr'
New python executable in /var/webapps/isoEnv/bin/python2
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:

source isoEnv/bin/activate

The name of the current virtual environment appears to the left of the prompt. For example:

(isoEnv) root@tecadmin$

To verify the correct Python version, run the following:

(isoEnv) root@tecadmin$ python -V

Python 2.7.12

Any package that you install using pip is now placed in the virtual environments project folder, isolated from the global Python installation.

Use pip2 to install a module:

(isoEnv) root@tecadmin$ pip2 install <module>

You can use ‘nose’ if you’re going to work with openstack. For example:

(isoEnv) root@tecadmin$ pip2 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
Share.
Leave A Reply

Exit mobile version