内容简介:使用 Pipenv 来管理你的 Python 项目
1. 简介
N 久前,曾经写过一篇文章介绍过 Python 的虚拟环境(virtualenv 相关笔记),不过 virtualenv 使用起来实在是不太友好,所以后来在其基础上又有了 virtualwrapper、pyenv、Pipenv,今天主要介绍一下 Pipenv 这个工具,现在已经成了 官方推荐 的包管理 工具 了。
1.1 为什么需要虚拟环境
使用虚拟环境有很多好处:
- 项目之间的包隔离,比如不同的项目之间需要使用不同版本的包;
- 防止污染系统 Python 环境,因为系统中还是有很多地方需要用到 Python 环境的(macOS 是 2.7),如果你的项目是使用 Python 3+ 的话,可能会带来一些迷之问题;
-
方便其他开发者来部署开发环境,使用虚拟环境可以方便地导出一份 requirements.txt(或者 Pipfile)来供开发者一键安装所需要的包,而不是运行的是否通过
ImportError
来得知缺少安装了什么包。
这里建议为每个 Project 都创建一份虚拟环境。
2. 安装
Pipenv 的安装十分简单:
pip install pipenv
如果你是 Mac 用户的话,也可以使用 Homebrew 来进行安装:
brew install pipenv
3. 基本使用
切换到你的 Project,创建一个 Python 3 环境:
$ pipenv --three Creating a virtualenv for this project… Using /usr/local/bin/python3 to create virtualenv… ⠋Running virtualenv with interpreter /usr/local/bin/python3 Using base prefix '/Library/Frameworks/Python.framework/Versions/3.7' /usr/local/Cellar/pipenv/8.3.2/libexec/lib/python2.7/site-packages/virtualenv.py:1039: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses import imp New python executable in /Users/jiayuan/.local/share/virtualenvs/demo-0922AcvC/bin/python3 Also creating executable in /Users/jiayuan/.local/share/virtualenvs/demo-0922AcvC/bin/python Installing setuptools, pip, wheel...done. Virtualenv location: /Users/jiayuan/.local/share/virtualenvs/demo-0922AcvC Creating a Pipfile for this project…
可以看到,它会使用你系统的 Python 3(在我的机器上是 Python 3.7)版本在 /Users/xxx/.local/share/virtualenvs/
目录下为当前的 Project 创建一个虚拟环境。
如果你想使用 Python 2,可以使用 pipenv --two
来创建。
或者你想要自己制定 Python 的版本,可以使用 pipenv --python 3.5
来进行创建。
创建完虚拟环境后,会在目录下面生成一个 Pipfile:
$ cat Pipfile [[source]] url = "https://pypi.python.org/simple" verify_ssl = true name = "pypi" [dev-packages] [packages] [requires] python_version = "3.7"
在虚拟环境中安装包:
$ pipenv install flask
这会安装 flask 以及相关的依赖包,可以使用 pipenv graph
来查看包的依赖关系:
$ pipenv graph Flask==0.12.2 - click [required: >=2.0, installed: 6.7] - itsdangerous [required: >=0.21, installed: 0.24] - Jinja2 [required: >=2.4, installed: 2.10] - MarkupSafe [required: >=0.23, installed: 1.0] - Werkzeug [required: >=0.7, installed: 0.14.1]
然后使用 pipenv shell
进入虚拟环境:
$ pipenv shell Spawning environment shell (/usr/local/bin/fish). Use 'exit' to leave.
这会创建一个新的 shell 进程,不想使用的时候输入 eixt
就可以退出了。
如果进入了一个新的项目,其中有 requirements.txt
或者 Pipfile
的话,运行 pipenv install
会自动为当前的 Project 创建一个虚拟环境并安装相关依赖。
4. 和 fish shell 一起使用
最近开始把 shell 从 zsh 切换到 fish,发现很多东西都不用配置了,比如想要 cd 进项目的时候就直接激活虚拟环境,只需要在 fish 中安装一个 pipenv 的插件就可以了: fisher pipenv
。具体 fish shell 的使用和配置,打算下篇文章详细讲一下。
感觉这篇博客整篇都在灌水……
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 使用 MonoRepo 管理前端项目
- 使用 Mkdocs 制作项目文档
- 使用Taro开发项目总结
- 使用 Maven 构建 Java 项目
- 使用gradle构建java项目
- flask使用蓝图规划大型项目
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Iterative Methods for Sparse Linear Systems, Second Edition
Yousef Saad / Society for Industrial and Applied Mathematics / 2003-04-30 / USD 102.00
Tremendous progress has been made in the scientific and engineering disciplines regarding the use of iterative methods for linear systems. The size and complexity of linear and nonlinear systems arisi......一起来看看 《Iterative Methods for Sparse Linear Systems, Second Edition》 这本书的介绍吧!
JS 压缩/解压工具
在线压缩/解压 JS 代码
RGB转16进制工具
RGB HEX 互转工具