For various reasons, we will need to install many versions of the Python interpreters under the same operating system. We may also need different versions of the 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 every 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 an “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 environments 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 needs 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.
- Open a terminal and run the below command.
(base) $ python3 -m venv test_python_env
- Then you can see the test_python_env directory has been created in the current folder.
(base) $ ls -l drwxr-xr-x 6 songzhao staff 192 Jan 24 18:26 test_python_env
- To enter the virtual python environment, you should run $ source test_python_env/bin/activate script in the terminal.
(base) $ source test_python_env/bin/activate (test_python_env) (base) $
- To exit the virtual python environment, you should run the $ deactivate command.
(test_python_env) (base) $ deactivate (base) $
- 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.
- Open a terminal and run the $ 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.
- 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) $
- Activate the virtual python environment by run $ source my_python_env/bin/activate
(base) $ source my_python_env/bin/activate (my_python_env) (base) $
- Now you can install your project required python library in the virtual python environment using the pip install command.
- You can also get the python version that the current virtual environment is used with the command python -v.
- To exit the virtual python environment, you should run deactivate command.
(my_python_env) (base) $ deactivate (base) $
- To delete the virtual python environment, just run rm -rf ./my_python_env
- 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 points out the python interpreter that you want to use in the virtual environment.
- The –no-site-packages argument points 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 install the 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 the command pip install -r requirements.txt to restore the python libraries into a new python virtual environment.