Introduction to using pipenv

Posted by illushinz on Wed, 08 May 2019 17:36:02 +0200

conda was used as a virtual environment management tool during development, but sometimes it is temporary and necessary.
If you use conda create to create a new environment, it takes a lot of time, sometimes forgot to delete it, and finally a bunch of virtual environments in the system.
After accidentally discovering pipenv, try it out and feel that its workflow adds requirements.txt to the previous virtual environment
The way is simpler and more convenient.

install

pipenv works best with Python 3

The easiest way to install is through pip

pip3 install pipenv

If it is a mac user, it can also be installed via Homebrew

brew install pipenv

If pip is not installed, you can install it as follows

https://raw.githubusercontent.com/kennethreitz/pipenv/master/get-pipenv.py | python

Use

Create a new project

mkdir myproject
pipenv install

This creates the Pipfile and Pipfile.lock files under the myproject file, as well as the virtual environment.
The virtual environment name is pipenv plus the project path hash value, such as pipenv-7BgKv-oX

ll
-rw-r--r-- 1 rookie rookie 138 4 February 20 20:24 Pipfile
-rw-r--r-- 1 rookie rookie 453 4 February 20 20:24 Pipfile.lock

The virtual machine environment defaults to ~/.local/share/virtualenvs/, since pipenv passes a path name hash with the virtual environment
Establish a mapping relationship, so if you move the project, the virtual environment will be unavailable.You can add them in.bashrc
ExpoPIPENV_VENV_IN_PROJECT=1 causes pipenv to create a virtual environment in the project file, which is then located in the myproject/.venv folder.

If the item deletes the virtual environment, run in the top directory of the project

pipenv --rm

If you want to specify a Python version, you can do so through --python, for example, if I want to install a virtual environment for Python 3.7

pipenv --python 3.7

configuration file

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

[dev-packages]

[packages]

[requires]
python_version = "3.6"

[[source]] module specifies the pip source location, usually domestic pip source to speed up download

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

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

[dev-packages]

[packages]

[requires]
python_version = "3.6"

Install and uninstall packages

You can edit packages, add Python libraries needed for your project, such as requests='**', or even specify different
Package Configuration under Platform, Detailed Configuration Reference Advanced Usage of Pipenv
But you can install requests via pipenv
To install, pipenv will automatically update Pipfile and Pipfile.lock.
[dev-packages] are storage directories for development-dependent but not project-dependent packages, such as pep8 checking tools such as pylint
You can specify pipenv install --dev at installation time to declare a package as dev-package
Unloading package s is also easy

pip uninstall

If you install and uninstall the package through the pipenv command, the Pipfile.lock file is also updated after the installation or uninstallation is complete, sometimes stuck in this step.Usually you can force ctrl+c, delete Pipfile.lock, and then

pipenv lock

Regenerate the file

Enter and exit virtual environments

Enter virtual environment

cd myproject

pipenv shell

Exit virtual environment

pipenv exit

Advanced Usage

Sometimes certain package s need to specify a specific pip source, which can be easily specified as follows

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

[[source]]
url = "http://pypi.home.kennethreitz.org/simple"
verify_ssl = false
name = "home"

[dev-packages]

[packages]
requests = {version="*", index="home"}
maya = {version="*", index="pypi"}
records = "*"

Sometimes you need to generate requirements.txt files

pipenv lock -r

It is also simple if you only need to generate a list of dev-pakcages

pipenv lock -r --dev

Automatically load environment variable files.If the.env file is in the project root directory, pipenv shell and pipenv run will
Environment variables in the file are automatically loaded.

$ cat .env
HELLO=WORLD

$ pipenv run python
Loading .env environment variables…
Python 2.7.13 (default, Jul 18 2017, 09:17:00)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['HELLO']
'WORLD'

pipenv also supports custom commands, which are convenient for some commands that are often used in development.
Add [scripts] to Pipfile

[scripts]
printspam = "python -c \"print('I am a silly example, no one would need to do this')\""

Custom commands can be run from pipenv run <custom_command>, such as

pipenv run echospam "indeed"
I am really a very silly example indeed

More advanced users can refer to Official Documents

Finally, welcome to my attention Blog-PrivateRookie

Topics: Python pip shell Mac