Requests作者Kenneth Reitz的另一个作品。

Pipenv is a dependency manager for Python projects. While pip can install python packages, Pipenv is recommanded as it’s a higher-level tool that simplifies dependenvy management for common use cases.

自动为 project 创建virtualenv。安装卸载包的时候会同步更新最新的包信息到Pipfile中。还会生成Pipfile.lock,其中包含了所有安装包和安装包的依赖,版本信息,hash 值等。

需要的依赖:Python & pip

安装Pipenv

pip install --user pipenv # 安装到user下

基本使用

创建虚拟环境

  • pipenv
  • pipenv --python 3:使用python3创建

卸载虚拟环境

  • 在项目目录中使用pipenv --rm直接删除
  • 也可以找到virtualenv文件夹的path,直接删文件夹呀

安装包

  1. 使用pipenv自动安装。在目录中使用pipenv install PACKAGE_NAME时会自动创建虚拟环境。
  2. 使用requirements.txt、Pipfile、Pipfile.lock文件安装包
    • requirements.txt文件安装:pipenv install -r requirements.txt(如果只有 requirements.txt 文件,pipenv install会自动从 requirements.txt 文件加载并生成 Pipfile)
    • Pipfile文件安装:pipenv install
    • Pipfile.lock文件安装:pipenv install --ignore-pipfile

部分参数说明:

  • --two / three:使用系统的 python2 或者 3
  • --dev:install both develop and default packages from Pipfile.lock。如我的pylint包就安装在dev环境中
  • --system - Use the system pip command rather than the one from your virtualenv.
  • --ignore-pipfile — Ignore the Pipfile and install from the Pipfile.lock.
  • --skip-lock — Ignore the Pipfile.lock and install from the Pipfile. In addition, do not write out a Pipfile.lock reflecting changes to the Pipfile.

卸载包

pipenv uninstall

参数说明(也支持pipenv isntall的参数):

  • --all — This parameter will purge all files from the virtual environment, but leave the Pipfile untouched.
  • --all-dev — This parameter will remove all of the development packages from the virtual environment, and remove them from the Pipfile.

在虚拟环境中运行 Python

  • pipenv run python PYTHON_FILE.py
  • pipenv shell: 先进入虚拟环境后,在运行

查看信息

  • pipenv --py: 查看python interpreter
  • pipenv --venv:查看虚拟环境的路径
  • pipenv graph:查看安装的包信息
  • pipenv --where:locate the project

Pipenv workflow

$ pipenv install                  #  1. 如果有常用的包文件,就从文件安装
$ pipenv install <package>        #  2. 安装新的包
$ pipenv lock                     #  3. 生成Pipfile.lock文件
$ pipenv install --ignore-pipfile #  4. 再新的环境中部署时直接使用Pipfile.lock文件

Advanced Usage of Pipenv

其实基本使用直接在命令行里面使用pipenv命令查看使用help查看。

在IDE中使用

无论是PyCharm中还是VSCode中我都会为每一个项目配置自己的虚拟环境。

  • PyCharm在Project Interpreter那里配置virtualenv environment就可行了。
  • VSCode在Settings里面配置python.pythonPath(如果没有记错的话)。

参考资料: