Total Pageviews

Wednesday, 31 August 2022

PDM

 A modern Python package dependency manager supporting the latest PEP standards.

https://pdm.fming.dev/

中文版本说明

Docs Twitter Follow Discord

Github Actions PyPI codecov Packaging status Downloads pdm-managed

What is PDM?

PDM is meant to be a next generation Python package management tool. It was originally built for personal use. If you feel you are going well with Pipenv or Poetry and don't want to introduce another package manager, just stick to it. But if you are missing something that is not present in those tools, you can probably find some goodness in pdm.

Highlights of features

  • Opt-in PEP 582 support, no virtualenv involved at all.
  • Simple and fast dependency resolver, mainly for large binary distributions.
  • A PEP 517 build backend.
  • PEP 621 project metadata.
  • Flexible and powerful plug-in system.
  • Versatile user scripts.
  • Opt-in centralized installation cache like pnpm.

What is PEP 582?

The majority of Python packaging tools also act as virtualenv managers to gain the ability to isolate project environments. But things get tricky when it comes to nested venvs: One installs the virtualenv manager using a venv encapsulated Python, and create more venvs using the tool which is based on an encapsulated Python. One day a minor release of Python is released and one has to check all those venvs and upgrade them if required.

PEP 582, on the other hand, introduces a way to decouple the Python interpreter from project environments. It is a relatively new proposal and there are not many tools supporting it (one that does is pyflow, but it is written with Rust and thus can't get much help from the big Python community and for the same reason it can't act as a PEP 517 backend).

PEP 582 proposes a project structure as below:

foo
    __pypackages__
        3.8
            lib
                bottle
    myscript.py

There is a __pypackages__ directory in the project root to hold all dependent libraries, just like what npm does. Read more about the specification here.

Installation

PDM requires python version 3.7 or higher.

Via Install Script

Like Pip, PDM provides an installation script that will install PDM into an isolated environment.

For Linux/Mac

curl -sSL https://raw.githubusercontent.com/pdm-project/pdm/main/install-pdm.py | python3 -

For Windows

(Invoke-WebRequest -Uri https://raw.githubusercontent.com/pdm-project/pdm/main/install-pdm.py -UseBasicParsing).Content | python -

For security reasons, you should verify the checksum of install-pdm.py. The sha256 checksum is: f09accb8a530315be312cf9ce7af987ccb608aa90d3972968d73e7ef7d8c547b

The installer will install PDM into the user site and the location depends on the system:

  • $HOME/.local/bin for Unix
  • %APPDATA%\Python\Scripts on Windows

You can pass additional options to the script to control how PDM is installed:

usage: install-pdm.py [-h] [-v VERSION] [--prerelease] [--remove] [-p PATH] [-d DEP]

optional arguments:
  -h, --help            show this help message and exit
  -v VERSION, --version VERSION | envvar: PDM_VERSION
                        Specify the version to be installed, or HEAD to install from the main branch
  --prerelease | envvar: PDM_PRERELEASE    Allow prereleases to be installed
  --remove | envvar: PDM_REMOVE            Remove the PDM installation
  -p PATH, --path PATH | envvar: PDM_HOME  Specify the location to install PDM
  -d DEP, --dep DEP | envvar: PDM_DEPS     Specify additional dependencies, can be given multiple times

You can either pass the options after the script or set the env var value.

Alternative Installation Methods

If you are on MacOS and using homebrew, install it by:

brew install pdm

If you are on Windows and using Scoop, install it by:

scoop bucket add frostming https://github.com/frostming/scoop-frostming.git
scoop install pdm

Otherwise, it is recommended to install pdm in an isolated environment with pipx:

pipx install pdm

Or you can install it under a user site:

pip install --user pdm

With asdf-vm

asdf plugin add pdm
asdf install pdm latest

Quickstart

Initialize a new PDM project

pdm init

Answer the questions following the guide, and a PDM project with a pyproject.toml file will be ready to use.

Install dependencies

pdm add requests flask

You can add multiple dependencies in the same command. After a while, check the pdm.lock file to see what is locked for each package.

Run your script with PEP 582 support

By default, PDM will create .venv in the project root, when doing pdm install on an existing project, as other package managers do. But you can make PEP 582 the default by pdm config python.use_venv false. To enable the full power of PEP 582, do the following steps to make the Python interpreter use it.

Suppose you have a script app.py placed next to the __pypackages__ directory with the following content(taken from Flask's website):

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

If you are a Bash user, set the environment variable by eval "$(pdm --pep582)". Now you can run the app directly with your familiar Python interpreter:

$ python /home/frostming/workspace/flask_app/app.py
 * Serving Flask app "app" (lazy loading)
 ...
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Ta-da! You are running an app with its dependencies installed in an isolated place, while no virtualenv is involved.

For Windows users, please refer to the doc about how to make it work, it also includes a simple explanation of how it works.

Badges

Tell people you are using PDM in your project by including the markdown code in README.md:

[![pdm-managed](https://img.shields.io/badge/pdm-managed-blueviolet)](https://pdm.fming.dev)

pdm-managed

Packaging Status

Packaging status

PDM Eco-system

Awesome PDM is a curated list of awesome PDM plugins and resources.

from  https://github.com/pdm-project/pdm

------

python的包管理器pdm

python项目的开发之中,虚拟环境是必不可少的部分。

以下是它的部分问题

  • 对于新手来说,虚拟环境的工作方式令人困惑
  • 不同的平台需要不同的命令集来激活虚拟环境
  • 你需要在每个终端中都激活虚拟环境
  • 多个虚拟环境嵌套的使用体验不佳

针对这些问题,本文介绍的 Python 包管理器: PDM - Python Development Master ,提供了一个将 Python 解释器和项目开发环境解耦的新途径。

以下是 PDM 的特点

  • 不依赖虚拟环境。
  • 简单且快速的依赖解析器
  • 完备的插件系统

PDM 使用起来非常轻松,接下来让我们尝试一下。

开始使用

首先安装PDM

> pip install pdm

以django为例,创建一个项目

> mkdir django
> cd django

初始化

> pdm init

一下是一些常用操作

> pdm add django #安装库
> pdm run python manage.py runserver #运行命令
> pdm list #显示列表

PDM创建者

PDM创建者 Frost Ming,Python 开源爱好者,坐标深圳。Pycon 讲师,pipenv 核心贡献者。活跃于各大社交平台,帮助众多 Python 患者解决各类疑难杂症。

 

No comments:

Post a Comment