Python project development local virtual environment management scheme on Ubuntu system: pyenv + pipenv

Time:2020-5-22

1. Overview

This paper introduces the management of local virtual environment of Python project development with pyenv + pipenv.

  • Pyenv: installs and manages multiple versions of Python.
  • Pipenv: create a separate virtual environment for each project.

All of the following operations are performed on Ubuntu 16.04 system.

2. Python version management: pyenv

2.1. Install pyenv

$ curl https://pyenv.run | bash

Pyenv related content will be installed in~/.pyenv/Directory.

After installation, follow the prompts to add the following to the~/.bashrc:

export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Upgrade pyenv:

$ pyenv update

Delete pyenv:

$ rm -rf ~/.pyenv

And delete the relevant environment variables in ~ /. Bashrc.

2.2. Install and manage multiple Python

To view installable versions:

$ pyenv install --list

Install the specified version:

$ pyenv install 3.8.2

Before installing python, install the dependency package 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

See common build problems, otherwise it will appear when importing some Python libraries after compilationModuleNotFoundError: No module named '_sqlite3'And so on.

To view the currently installed version of Python:

To view the currently installed version of Python:

$ pyenv versions
* system (set by /home/hy/.pyenv/version)
  3.8.2

All Python versions installed through pyenv are saved in~/.pyenv/versions/Directory.

2.3. Each directory can be specified to execute a specific Python version

Before it 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.
>>> 

adoptpyenv localThe command specifies the python version to execute when executing Python in this directory:

$ pyenv local 3.8.2

$ ls -la
total 12
Drwxrwxr-x 2hy hy 4096 March 10 16:04
Drwxrwxr-x 42 hy hy 4096 March 10 13:02
-Rw-rw-r -- 1 hy hy 6 March 10 16:03. Python version

$ cat .python-version 
3.8.2

localThe command generates a hidden file with version number in the current directory.python-version

Verify the executed Python version:

$ 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.
>>> 

2.4. Switch the global Python version

$ 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.
>>> 

3. Virtual environment management: pipenv

3.1. Install pipenv

Make sure the latest 3. X versions of Python and pip are installed

$ python -V
Python 3.8.2

$ pip -V
pip 19.2.3 from /home/hy/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pip (python 3.8)

Installation:

$ pip install pipenv

Upgrade:

$ pip install --upgrade pipenv

3.2. Create a separate virtual environment for each project

Create project directory:

$ mkdir django_test && cd django_test

takeexport PIPENV_VENV_IN_PROJECT=1Add to~/.bashrcFor the configuration to take effectsource ~/.bashrcAfter that, the virtual environment managed by pipenv will be installed in the root directory of the project.venvDirectory.

To create a virtual environment:

$ pipenv --python 3.8

Creating a virtualenv for this 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-64 in 178ms
  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 Pipfile for this project…

$ ls -la
total 16
Drwxrwxr-x 3 hy hy 4096, March 11, 12:15
Drwxrwxr-x 42 hy hy 4096 March 10 13:02
-Rw-rw-r -- 1 hy hy 138 March 11 12:15 pipfile
Drwxrwxr-x 4 hy hy 4096 March 11 12:15.venv

Where automatically generatedPipfileThe URL of the pypi source is saved in the build:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]

[requires]
python_version = "3.8"

You can set the source URL to the domestic image address to improve the download speed:

[[source]]
name = "pypi"
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
verify_ssl = true

[dev-packages]

[packages]

[requires]
python_version = "3.8"

Install dependency package:

$ 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.

Install the dependency package in the test environment:

$ pipenv install pytest --dev

Show installed dependency 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]

To delete a dependent package:

$ 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)!

Enter the virtual environment:

$ pipenv shell

4. Project management

$ ls -la
total 20
Drwxrwxr-x 3 hy hy 4096, March 11, 12:31
Drwxrwxr-x 42 hy hy 4096 March 10 13:02
-Rw-rw-r -- 1 hy hy 185 March 11 12:31 pipfile
-Rw-r -- R -- 1 hy hy 3666 March 11 12:31 Pipfile.lock
Drwxrwxr-x 5 hy hy 4096 March 11 12:21.venv

Will be automatically generatedPipfileandPipfile.lockDocuments are added to version control system,.venvDo not add directory to version control system.

Team members install pyenv and pipenv in~/.bashrcConfigure the corresponding environment variables in, clone project source code, runpipenv install --devThe virtual development environment can be rebuilt.

$ cd django_test
$ pipenv install --dev

resources

  • Installing pyenv under Ubuntu to realize the coexistence of multiple versions of Python
  • Pyenv official website
  • pyenv Common build problems
  • Pipenv document
  • Using pipenv and pyenv to manage multiple independent Python virtual development environments