Manage your python projects with pipenv|用pipenv管理你的python项目
What Will I Learn?
- You will learn what is pipenv
- You will learn pipenv features
- You will learn basic usage of pipenv
Requirements
- have experience in python
- have installed python and pip
Difficulty
- Intermediate
Tutorial Contents
What is pipenv
Pipenv in Python is like npm in Nodejs or bundler in Ruby. It's a development workflow in python. It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages. It also generates the ever–important Pipfile.lock, which is used to produce deterministic builds.
Pipenv features
- 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.
Install Pipenv
pip install pipenv
If you already have a python project and want to manage it with pipenv
cd myproject
pipenv install
you can initialise your project with specified Python
pipenv --two install
pipenv --three install
pipenv --python3.6 install
pipenv --python path_to_excutable_python install
These commands will create two files Pipfile and Pipfile.lock. Pipfile includes dependencies without specified version while Pipfile.lock is detailed and strict.
Manage python dependencies
To install a Python package for your project use the install keyword. For example,
pipenv install requests
will install the current version of requests package. It will be removed with a similar keyword uninstall.
pipenv uninstall requests
The package name, together with its version and a list of its own dependencies, can be frozen by updating the Pipfile.lock.
pipenv lock
With Pipfile and Pipfile.lock, you can create a virtualenv and reinstall all dependencies in a new environment. With the following command, pipenv will load Pipfile and Pipfile.lock automatically in another environment.
pipenv install
Manage virtual environments
When you initialise a project, the virtualenv will be created automatically in ~/.local/share/virtualenvs.When I run
cd steemrobot
pipenv install
a virtualenv is created in ~/.local/share/virtualenvs/steemrobot-SeO244qt.
You can locate your project and your virtualenvs with some keywords.
Locate the project:
$ pipenv --where
/store/MyProjects/steemrobot
Locate the virtualenv:
$ pipenv --venv
/home/mucyoung/.local/share/virtualenvs/steemrobot-SeO244qt
Locate the excutable python:
pipenv --py
/home/mucyoung/.local/share/virtualenvs/steemrobot-SeO244qt/bin/python
Development environment
There are usually some Python packages that are only required in your development environment and not in your production environment, such as unit testing packages. Pipenv will let you keep the two environments separate using the --dev flag.
pipenv install --dev requests
The above command will only install requests package in your development environment instead of production environment. When you initialise a project in a development mode, you should run
pipenv install --dev
while
pip install
in a production mode.
Running your project
You can run your project by entering the virtualenv with the command.
$ pipenv shell
(steemrobot-SeO244qt) mucyoung@mucyoung-P7xxTM:/store/MyProjects/steemrobot$
It's just like what you do with virtualenv.without explicitly activating it first, by using the run keyword.
pipenv run myproject.py
您能从本教程学到什么?
- pipenv是什么
- pipenv的特点
- pipenv的基本使用
准备条件
- 有python使用经验
- 已经安装了python和pip
难度
- 中等
教程内容
什么是pipenv
Pipenv之于Python就像npm之于Nodejs或者bundler之于Ruby。它是Python中的开发工作流。 它自动为你的工程创建一个virtualenv, 你在安装和删除包的时候可以自动生成更新相应的Pipfile。它也生成了一个同样重要的Pipfile.lock文件,Pipfile.lock文件是为了精确构建项目(所有依赖包版本必须精确)。
Pipenv的特点
- 允许项目的精确build,指定了你需要的一切。
- 对于锁定版本的依赖包生成并检查文件哈希。
- 自动安装需要版本的python。
- 通过寻找Pipfile自动递归寻找你的工程目录。
- 如果项目中不存在Pipfile,自动生成Pipfile。
- 在一个标准目录为一个项目自动生成一个virtualenv。
- 在安装和删除包的时候可以自动生成更新相应的Pipfile。
- 自动装载.env文件。
安装Pipenv
pip install pipenv
如果你已经有了一个python项目并想用pipenv管理它,你可以运行
cd myproject
pipenv install
你也可以指定具体的python版本
pipenv --two install
pipenv --three install
pipenv --python3.6 install
pipenv --python path_to_excutable_python install
这些操作会创建Pipfile和Pipfile.lock两个文件。Pipfile包含不指定具体版本的依赖包信息而Pipfile.lock更加具体和严格。
管理python依赖包
你可以用install关键字来安装一个python依赖包。比如,
pipenv install requests
命令会安装当前最新版本的requests包。同样的卸载相应包也可以用类似的uninstall关键字。
pipenv uninstall requests
依赖包名及版本可以通过如下命令被Pipfile.lock保存。
pipenv lock
通过Pipfile和Pipfile.lock文件,在一个新的环境你可以创建一个virtualenv环境并重新安装所有依赖包。通过如下命令, 在新的环境pipenv会自动读取Pipfile和Pipfile.lock文件并安装。
pipenv install
管理virtualenv
当你初始化一个项目时,~/.local/share/virtualenvs目录会自动创建一个virtualenv环境。当运行
cd steemrobot
pipenv install
时,~/.local/share/virtualenvs/steemrobot-SeO244qt就被创建了。
你也可以用一些关键字定位你的项目和virtualenv。
定位项目:
$ pipenv --where
/store/MyProjects/steemrobot
定位virtualenv:
$ pipenv --venv
/home/mucyoung/.local/share/virtualenvs/steemrobot-SeO244qt
定位python执行环境:
pipenv --py
/home/mucyoung/.local/share/virtualenvs/steemrobot-SeO244qt/bin/python
开发环境
有一些包只会在开发环境中用到,而在生产环境中不会用到,比如单元测试相关包。 Pipenv用--dev来区分这开发环境和生产环境。
pipenv install --dev requests
上面的命令只会在开发环境中安装requests包,而不会在生产环境中安装。当你在开发模式下初始化下项目时,你应该运行
pipenv install --dev
而
pip install
则是在生产模式下初始化项目。
运行你的项目
你可以通过如下命令进入你创建的virtualenv环境之下。
$ pipenv shell
(steemrobot-SeO244qt) mucyoung@mucyoung-P7xxTM:/store/MyProjects/steemrobot$
剩下的操作就跟你在virtualenv中用的一样。如果你不希望事先activate你的virtualenv环境,你可以使用run关键字。
pipenv run myproject.py
Posted on Utopian.io - Rewarding Open Source Contributors
不错不错!
Your contribution cannot be approved because it does not follow the Utopian Rules.
Your tutorial needs to be more descriptive, and to contain images that illustrate the steps and the work
You can contact us on Discord.
[utopian-moderator]
Thanks, I'll learn a lesson from this.