PDM series directory

The Python package manager (PDM) is a package manager for Python. The Package manager (PDM) is a package manager for Python. Python package manager — PDM local & Global Project 5. Python Package manager — PDM cache mechanism 6. How to get PyCharm to support PDM?


PDM introduced pep 582’s local package directory, and many questioned: how is that different from a Venv virtual environment when each project is under its own project directory?

Many people don’t have a deep understanding of virtual environments and PEP 582, and this is understandable.

First, the first difference is that the virtual environment has its own Python interpreter, whereas PEP 582 has no new Python interpreter, so pep 582 is much lighter.

Then, the second difference, which is the core of today, is the support for PDM caching.

If multiple PDM projects rely on the same Python package in the same version, each project would normally save a copy of it to its own __pypackages__ directory.

But there are several problems with this:

  1. Waste of disk space
  2. Slow installation

You may think that disk is the cheapest hardware nowadays, so it doesn’t matter if you waste a little bit. However, some Python projects have more dependent packages than you can imagine. For example, OpenStack, the largest Python project in the world, has thousands of dependent packages.

When you create a new PDM project and have to install so many dependencies all over again that it takes more than a day to do it, you know the importance of caching.

1. Enable cache

PDM cache is disabled by default. You can run the following command to enable cache

$ pdm config install.cache on

Copy the code

There are three cache-related configurations

  • Install. cache: Indicates whether cache is enabled
  • Install.cache_method: Selects the connection cache method
  • Cache_dir: Specifies the cache directory

If you don’t need to use cache_dir, use the default directory

/Users/iswbm/Library/Caches/pdm

Copy the code

The tricker-to-understand, and worth talking about, is install.cache_method, which has two values:

  • Symlink: Soft link
  • PTH: Connects in PTH mode

I’ll explain the difference in detail later, so please keep going

2. Simple example

Here is a simple example to give you an idea of how caching works.

First I create two PDM projects

Mkdir PDM -demo2&& CD PDM -demo2pdm initCopy the code

Under PDM-demo1, install typer’s package

pdm add typer

Copy the code

Then go to the Python interactive interpreter and try importing. What is the path of the typer package imported?

You can see that the directory is the same one configured for cache_dir

Then go to PDM-Demo2 and install the typer package again

pdm add typer

Copy the code

Also go to the Python interactive interpreter and try importing. What is the path of the typer package imported?

It can be found that the typer imported is the same as the path of pdM-demo1 before, indicating that the two projects use the same typer package, avoiding the repeated installation of the same package and version.

3. Principle of caching

The principles of caching are not that difficult, and vary from one install. Cache_method to another

cache_method=symlink

Symlink is the default connection and one of the best understood.

When you install the typer package, you can see in the local package directory that the typer points to the typer package in the cache directory via a soft link

cache_method=pth

For. PTH believe that many people are not clear about its usage and principle, here is a brief mention.

When Python finds a.pth file while traversing a known library directory, it adds the path recorded in the file to the sys.path setting, so that the library specified by the.pth file can be found by the Python runtime.

Back to PDM, if you use cache_method= PTH, each time you install a package, a.pth file is generated in your local package directory, which records the lib directory of the package to be cached.

This way, when Python looks for packages in the __pypackages__ directory and finds a.pth file, it adds the path recorded in the.pth file to sys.path.

In the example above, looking at the __pypackages__ directory, you can see that there are many aaa_xxx. PTH files, and the contents of these files are the corresponding package lib directory in our cache directory

4. Cache management

The following is the help for PDM cache management commands

  • PDM cache clear: Clears all caches
  • PDM cache info: Displays all cache information
  • PDM remove [pattern] : deletes matched files
  • PDM cache List: Lists all wheel files in the cache