程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

A new generation Python package management tool -- PDM

編輯:Python

PDM It's a new one Python Package manager , Maybe you don't know its existence yet , But actually PDM It has been born for two years , And in 2021 Released in 1.0 edition , At present, the highest version is 1.12.8.

Just heard that PDM when , I subconsciously thought it was Python Development Manager, Another and Pipenv and Poetry The same virtual environment management tool for changing soup without changing medicine .

Until I turned to the author's blog , not have understood until then PDM The full name is Python Development Master, It's even better than I thought .

It is worth mentioning that ,PDM The author is PyPa member 、Pipenv One of the main maintainers at present , most important of all , He is a Chinese , So this is a tool developed by Chinese people .

# 1. Why PDM?

Early package managers ( Such as Pipevn,Poetry), Are based on virtual environments , Virtual environment is mainly used to isolate the project development environment , But if it comes to virtual Environment nested virtual environment , The problem is difficult , There are always problems .

PDM Benefit from one 2018 Year of PEP The proposal (PEP582,Python local packages directory), Completely abandon the virtual environment .

From the author's blog , The reason why we had to build a wheel again , It's all because Pipenv and Poetry It's not easy to use , It happens that PEP582 , Can develop an epoch-making Python Package management tools , It is PDM .

PDM It includes the following features :

  • PEP 582 Local project library Directory , Support installation and running commands , There's no need for a virtual environment .

  • A simple and relatively fast dependency parser , Especially for large binary packages .

  • compatible PEP 517 Build the back end of , For building release packages ( Source format and wheel Format )

  • Have a flexible and powerful plug-in system ( If there is a plug-in system, it will directly open a level )

  • PEP 621 Metadata format

  • image pnpm The same centralized installation cache , Save disk space

Even though PDM It was developed by Chinese people , But considering internationalization , The official website documents are all in English .

I spent the whole day , After reading the document , Digested 70% Of PDM usage , Now share your experience , I'll start with you PDM To be helpful to .

About PDM, It's a lot of content , I intend to introduce it completely in two parts :

  • Entry level tutorial for novices

  • A tutorial for ashes

This is the first one , Let's start with pdm There is a framework understanding of the basic usage of , and pdm Real competitiveness, please continue to pay attention to the follow-up articles .

# 2. install PDM

PDM There are many ways to install , On the official website 6 Kind of , such as pip、pipx、homebrew etc.

In previous articles , I recommended pipx Tools , It is very easy to use when installing the package of command-line application .

details : Use pipx Tool installation command line tool

And then PDM Is a command line tool , So I also recommend pipx install , It is convenient to manage the command line uniformly

perform pipx install pdm You can install

PDM Only Python 3.7+ Only the version of , Use other methods to install , Make sure your Python edition , But use pipx You don't need to worry about .

# 3. initialization PDM

perform pdm init Will start initializing , When initializing , Some information that will let you choose the project

  • Whether to upload PyPI

  • Rely on the Python edition

  • License type

  • The author information

  • Email information

I have... On my machine Python 2.7 and Python 3.10 Two versions , When initializing the project, all the files on the machine will be deleted Python The versions have been scanned , Will let you choose the project Python edition .

When it's done ,PDM Will take your choice as toml Format to write pyproject.toml In profile .

# 4. PDM usage

pdm There are so many orders , Use -h You can see the help menu

4.1 Installation package

and Poetry equally , The installation uses add command , but pdm Of add Than poetry To use , Mainly reflected in grouping , Please pay attention to the following articles

4.2 Check the bag

Use pdm list You can list the packages installed in the current environment in the form of a list

Add another --graph You can view in a tree , The hierarchy of direct and indirect dependency packages is clear at a glance

pdm list There are two more options :

  • –freeze: With requirements.txt List installed packages in the format of

  • –json: With json List installed packages in the format of , But it has to be with --graph Use at the same time

To view the details of a package , Direct use pdm show that will do

4.3 Delete package

Delete package using remove command

4.4 Project configuration

Without any parameters , You can print out the environment configuration of the project

If you want to change it , Just add key and value As a parameter , To modify pypi Take the mirror proxy as an example

It turned out that it was watercress , Now I want to change it to Ali source , Just execute the following command , Comparable poetry It's much more convenient ~

pdm config There are many configurations , If you want to find out one by one, you can go to the official website :https://pdm.fming.dev/configuration/

4.5 Run the command

Want to be in pdm Execute commands or projects in an environment , have access to run command , If the project is executed , There are many parameters , Can be in pyproject.toml Configure command aliases , The specific use , Please look back

4.6 Look at the environment

Use info command , You can view the environment information of the current project

4.7 Update package

Updated words , In a simple scenario , Just use the following two

# Update all packages
pdm update
# Update a package
pdm update <pkg>

