1. An overview of the
This article introduces the use of Pyenv + Pipenv to manage local virtual environments for Python projects.
- Pyenv: Installs and manages multiple Versions of Python.
- Pipenv: Create a separate virtual environment for each project.
All operations below are performed on Ubuntu 16.04.
2. Python version management: Pyenv
2.1. Installation pyenv
$ curl https://pyenv.run | bash
Copy the code
Pyenv-related content is installed in the ~/. Pyenv/directory.
After installation, add the following to ~/.bashrc as prompted:
export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Copy the code
Upgrade pyenv:
$ pyenv update
Copy the code
Delete pyenv:
$ rm -rf ~/.pyenv
Copy the code
And delete environment variables in ~/.bashrc.
2.2. Install and manage multiple Pythons
View installable versions:
$ pyenv install --list
Copy the code
Install the specified version:
$pyenv install 3.8.2Copy the code
Before installing Python, install the dependency packages required to compile Python:
$ sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
Copy the code
See Common Build Problems. ModuleNotFoundError: No module named ‘_sqlite3’ may cause problems when importing some Python libraries after compiling.
View the current installed Python version:
$ pyenv versions
* system (setBy the/home/hy /. Pyenv 3.8.2 / version)Copy the code
All Python versions installed using Pyenv are saved in ~/. Pyenv /versions/.
2.3. Each directory can specify a particular version of Python to execute
Before this is specified, the default Python is 2.7:
$ mkdir test
$ cd test$python Python 2.7.12 (default, Oct 8 2019, 14:14:10) [GCC 5.4.0 20160609] on linux2 Type"help"."copyright"."credits" or "license" for more information.
>>>
Copy the code
The pyenv local command specifies the version of Python to execute when python is executed in this directory:
$ pyenv local$ls-la Total 12 drwxrwxr-x 2 HY HY 4096 3月 10 16:04. Drwxrwxr-x 42 HY hy 4096 3月 10 13:02.. -rw-rw-r-- 1 hy HY 6 3月 10 16:03. Python -version $cat. Python -version 3.8.2Copy the code
The local command generates a hidden file with the version number in the current directory. Python-version.
Verify the python version executed:
$python Python 3.8.2 (default, Mar 10 2020, 13:47:49) [GCC 5.4.0 20160609] on Linux Type"help"."copyright"."credits" or "license" for more information.
>>>
Copy the code
2.4. Switch global Python versions
$pyenv Global 3.8.2 $python Python 3.8.2 (default, Mar 10 2020, 13:47:49) [GCC 5.4.0 20160609] on Linux Type"help"."copyright"."credits" or "license" for more information.
>>>
Copy the code
3. Virtual environment Management: Pipenv
3.1. Installation pipenv
Make sure you have the latest 3.x version of Python and PIP installed
Python 3.8.2 $$python - V PIP -v PIP 19.2.3 from/home/hy /. Pyenv/versions / 3.8.2 / lib/python3.8 / site - packages/PIP (python 3.8)Copy the code
Installation:
$ pip install pipenv
Copy the code
Update:
$ pip install --upgrade pipenv
Copy the code
3.2. Create a separate virtual environment for each project
Create project directory:
$ mkdir django_test && cd django_test
Copy the code
Add export PIPENV_VENV_IN_PROJECT=1 to ~/. Bashrc. For the configuration to take effect, run source ~/. Bashrc.
Create a virtual environment:
$ pipenv --python 3.8
Creating a virtualenv forThis project... Pipfile: / home/hy/workspace/temp/django_test Pipfile/Using/home/hy /. Pyenv/versions / 3.8.2 / bin/python (3.8.2) to create Virtualenv... ⠸ Creating virtual environment... Created virtual environment CPython3.8.2. Final. 0-64in178ms creator CPython3Posix(dest=/home/hy/workspace/temp/django_test/.venv, clear=False, global=False) seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/home/hy/.local/share/virtualenv/seed-app-data/v1) activators BashActivator, CShellActivator FishActivator, PowerShellActivator PythonActivator, XonshActivator ✔ Successfully created virtual environment! Virtualenv location: /home/hy/workspace/temp/django_test/.venv Creating a PipfileforThis project... $ls-la Total 16 DRwxrwxr-x 3 HY HY 4096 3月 11 12:15. Drwxrwxr-x 42 HY 4096 3月 10 13:02.. -RW-RW-r -- 1 HY HY 138 3月 11 12:15 Pipfile drwxrwxr-x 4 HY hy 4096 3月 11 12:15. VenvCopy the code
Where the URL of pypI source is saved in the automatically generated Pipfile generation:
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
[requires]
python_version = "3.8"
Copy the code
You can set the source URL to a domestic mirror address to speed up the download:
[[source]]
name = "pypi"
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
verify_ssl = true
[dev-packages]
[packages]
[requires]
python_version = "3.8"
Copy the code
Install dependency packages:
$ pipenv install "Django = = 2.1"Installing django = = 2.1... Adding django to Pipfile's [packages]... ✔ Installation Succeeded Pipfile. Lock not found, creating... Locking/dev - packages dependencies... Locking/packages dependencies... ✔ Success! Updated Pipfile.lock (a5a621)! Installing Dependencies from Pipfile.lock (a5A621)... 🐍 ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ ▉ 2/2-00:00:01 To activate this project 's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
Copy the code
Install dependency packages in test environment:
$ pipenv install pytest --dev
Copy the code
Displays installed dependency package relationships:
$pipenv graph Django==2.1 - pytz [Required: Any, installed: 2019.3] Pytest ==5.3.5 - attrs [required: >=17.4.0, installed: 19.3.0] -more-itertools [Required: >=4.0.0, installed: 8.2.0] -packaging [required: Any, installed: 20.3] -Pyparsing [Required: >=2.0.2, installed: 2.4.6] -six [required: Any, installed: 1.14.0] -pluggy [Required: >=0.12,<1.0, installed: 0.13.1] -py [Required: >=1.5.0, installed: 1.8.1] -wcwidth [Required: Any, installed: 0.1.8]Copy the code
Delete dependent packages:
$pipenv uninstall Django Uninstalling Django... Found Existing Installation: Django 2.1 Uninstalling Django-2.1: Successfully Uninstalled Django-2.1 Removing Django from Pipfile... Locking/dev - packages dependencies... Locking/packages dependencies... Updated Pipfile.lock (91e3b9)!Copy the code
Enter the virtual environment:
$ pipenv shell
Copy the code
4. Project management
$ls-la Total 20 DRwxrwxr-x 3 HY HY 4096 3月 11 12:31. Drwxrwxr-x 42 HY 4096 3月 10 13:02.. -RW-rw-r -- 1 hy HY 185 3月 11 12:31 pipfile-RW-r --r-- 1 HY HY 3666 3月 11 12:31 pipfile. lock drwxrwxr-x 5 hy 4096 3月 11 12:21 .venvCopy the code
Add automatically generated Pipfile and pipfile. lock files to version control. Do not add.venv directories to version control.
Team members install Pyenv and Pipenv, configure the corresponding environment variables in ~/.bashrc, clone project source code, run pipenv install –dev to rebuild the virtual development environment.
$ cd django_test
$ pipenv install --dev
Copy the code
resources
- Install PyEnv in Ubuntu to implement multiple versions of Python
- Pyenv website
- pyenv Common build problems
- Pipenv document
- Manage multiple independent Python virtual development environments using Pipenv and PyEnv