Installing and Using virtualenv in Python

by Alex
Installing and Using virtualenv in Python

virtualenv is a tool for creating an isolated Python environment. This environment has a separate python installation and the downloaded libraries are not available to others. It is possible to make this environment have no access to the global libraries. Virtualenv is a simple and recommended way to set up a Python environment.

The differences between virtualenv and venv

Venv is the default package that comes with Python 3.3+. It is not available in Python 2. Virtualenv is a more advanced library. See the link for the main differences. You can create a virtual environment with venv but it is recommended to install and use virtualenv to get the most out of it.

Installing virtualenv with pip

You will need to use pip to install virtualenv with Python. Updating this tool is a good idea.

python -m pip install --upgrade pip

Or use python3 if python refers to version 2. After the upgrade you can install virtualenv as well:

pip install virtualenv # or pip3

Creating a virtual environment

1. Go to the directory where you want to create the virtual environment (e.g. project folder).

~$ cd test
~/test$

2. Create the virtual environment, specifying a particular version of Python if necessary. The following command creates a virtualenv named venv_name and uses the -p flag to specify the full path to the Python version:

virtualenv -p /usr/bin/python venv_name
# or
virtualenv venv_name

You can name the environment anything you like After running the command you will see the logs:

Running virtualenv with interpreter /usr/bin/python
Already using interpreter /usr/bin/python
Using base prefix '/usr'
New python executable in /home/username/test/venv_name/bin/python
Installing setuptools, pip, wheel...
done.

This command creates a local copy of the environment. When working with it, it is important to remember to activate it so that the correct versions of specific tools and packages are used. If setuptools pip failed with error code 1` error during installation, use the following command to solve the problem:

pip install --upgrade setuptools # or pip3

3. Use the command to activate the new virtual environment:

source venv_name/bin/activate

The name of the current environment will then appear to the left of the input symbol: (venv_name) [email protected]:~/test$ Now if you install any package using pip, it will be placed in the folders of this environment, isolated from the global installation.

Deactivate virtualenv

Once the virtual environment has been completed, you can deactivate the virtual environment with the deactivate command. Enter it and the venv_name prefix will disappear. You will go back to using the global version of python.

Uninstalling the virtual environment

To uninstall a virtual environment it is sufficient to simply delete the project folder. The following command is used to do this:

rm -rf venv_name

Solving popular errors

Errors when creating virtualenv. The following errors may occur when trying to create virtualenv with Python 3.7.

AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
OSError: Command /home/username/venv/bin/python3 -c "import sys, pip; sys...d\"] + sys.argv[1:])" setuptools pip failed with error code 1

To fix them, add the following line to the .bash_profile.

export LC_ALL="en_US.UTF-8"

Using the full path to the virtual environment. It may be that the wrong version is used when using the virtualenv command. To solve the problem, you just need to set the full paths to both virtualenv and Python on the system.

And you can get them using this command:

/home/username/opt/python-3.8.0/bin/virtualenv -p /home/username/opt/python-3.8.0/bin/python3 venv

Related Posts

LEAVE A COMMENT