pipenv 项目管理
1. 简介
Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. Windows is a first--class citizen, in our world.
pipenv 是更佳便捷的Python项目管理工具.
1.1 网址
1.2 优点
- You no longer need to use
pip
andvirtualenv
separately. They work together. - Managing a
requirements.txt
file can be problematic, so Pipenv uses the upcomingPipfile
andPipfile.lock
instead, which is superior for basic use cases. - Hashes are used everywhere, always. Security. Automatically expose security vulnerabilities.
- Give you insight into your dependency graph (e.g.
$ pipenv graph
). - Streamline development workflow by loading
.env
files.
1.3 好处
- Enables truly deterministic builds, while easily specifying only what you want.
- Generates and checks file hashes for locked dependencies.
- Automatically install required Pythons, if
pyenv
is available. - Automatically finds your project home, recursively, by looking for a
Pipfile
. - Automatically generates a
Pipfile
, if one doesn't exist. - Automatically creates a virtualenv in a standard location.
- Automatically adds/removes packages to a
Pipfile
when they are un/installed. - Automatically loads
.env
files, if they exist
2. 使用
2.1 安装
这里我们可以直接使用pip进行安装
pip install pipenv
pip3 install pipenv
2.2 查看
pipenv --version
2.3 工程化
首先我们需要创建一个 目录
mkdir site
然后 进入这个 目录
cd site
创建一个 工程
pipenv --python 3
pipenv --three
都 0202
年了 py2 就不贴了
当我们执行这条命令后 会自动创建一个 python3 的虚拟环境
2.3 查看目录
我们 创建好目录后 如果要 查看对应虚拟环境目录 可以使用 venv
命令
pipenv --venv
C:\Users\LuZhenFang\Desktop\blog>pipenv --venv
C:\Users\LuZhenFang\.virtualenvs\blog-cqxptZsF
2.4 查看依赖
使用 graph
可以查看当前安装的库 和依赖
pipenv graph
C:\Users\LuZhenFang\Desktop\blog>pipenv graph
Flask==1.1.1
- click [required: >=5.1, installed: 7.0]
- itsdangerous [required: >=0.24, installed: 1.1.0]
- Jinja2 [required: >=2.10.1, installed: 2.10.3]
- MarkupSafe [required: >=0.23, installed: 1.1.1]
- Werkzeug [required: >=0.15, installed: 0.16.0]
C:\Users\LuZhenFang\Desktop\blog>
2.4 开发环境
当我们 要使用 某个库在开发环境
中 时 我们可以 添加 后缀dev
pipenv install --dev requests
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
requests = "*"
[packages]
flask = "*"
[requires]
python_version = "3.7"
3. 部署
3.1 执行
执行文件 我们可以使用 run
命令
pipenv run python main.py
[root@iZ2ze9hn3hwr4e9xprmt32Z test]# pipenv run python
a.py
hello world
[root@iZ2ze9hn3hwr4e9xprmt32Z test]#
3.2 查看
查看安装库 我们可以使用 list
命令
pipenv run pip list
C:\Users\LuZhenFang\Desktop\blog>pipenv run pip list
Package Version
------------ ----------
certifi 2019.11.28
chardet 3.0.4
Click 7.0
Flask 1.1.1
idna 2.8
itsdangerous 1.1.0
Jinja2 2.10.3
MarkupSafe 1.1.1
pip 19.3.1
requests 2.22.0
setuptools 44.0.0
urllib3 1.25.7
Werkzeug 0.16.0
wheel 0.33.6
3.3 Pipfile
建立好的 pipfile 文件 直接拷贝给其他成员,可迅速完成开发环境搭建
需要当前目录下有 Pipfile文件
拷贝项目环境
pipenv install
拷贝开发环境库
pipenv install --dev
这里 就可以把 我们 之前项目下 所有的依赖项 全部安装到 虚拟环境中
3.4 交互环境
使用 shell
命令可以 进入 交互式环境
pipenv shell
[root@iZ2ze9hn3hwr4e9xprmt32Z test]# pipenv shell
Launching subshell in virtual environment…
[root@iZ2ze9hn3hwr4e9xprmt32Z test]# . /root/.local/share/virtualenvs/test-zQyuU65H/bin/activate
(test) [root@iZ2ze9hn3hwr4e9xprmt32Z test]#
3.5 目录信息
查看当前目录信息 我们可以用 where
命令
pipenv --where
3.5 脚本
执行脚本命令 需要配合 pipenv run
命令
[scripts]
start = "python app.py"
test ="python test.py"
(test) [root@iZ2ze9hn3hwr4e9xprmt32Z test]# pipenv run start
(test) [root@iZ2ze9hn3hwr4e9xprmt32Z test]# pipenv run test
(test) [root@iZ2ze9hn3hwr4e9xprmt32Z test]# pipenv run list
(test) [root@iZ2ze9hn3hwr4e9xprmt32Z test]# pipenv run start
hello world
(test) [root@iZ2ze9hn3hwr4e9xprmt32Z test]# pipenv run test
test model
(test) [root@iZ2ze9hn3hwr4e9xprmt32Z test]# pipenv run list
Package Version
------------ ----------
certifi 2019.11.28
chardet 3.0.4
Click 7.0
Flask 1.1.1
idna 2.8
itsdangerous 1.1.0
Jinja2 2.10.3
MarkupSafe 1.1.1
pip 19.3.1
requests 2.22.0
setuptools 44.0.0
urllib3 1.25.7
Werkzeug 0.16.0
wheel 0.33.6
(test) [root@iZ2ze9hn3hwr4e9xprmt32Z test]#