Skip to content

Instantly share code, notes, and snippets.

@Xarrow
Last active August 14, 2019 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xarrow/7db0bdf81a94ff7ae7a1ea78a7ca2eb7 to your computer and use it in GitHub Desktop.
Save Xarrow/7db0bdf81a94ff7ae7a1ea78a7ca2eb7 to your computer and use it in GitHub Desktop.
pipenv

前提

  • 基于 Python3.7 环境

安装

  • pip 方式

    pip install pipenv

  • brew 方式

    brew install pipenv

快速使用

  • 创建环境

    使用 Python3 环境

    pipenv --python 3

    指定 Python 版本

    pipenv --python 3.6

    pipenv --python 2.7.14

  • 查看环境信息

    pipenv --venv

  • 查看环境路径

    pipenv --where

  • 查看环境变量

    pipenv --envs

  • 进入虚拟环境

    pipenv shell

  • 安装依赖,项目根目录下更新 Pipfile(依赖版本管理)Pipfile.lock(依赖变化记录,类似做文件事务管理,一般可以忽略) 文件

    pipenv install [package names]

    pipenv install requests~=1.2

    跳过 Pipfile.lock 安装速度会快些

    pipenv install requests~=1.2 --skip-lock

    参考:pipenv-install

  • 查看依赖

    $ pipenv graph
    
    #requests==2.18.4
    #  - certifi [required: >=2017.4.17, installed: 2017.7.27.1]
    #  - chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]
    #  - idna [required: >=2.5,<2.7, installed: 2.6]
    #  - urllib3 [required: <1.23,>=1.21.1, installed: 1.22]

    显示简要依赖

    pipenv list

  • 如果原有 pipenv 项目,可以直接执行,为项目按照全部依赖(包含 dev 版本)

    pipenv install --dev

  • 依赖安全漏洞检查

    pipenv check

  • 显示过期依赖

    pipenv update --outdated

  • 更新过期依赖

    pipenv update 全部更新

    pipenv update <pkg> 只更新某一个

  • 卸载没有用到的依赖

    pipenv clean

  • 卸载依赖

    pipenv uninstall [package names]

  • 退出虚拟环境

    exit

  • 删除环境,会直接删除虚拟环境目录

    pipenv --rm

Pipfile 文件

```python
# 依赖下载源
[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

# 指定依赖版本
[packages]
requests = "*"


[dev-packages]
pytest = "*"

# Python 环境
[requires]
python_version = "3.6"
```

兼容 requirements.txt

  • 对于项目更目录下存在 requirements.txt,直接运行

    pipenv install

  • 生成 requirements.txt , 然后删除生成后的文件第一行。

    pipenv lock -r > requirements.txt

    或者

    pipenv run pip freeze > requirements.txt

部署

  • 在项目根目录下执行 pipenv install --skip-lock && pipenv run python main.py

针对国内

  • 修改 Pipfile 中依赖源,可以存在多个源。

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

参考

Basic Usage of Pipenv

@asdf2014
Copy link

厉害了 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment