pipenv常用命令

官网:https://pipenv.kennethreitz.org/en/latest/

安装

1
pip install pipenv

查看版本

1
pipenv --version

第一个工程项目

1
2
mkdir myweb
cd myweb

初始化python3虚拟环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
myweb>pipenv --python 3
Creating a virtualenv for this project…
Pipfile: myweb\Pipfile
Using D:/python/python3.7.3/python.exe (3.7.3) to create virtualenv…
[== ] Creating virtual environment...Already using interpreter D:\python\python3.7.3\python.exe
Using base prefix 'D:\\python\\python3.7.3'
New python executable in C:\Users\主机名\.virtualenvs\myweb-hLv3G6Vy\Scripts\python.exe
Installing setuptools, pip, wheel...
done.
Running virtualenv with interpreter D:/python/python3.7.3/python.exe

Successfully created virtual environment!
Virtualenv location: C:\Users\主机名\.virtualenvs\myweb-hLv3G6Vy
Creating a Pipfile for this project…

下面是虚拟环境目录

1
C:\Users\主机名\.virtualenvs

查看虚拟环境目录

1
2
myweb>pipenv --venv
C:\Users\主机名\.virtualenvs\myweb-hLv3G6Vy

安装django

安装django(为什么这么慢……),如果没有上面的初始化python3虚拟环境,下面的pipenv install django也会先进行初始化。

1
2
3
4
5
6
7
8
9
10
11
12
13
myweb>pipenv install django
Installing django…
Adding django to Pipfile's [packages]…
Installation Succeeded
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Success!
Updated Pipfile.lock (4f9dd2)!
Installing dependencies from Pipfile.lock (4f9dd2)…
================================ 3/3 - 00:00:12
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

To activate this project’s virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

注意上面的两句话,也就是比如 pipenv run pip list 是查看当前虚拟环境安装的依赖,那么也可以先pipenv shell进入虚拟环境,再输入pip list

激活虚拟环境

1
2
3
4
myweb>pipenv shell
Launching subshell in virtual environment…
Microsoft Windows [版本 10.0.18362.476]
(c) 2019 Microsoft Corporation。保留所有权利。

查看项目内的包

1
myweb>pipenv run pip list
1
2
3
4
5
6
7
8
9
(myweb-hLv3G6Vy) myweb>pip list
Package Version
---------- -------
Django 2.2.7
pip 19.3.1
pytz 2019.3
setuptools 41.6.0
sqlparse 0.3.0
wheel 0.33.6

退出虚拟环境

1
(myweb-hLv3G6Vy) myweb>exit

查看目前安装的库及其依赖关系

1
2
3
4
myweb>pipenv graph
Django==2.2.7
- pytz [required: Any, installed: 2019.3]
- sqlparse [required: Any, installed: 0.3.0]

安装开发专用包

1
myweb>pipenv install --dev requests

运行python文件

1
myweb>pipenv run python main.py

删除虚拟环境

在删除项目前要记得删除虚拟环境,前面有目录。

1
myweb>pipenv --rm

安装所有依赖

如果在有Pipfile的情况下,可以运行下面的命令安装相关的依赖

1
myweb1>pipenv install --dev # 如果不加--dev,就不能安装开发专用包

更换国内源

https://github.com/pypa/pipenv/blob/master/docs/advanced.rst

把下面的url改为清华源就可以,以后安装包就会很快。

1
2
3
4
5
6
7
8
9
10
11
12
[[source]]
name = "pypi"
url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
verify_ssl = true

[dev-packages]

[packages]
requests = "*"

[requires]
python_version = "3.8"

定义执行脚本

在 Pipfile 下写入下面的内容,那么现在 pipenv run python main.py 直接可以简写成 pipenv run startpipenv run list 就可以查看虚拟环境中安装的包。

1
2
3
4
[scripts]
start = "python main.py"
test = "pytest"
list = "pip list"

导入 requirements.txt

If you only have a requirements.txt file available when running pipenv install, pipenv will automatically import the contents of this file and create a Pipfile for you.

You can also specify $ pipenv install -r path/to/requirements.txt to import a requirements file.

上面的文档还是要读的仔细一点,如果已经有了 Pipfile 和 requirements.txt 的话,命令应该是 pipenv install -r requirements.txt ,如果没有 Pipfile 的话(即尚未初始化虚拟环境),可以使用 pipenv install

生成 requirements.txt

1
2
3
4
5
6
# 生成requirements.txt文件 generate a requirements.txt out of it:
$ pipenv lock -r

# 生成dev-packages的requirements.txt文件
# If you wish to generate a requirements.txt with only the development requirements you can do that too!
$ pipenv lock -r -d