Complex scenes ,pdm It's all for you , It offers many options , It can be used as needed ( If there is any explanation error in the following , Please help to correct )

  • –save-compatible: Projects rely on compatible versions

  • –save-wildcard: Save wildcard version ( I don't understand )

  • –save-exact: Save the package with the specified exact version

  • –save-minimum: Keep the minimum version of the package

  • –update-reuse: Try to update only the packages specified on the command line , The dependent package cannot be updated without updating

  • –update-eager: Update a package and update its dependent packages ( Recursive upgrade )

  • –prerelease: Allow early release ( I don't understand )

  • –unconstrained: Ignore package version constraints , You can upgrade the package to the latest version

  • –top: Only updated in pyproject.toml My bag

  • –dry-run: A test run , Without modifying lock file

  • –no-sync: Update only lock file , But don't update the package

If your dependent package has set grouping , Group updates can also be specified

pdm update -G security -G http

You can also specify a group to update a package in the group

pdm update -G security cryptography

Add another -d You can specify dev rely on

# Update all dev rely on
pdm update -d
# to update dev A package that depends on the next group
pdm update -dG test pytest

similarly , You can also specify --prod perhaps --production Upgrade non dev ( That is production ) My bag .

4.8 Switch py

When you are initializing pdm Project time , You have selected the current Python Version and available Python Version range , If you want to change later , have access to use command , However, the version is subject to the previously set version range .

Suppose the allowable range is python 3.9+, Currently using python 3.10, You can switch directly .

pdm use python3.9

# 5. Command alias

stay pyproject.toml add to [tool.pdm.scripts] You can set the shortcut command alias , If the execution of the project has many parameters , This method of setting aliases will be useful .

[tool.pdm.scripts] There are two forms

# The first one is
[tool.pdm.scripts]
start = "python main.py"
# The first one is
[tool.pdm.scripts]
start = {cmd = "python main.py"}

But if you want to add comments to the parameters , You have to use the second method , Such as this

[tool.pdm.scripts]
start = {cmd = [
"flask",
"run",
# Important comment here about always using port 54321
"-p", "54321"
]}

except cmd outside , There are two more parameters

One is shell Parameters , From the output, you should see and cmd The difference between , and subprocess.Popen() with shell=True Almost one thing

One is env_file Parameters , You can specify the file for configuring environment variables

[tool.pdm.scripts]
start.cmd = "flask run -p 54321"
start.env_file = ".env"

If you want to limit the file of this environment variable to more than one command , It is pdm run overall situation , You can configure it like this

[tool.pdm.scripts]
_.env_file = ".env"

Add --list perhaps -l You can view the shortcut alias of all settings

For every shortcut command , All can be set pre and post command :

  • pre command : Execute... Before each shortcut command is executed

  • post command : After each shortcut command is executed

[tool.pdm.scripts]
pre_compress = "{
{ Run BEFORE the `compress` script }}"
compress = "tar czvf compressed.tar.gz data/"
post_compress = "{
{ Run AFTER the `compress` script }}"

# 6. Automatic completion

pdm Although there are many orders , But it's not complicated , There is little need to use automatic completion , If you really need to complete , It can also be realized .

For different shell, The configuration of automatic completion is different , This is described in detail on the official website .

If you use it like me zsh, You can refer to my configuration method .

One step in the middle of the screenshot is vim ~/.zshrc , Yes, it will pdm Plug in configuration to zsh in

plugins=(git z macos extract zsh-syntax-highlighting zsh-autosuggestions pdm)

# 7. The scheme is compatible with

Other solutions migrate to pdm

pdm Good enough to use , And open enough , If you are currently using another package manager , such as pipenv ,poetry, Or still using the most primitive requirements.txt , You can also easily migrate to pdm In the to :

  • Use pdm import -f {file} No initialization is required , Direct conversion

  • perform pdm init perhaps pdm install When , It will automatically identify your current dependency and convert

pdm Migrate to other solutions

alike , You can also be pdm Managed projects , Export to other schemes

pyproject.toml and pdm.lock yes pdm Two core documents of .

pdm As a rising star , And I didn't forget my roots , It supports :

  • take pyproject.toml Turn into setup.py

    pdm export -f setuppy -o setup.py
    
  • take pdm.lock Turn into requirements.txt

    pdm export -o requirements.txt
    

# 8. To sum up

It took a lot of effort , Finally put PDM After introducing the basic usage of , I believe someone will question : This is what you call An epoch-making package manager

actually , The above is just an introduction , and PDM Some of the core knowledge of , Considering the limited space , I will arrange the contents of these advanced classes in subsequent articles , It will include but not be limited to :

  • PDM Analysis of the principle of :PEP 582 The proposal

  • Build the release package :PEP 517 The proposal

  • Hook Definition and use of script

  • Plug in management system and custom plug-ins

  • Introduction of cache management system

These are PDM At the heart of , Only by understanding these , You can really make good use of PDM, Then you will feel : Why? Guido It's not a standard tool to pack ?

About PDM Follow up advanced articles , Please pay attention to my official account 《 Mingo who writes some code 》

I'll keep updating about... There PDM Advanced use of

About packages and virtual environment management tools , I have written special articles on many tools

Interested in learning systematically , You can visit this link :https://python.iswbm.com/

Finally, make a little investigation

What package management tools do you usually use ?
1、requirements.txt
2、virtualenv
3、pipenv
4、poetry
5、venv
6、 other

With PDM, Do you still use these tools ?
1、 Meeting
2、 Can't
3、 Learn more about

Welcome to share in the comments section


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved