Quantcast
Channel: 看得透又看得远者prevail. ppt.cc/flUmLx ppt.cc/fqtgqx ppt.cc/fZsXUx ppt.cc/fhWnZx ppt.cc/fnrkVx ppt.cc/f2CBVx
Viewing all articles
Browse latest Browse all 20537

Pipenv的使用

$
0
0

解决问题

过去一般用 virtualenv 创建虚拟环境,不想让依赖和项目关联在一起。建立 requirements.txt文件,一个用于开发环境的 requirements.txt,一个用于生产环境的 requirements.txt。这些步骤十分繁琐,因此 Kenneth Reitz’s开发了 Pipenv来简化Python 项目的依赖管理流程。
包含有 PipPipfileVirtualenv,还有强大的命令行工具。

开始使用

安装 pipenv 及其依赖
$ pip install pipenv
初始化 pipenv
pipenv 默认把虚拟环境的真实文件全都放在 %userprofile%\.virtualenvs\,Linux 则是在 ~/.virtualenvs下。项目文件夹内只创建了两个新文件 Pipfile Pipfile.lock
$ cd my_project
$ pipenv install
这一步会创建两个文件,PipfilePipfile.lock
$ pipenv --three # 初始化 Python 3
$ pipenv --two # 初始化 Python 2
$ pipenv --python 3.6 # 指定 Python 3.6 版本初始化
Pipfile配置的源为官方源,速度太慢,建议换成国内的源。
[[source]]
url = "https://mirrors.aliyun.com/pypi/simple"

管理依赖

Pipfile包含了项目依赖,用于取代 requirements.txt
  • 安装 Python 包
    $ pipenv install beautifulsoup4
  • 卸载 Python 包
    $ pipenv uninstall beautifulsoup4
  • 通过更新 Pipfile.lock文件锁定包的版本和依赖
    $ pipenv lock
  • 建议把 Pipfile添加到 Git 仓库,这样他人也可以克隆该文件,然后只需要输入 pipenv installpipenv就会自动搜寻 Pipfiles文件并创建虚拟环境,安装所需的包依赖。

管理虚拟环境

通常有些包只在开发环境中需要,在生产环境并不需要,比如 unit testing包,可以 --dev分离两个环境。
$ pipenv install --dev nose2
上面操作会安装 nose2,但是会将其关联到开发环境。
如果你在生产环境输入pipenv install,默认 nose2并不会安装,当另外一个开发者克隆了你的项目,然后输入 pipenv install --dev,所有的依赖都会被安装,包括开发包。

运行代码

为了激活和 Python 项目关联的虚拟环境,你输入
$ pipenv shell
在虚拟环境中调用系统 shell 命令,如查看运行的python 程序所在目录
$ pipenv run which python
在虚拟环境中运行 Python 代码
$ pipenv run python my_project.py
如果你嫌命令太长,可以设置别名
$ alias prp="pipenv run python"

自动补全

For example, with fish, put this in your ~/.config/fish/completions/pipenv.fish:
eval(pipenv --completion)
Alternatively, with bash, put this in your .bashrc or .bash_profile:
eval"$(pipenv --completion)"

其他命令

$ pipenv
Usage: pipenv [OPTIONS] COMMAND [ARGS]...

Options:
--where Output project home information.
--venv Output virtualenv information.
--py Output Python interpreter information.
--envs Output Environment Variable options.
--rm Remove the virtualenv.
--bare Minimal output.
--completion Output completion (to be eval'd).
--man Display manpage.
--three / --two Use Python 3/2 when creating virtualenv.
--python TEXT Specify which version of Python virtualenv should use.
--site-packages Enable site-packages for the virtualenv.
--version Show the version and exit.
-h, --help Show this message and exit.


Usage Examples:
Create a new project using Python 3.6, specifically:
$ pipenv --python 3.6

Install all dependencies for a project (including dev):
$ pipenv install --dev

Create a lockfile containing pre-releases:
$ pipenv lock --pre

Show a graph of your installed dependencies:
$ pipenv graph

Check your installed dependencies for security vulnerabilities:
$ pipenv check

Install a local setup.py into your virtual environment/Pipfile:
$ pipenv install -e .

Use a lower-level pip command:
$ pipenv run pip freeze

Commands:
check Checks for security vulnerabilities and against PEP 508 markers
provided in Pipfile.
clean Uninstalls all packages not specified in Pipfile.lock.
graph Displays currently–installed dependency graph information.
install Installs provided packages and adds them to Pipfile, or (if none
is given), installs all packages.
lock Generates Pipfile.lock.
open View a given module in your editor.
run Spawns a command installed into the virtualenv.
shell Spawns a shell within the virtualenv.
sync Installs all packages specified in Pipfile.lock.
uninstall Un-installs a provided package and removes it from Pipfile.

遇到问题

  1. 低版本 Python,如 Python 3.4 直接安装会出错
    error in pipenv setup command: 'install_requires' must be a string or list of strings containing valid project/version requirement specifiers
    解决方法:先升级 setuptools
    pip3 install --upgrade setuptools
    pip3 install pipenv

Viewing all articles
Browse latest Browse all 20537

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>