Ubuntu 16.04系统默认有Python 2.7.12和Python 3.5.2
以在目录[/usr/ccdocs
]下创建项目文件夹[myproject
]为例。
Pipenv: The Future of Python Dependency Management
Pipenv,其项目简介为 Python Development Workflow for Humans,是 Python 著名的 requests 库作者 kennethreitz 写的一个包管理工具,它可为我们的项目自动创建和管理虚拟环境并非常方便地管理 Python 包,现在它也是 Python 官方推荐的包管理工具。
Pipenv在各个平台(Window/iOS/Linux)命令是一样的。
一、创建、并进入虚拟环境
终端(Terminal)里输入如下命令:
cd /usr/ccdocs
sudo mkdir myproject
sudo chmod 777 myproject
pipenv --three #为当前目录 创建Python3虚拟环境,pipenv install 会默认使用Python2(在没有配置默认Python2的情况下)
pipenv --two #为当前目录 创建Python2虚拟环境
示例如下:
1)、用pipenv安装Python 2 虚拟环境
chenchen@chenchen-ubuntu:/usr/ccdocs/myproject$ pipenv install #这和pipenv --two是一样的
Creating a virtualenv for this project…
Pipfile: /usr/ccdocs/myproject/Pipfile
Using /usr/bin/python (2.7.12) to create virtualenv…
⠦Already using interpreter /usr/bin/python
New python executable in /home/chenchen/.local/share/virtualenvs/myproject-f5Hwi5Rz/bin/python
Installing setuptools, pip, wheel...
done.
Virtualenv location: /home/chenchen/.local/share/virtualenvs/myproject-f5Hwi5Rz
Creating a Pipfile for this project…
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (dfae9f)!
Installing dependencies from Pipfile.lock (dfae9f)…
? ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
当前目录下会新增分别名为Pipfile
、Pipfile.lock
两个文件:
chenchen@chenchen-ubuntu:/usr/ccdocs/myproject$ ll
总用量 16
drwxrwxrwx 2 root root 4096 11月 14 16:05 ./
drwxr-xr-x 4 root root 4096 11月 14 15:58 ../
-rw-rw-r-- 1 chenchen chenchen 138 11月 14 16:05 Pipfile
-rw-r--r-- 1 chenchen chenchen 453 11月 14 16:05 Pipfile.lock
2)、用pipenv安装Python 3 虚拟环境
chenchen@chenchen-ubuntu:/usr/ccdocs/myproject$ pipenv --three
Creating a virtualenv for this project…
Pipfile: /usr/ccdocs/myproject/Pipfile
Using /usr/bin/python3.5 (3.5.2) to create virtualenv…
⠼Running virtualenv with interpreter /usr/bin/python3.5
Using base prefix '/usr'
New python executable in /home/chenchen/.local/share/virtualenvs/myproject-f5Hwi5Rz/bin/python3.5
Also creating executable in /home/chenchen/.local/share/virtualenvs/myproject-f5Hwi5Rz/bin/python
Installing setuptools, pip, wheel...
done.
Virtualenv location: /home/chenchen/.local/share/virtualenvs/myproject-f5Hwi5Rz
Creating a Pipfile for this project…
当前目录下会新增名为Pipfile的文件:
chenchen@chenchen-ubuntu:/usr/ccdocs/myproject$ ll
总用量 12
drwxrwxrwx 2 root root 4096 11月 14 16:49 ./
drwxr-xr-x 4 root root 4096 11月 14 15:58 ../
-rw-rw-r-- 1 chenchen chenchen 138 11月 14 16:49 Pipfile
上述两个命令小节:
Virtualenv location: /home/chenchen/.local/share/virtualenvs/myproject-f5Hwi5Rz
,虚拟环境名由 当前文件夹名称-当前路径标识符
组成。还会告知Python解释器所在位置。Pipfile
文件用于记录虚拟环境安装的软件包版本。作用类似于requirements.txt文件,但作用更强大。Pipfile.lock
文件顾名思义是起到版本锁作用,用于避免版本更新导致不兼容的问题。它详细标识了该项目的安装的包的精确版本信息、最新可用版本信息和当前库文件的hash
值。换句话说是:冻结软件包名称及其版本,以及其依赖关系的列表。Pipfile
文件,那么文中标识的依赖若为<包名>="*"
,意味着没有版本限制,将会默认安装最新版本的包。Pipfile.lock
文件,那么将根据Pipfile.lock
进行安装指定版本的包。切勿手动修改Pipfile.lock
文件。pipenv lock
命令生成。chenchen@chenchen-ubuntu:/usr/ccdocs/myproject$ pipenv lock
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (a79791)!
chenchen@chenchen-ubuntu:/usr/ccdocs/myproject$ ll
总用量 16
drwxrwxrwx 2 root root 4096 11月 14 17:16 ./
drwxr-xr-x 4 root root 4096 11月 14 15:58 ../
-rw-rw-r-- 1 chenchen chenchen 138 11月 14 16:49 Pipfile
-rw-r--r-- 1 chenchen chenchen 453 11月 14 17:16 Pipfile.lock
二、激活、退出虚拟环境
1、激活虚拟环境,在项目所在目录下,终端输入:pipenv shell
chenchen@chenchen-ubuntu:/usr/ccdocs/myproject$ . /home/chenchen/.local/share/virtualenvs/myproject-f5Hwi5Rz/bin/activate
(myproject) chenchen@chenchen-ubuntu:/usr/ccdocs/myproject$
PS:激活后,项目文件夹名称 将出现在前面。
2、退出虚拟环境:exit
(myproject) chenchen@chenchen-ubuntu:/usr/ccdocs/myproject$ exit
exit
chenchen@chenchen-ubuntu:/usr/ccdocs/myproject$
三、在虚拟环境中管理安装包
1、在clone了有Pipfile
(和Pipfile.lock
)的项目目录下,安装Pipfile.lock
上所有包(完整移植开发环境),可使用名:pipenv install
或pipenv sync
。
pipenv install
命令会自动找到Pipfile
文件,创建一个新的虚拟环境并安装必要的软件包。
2、安装指定软件包,多个以空格作为分隔符:pipenv install <软件包名1> <软件包名2>
(myproject) chenchen@chenchen-ubuntu:/usr/ccdocs/myproject$ pipenv install flask
Installing flask…
Collecting flask
Using cached https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl
Collecting itsdangerous>=0.24 (from flask)
Using cached https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Werkzeug>=0.14 (from flask)
Using cached https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl
Collecting Jinja2>=2.10 (from flask)
Using cached https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl
Collecting click>=5.1 (from flask)
Using cached https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->flask)
Using cached https://files.pythonhosted.org/packages/3e/a5/e188980ef1d0a4e0788b5143ea933f9afd760df38fec4c0b72b5ae3060c8/MarkupSafe-1.1.0-cp35-cp35m-manylinux1_x86_64.whl
Installing collected packages: itsdangerous, Werkzeug, MarkupSafe, Jinja2, click, flask
Successfully installed Jinja2-2.10 MarkupSafe-1.1.0 Werkzeug-0.14.1 click-7.0 flask-1.0.2 itsdangerous-1.1.0
Adding flask to Pipfile's [packages]…
Pipfile.lock (e239e5) out of date, updating to (a79791)…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (e239e5)!
Installing dependencies from Pipfile.lock (e239e5)…
? ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 6/6 — 00:00:01
(myproject) chenchen@chenchen-ubuntu:/usr/ccdocs/myproject$
如果某些Python包只在开发环境中需要,而在生产环境中不需要,Pipenv可使用--dev
标志保持两个环境分开:
pipenv install --dev <软件包名>
3、查看已安装软件包,命令:pipenv graph
(myproject) chenchen@chenchen-ubuntu:/usr/ccdocs/myproject$ pipenv graph
Flask==1.0.2
- click [required: >=5.1, installed: 7.0]
- itsdangerous [required: >=0.24, installed: 1.1.0]
- Jinja2 [required: >=2.10, installed: 2.10]
- MarkupSafe [required: >=0.23, installed: 1.1.0]
- Werkzeug [required: >=0.14, installed: 0.14.1]
(myproject) chenchen@chenchen-ubuntu:/usr/ccdocs/myproject$
4、删除某指定软件包,命令:pipenv uninstall <软件包名>
5、运行py文件,命令:pipenv run python[选择python2或3]
四、删除虚拟环境
在项目所在目录下,终端输入:pipenv --rm
chenchen@chenchen-ubuntu:/usr/ccdocs/myproject$ pipenv --rm
Removing virtualenv (/home/chenchen/.local/share/virtualenvs/myproject-f5Hwi5Rz)…
但是,目录下的Pipfile
、Pipfile.lock
会保留。只是删除虚拟环境而已。
参考文档:
1、官方
2、大佬博客