Python environment management with Pipenv

Hello my friend!


I will assume that pip and virtualenv are still used to control Python environment in your project.


If so, then let me tell you about a tool like Pipenv.


Pipenv is a modern tool for managing your Python desktop environment.


Main features of pipenv:



As an illustrative example, let's compare the use of pip and virtualenv with pipenv to create a virtual environment:


  1. pip and virtualenv


    $ virtualenv venv $ source venv/bin/activte $ pip install Flask gunicorn $ pip freeze > requirements.txt 

  2. pipenv


     $ pipenv install Flask gunicorn 


Beginning of work


Install the latest version of pipenv:


 $ pip install pipenv 

Work Environment Management


Go to the directory with the Python project and create a virtual environment specifying the version of the interpreter:


 $ cd yourproject $ pipenv --python 3.7 

The team will automatically create a new virtual environment for your project, if it does not already exist.


You can activate a virtual project environment by running the shell command:


 $ pipenv shell 

Exit the virtual environment shell using the exit :


 $ exit 

Application dependency management


To install packages, use the install command:


 $ pipenv install Flask 

Pipenv installs the latest version of Flask and automatically adds it to Pipfile.


During installation, we can specify a specific version of the package:


 $ pipenv install Flask==1.0.2 

The developers of this tool have taken care of working with the development environment - packages that are necessary at the stage of building or testing applications.


If you specify the --dev flag, the package will be installed as part of the development environment:


 $ pipenv install pytest --dev 

To install all the packages, including the development environment packages, you must run:


 $ pipenv install --dev 

To uninstall packages, there is an uninstall command:


 $ pipenv uninstall Flask 

Information about installed packages and their dependencies is stored in the Pipfile.lock file, which is automatically generated and should not be changed by the user.


Run source code


It is possible to run source code inside the virtualenv shell:


 $ pipenv run python yourapplication.py 

If there is an .env file, the $ pipenv shell and $ pipenv run commands will automatically load environment variables from it:


 $ cat .env DEBUG=1 $ echo $DEBUG 1 

Deploying the application


Pipenv allows you to install dependencies in the parent system by specifying the --system flag:


 $ pipenv install --system 

This is useful when deploying applications in Docker.


If you specify the --deploy flag, pipenv will give an error if Pipfile.lock is outdated or the Python version does not match the specified one.


Example of Dockerfile for working with pipenv:


 FROM python:3.7 RUN pip3 install pipenv WORKDIR /usr/src/app COPY Pipfile ./ COPY Pipfile.lock ./ RUN set -ex && pipenv install --deploy --system COPY . . 

A sample application on Flask using the features of Pipenv: github.com/fdhadzh/flask-pipenv-example .

Source: https://habr.com/ru/post/413009/


All Articles