How To Install And Use Python Virtualenv Module

For various reasons, we will need to install many versions of Python interpreter under the same operating system. We may also need different versions of same module for various reasons in one Python project. This causes a lot of confusion in Python environment management.

Usually, different Python interpreters and dependency libraries are required for each single project, or even for versions at different stages of one single python project. In order to manage these environments clearly, safely and reliably, we need to create a “independent, isolated” Python operating environment for each python project or application. Virtualenv is just the tool used to create a set of “isolated” Python runtime environment for each python project, this article will tell you how to install and use python virtualenv.

1. Install Virtualenv.

Prior to Python version 3.4, virtualenv need to be installed independently and manually before it could be executed. In later versions, python comes with a venv library. You can execute python – m venv [virtual_env_directory_name] command to achieve the same effect as virtalenv. But more users are accustomed to using virtualenv. Now i will tell you both how to use python venv library and virtualenv tool.

1.1 How To Use Python Venv Library.

  1. Open a terminal and run below command.
    (base) $ python3 -m venv test_python_env
  2. Then you can see the test_python_env directory has been created in current folder.
    (base) $ ls -l
    drwxr-xr-x    6 songzhao  staff      192 Jan 24 18:26 test_python_env
  3. To enter the virtual python environment, you should run $ source test_python_env/bin/activate script in terminal.
    (base) $ source test_python_env/bin/activate
    (test_python_env) (base) $
  4. To exit the virtual python environment, you should run $ deactivate command.
    (test_python_env) (base) $ deactivate
    (base) $
  5. To remove the virtual environment, just run rm -rf test_python_env to remove the virtual env folder.

1.2 How To Use Python Virtualenv Tool.

  1. Open a terminal and run $ pip3 install virtualenv command to install virtualenv software.
    (base) $ pip3 install virtualenv
    Collecting virtualenv
      Downloading https://files.pythonhosted.org/packages/05/f1/2e07e8ca50e047b9cc9ad56cf4291f4e041fa73207d000a095fe478abf84/virtualenv-16.7.9-py2.py3-none-any.whl (3.4MB)
         |████████████████████████████████| 3.4MB 471kB/s 
    Installing collected packages: virtualenv
    Successfully installed virtualenv-16.7.9
    WARNING: You are using pip version 19.1.1, however version 20.0.1 is available.
    You should consider upgrading via the 'pip install --upgrade pip' command.
  2. Go to your python project directory ( $ cd my_python_project/ ), and run the virtualenv command ( $ virtualenv my_python_env ) to create a virtual python environment directory.
    (base) $ cd my_python_project/
    (base) $ virtualenv my_python_env
    Using base prefix '/Library/Frameworks/Python.framework/Versions/3.7'
    New python executable in /Users/songzhao/my_python_project/my_python_env/bin/python3.7
    Also creating executable in /Users/songzhao/my_python_project/my_python_env/bin/python
    Installing setuptools, pip, wheel...
    done.
    (base) $ ls -l
    total 0
    drwxr-xr-x  6 songzhao  staff  192 Jan 24 19:17 my_python_env
    (base) $ 
    
  3. Activate the virtual python environment by run $ source my_python_env/bin/activate
    (base) $ source my_python_env/bin/activate
    (my_python_env) (base) $ 
    
  4. Now you can install your project required python library in the virtual python environment use pip install command. You can also get the python version that current virtual environment used with command python -v.
    get-current-python-version-with-python-v-commandget-current-python-version-with-python-v-commandget-current-python-version-command
  5. To exit the virtual python environment, you should run deactivate command.
    (my_python_env) (base) $ deactivate 
    (base) $
  6. To delete the virtual python environment, just run rm -rf ./my_python_env
  7. If you want to use another python interpreter as the virtual environment used python interpreter, you can run command virtualenv –-python=/usr/bin/python2.7 –-no-site-packages python_2.7_env. The –python argument point out the python interpreter that you want to use in the virtual environment. The –no-site-packages argument point out that the virtual environment will not copy the original python system library packages into the new virtual environment.

2. Backup And Restore Python Virtual Environment Libraries.

After you create the python virtual environment and installed required library packages in it. You can run command pip freeze > /Users/requirements.txt to backup the virtual environment installed python libraries list into the requirements.txt file. Then you can run command pip install -r requirements.txt to restore the python libraries into a new python virtual environment.

References

  1. https://virtualenv.pypa.io/en/latest/