Translator’s Comments:
Software developers will never stop pursuing engineering environment and development tools. As development elements change, tools evolve and innovate to meet new needs and solve problems. The discovery/understanding/adoption of new tools is repeated. We definitely need to consider the cost of switching tools, but we can’t stay stuck in the old ways. This has to be self-evaluated and balanced.
In this article, control of a Python project is divided into two parts: the Python interpreter is given to PyEnv, and the Python packages used in the project are given to Pipenv. It’s a really good choice.
This is a highly recommended way to develop locally in Python. As you may have noticed, different projects have different versions of Python dependencies, which can be cumbersome to manage.
To complicate matters further, there are several ways to install Python:
- The operating system is preinstalled
- Use a package manager such as Brew or APT
- Use the binaries at www.python.org
- Install and manage the Python installation process using the Pyenv-Easy method π
This guide uses PyEnv to manage Python installations and Pipenv to manage project dependencies (rather than the original PIP).
Install pyenv
Let’s install via BREW:
$ brew install pyenv
If you’re not on a Mac, see the Installation instructions for PyEnv.
Add the following to ~ /.bash_profile or ~ /.bashrc (depending on your shell) to automatically initialize Pyenv when the terminal loads:
The eval “$(pyenv init -)”
Pyenv has an automatic installer that makes it easier to use.
How does PyEnv work?
View all available Python versions:
$pyenv install – the list
Let’s install Python 3.6.6
$pyenv install 3.6.6 Installed Python – 3.6.6 to/Users/DVF /. Pyenv/versions / 3.6.6
Pyenv does not change your global interpreter unless you tell it:
$python version
Python 2.7.14
$pyenv global 3.6.6
Python 3.6.6
Pyenv allows you to install different versions of Python natively into one directory. Let’s create a project for Python 3.7.0:
$pyenv install 3.7.0
Installed Python – 3.7.0 to/Users/DVF /. Pyenv/versions / 3.7.0
$ mkdir my_project && cd my_project
$python version
Python 3.6.6
$pyenv local 3.7.0
$python version
Python 3.7.0
Now, whenever you find yourself in my_project, you will automatically use the Python 3.7.0 interpreter.
Get it? If not, stop here and spend some time tinkering with Pyenv – it works by installing all Python interpreters in ~ /.pyenv and dynamically adjusting your $PATH based on your current directory.
What is Pipenv? How does it work?
Pipenv is the official recommended way to manage project dependencies. Instead of having requirements.txt files and managing virtualenv in your project, you now have a Pipfile file in your project that does all of this automatically.
Install it first via PIP, this is a fast moving project, so make sure you have the latest version (as of this writing, 10.13, 2018.13) :
pip install -U pipenv
Using Pipenv for the first time Let us set up Pipenv in your project:
$ cd my_project
$ pipenv install
Creating a Virtualenv for this project…
Pipfile: /Users/dvf/my_project/Pipfile
Using/Users/DVF /. Pyenv/versions / 3.7.0 / bin/python3.7 (3.7.0) to create virtualenv…
You will find two new files in the project: Pipfile and pipfile.lock.
If you install in an existing project, Pipenv will convert your old requirements.txt file into a Pip file. Cool enough? For a new project, the PipFile should look like this:
[[source]]
Url = “https://pypi.org/simple”
verify_ssl = true
Name = “pypi”
[packages]
[dev-packages]
[requires]
Python_version = “3.7”
Please note that we don’t have any virtual environments active here, and Pipenv handles the virtual environments for us. Therefore, installing a new dependency is simple:
$ pipenv install django
Installing django
…
Installing collected packages: pytz, django
Successfully installed django – 2.1.2 pytz – 2018.5
Adding Django to Pipfile’s [packages]…
Pipfile.lock (4f9dd2) out of date, updating to (a65489)…
Locking/dev – packages dependencies…
Locking/packages dependencies…
Updated Pipfile.lock (4f9dd2)!
Installing dependencies from pipfile.lock (4f9DD2)…
π β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β 2/2-00:00:01
To activate this projectβs virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
If you examine the Pipfile, you’ll see that it now contains Django = “*” as a dependency.
If we want to install dev dependencies for use during development, such as YAPF, we need to add -dev to the installation step:
$ pipenv install βdev yapf
What is the pipfile.lock file?
The pipfile.lock file is important because it does two things:
Provides good security by maintaining the hash value of each installed package. Introduce versions of all dependencies and child dependencies to give you a replicable environment. Let’s take a look at what it looks like today:
{ “_meta”: { “hash”: { “sha256”: “627ef89… 64 f9dd2 “}, “pipfile – spec” : 6, “requires” : {” python_version “:” 3.7 “}, “sources” : [{” name “:” pypi “, “url” : “https://pypi.org/simple”, “verify_ssl”: true } ] }, “default”: { “django”: { “hashes”: [” sha256: ab5bb3 acdcc1… “, “sha256: efbcad… d16b45]”, “index” : “pypi”, “version” : “= = 2.1.2”}, “pytz” : {” hashes “: [” sha256: a061aa… 669053 “, “sha256: ffb9ef… 2 bf277]”, “version” : “= = 2018.5}}”, “develop” : {}}
Note that the version of each dependency is fixed. You will always want to submit this file to your source code management system without a good reason.
Custom index
Prior to the advent of Pipenv, it was difficult to use private Python repositories. For example, you want to host a private Python repository within your organization. Now all you need to do is define them as additional sources in your Pipfile:
[[source]]
Url = “https://pypi.org/simple”
verify_ssl = true
Name = “pypi”
[[source]]
Url = “https://www.example.com”
verify_ssl = true
Name = “some – ’08 – name”
[packages]
Django = “*”
My – private – app = {version = “*”, some index = “repo – name”}
[dev-packages]
[requires]
Python_version = “3.7”
Note that we told my-private-app to use a private repository. If omitted, Pipenv traverses the index until the package is found.
π‘Pipenv will also use any environment variables in the value, which can be very useful if you have sensitive credentials that you don’t want to put in your source code management system (this is my contribution).
At deployment time, it is important to know that the deployment failed if the installed dependencies do not match pipfile.lock. So you should add — deploy to your installation steps, like this:
$pipenv install, deploy
You can also check which dependencies don’t match:
$ pipenv check
And see what subdependencies these packages have installed:
$ pipenv graph βreverse
PIP = = 18.1
Pytz = = 2018.5
– Django = = 2.1.2 [the requires: pytz]
Setuptools = = 40.4.3
Wheel = = 0.32.2
Yapf = = 0.24.0
If you are actively working on a project, activating a virtual environment can be helpful:
$ pipenv shell
Launching subshell in virtual Environment…
(my_project) β my_project
Or, if you want to execute commands in a virtual environment:
$ pipenv run python manage.py runserver
You can also add a script like NPM package.json to your pipfile:
[[source]]
Url = “https://pypi.org/simple”
verify_ssl = true
Name = “pypi”
[packages]
Django = “*”
[dev-packages]
Yapf = “*”
[scripts]
Server = “Python manage.py runserver”
[requires]
Python_version = “3.7”
Now you can execute the script:
$ pipenv run server
We have just touched the tip of the iceberg. If you want to learn more about Pen V, I recommend reading its excellent documentation.
The translator blog