PDM Scripts#
Like npm run, with PDM, you can run arbitrary scripts or commands with local packages loaded.
Arbitrary Scripts#
1 | |
It will run flask run -p 54321 in the environment that is aware of packages in your project environment.
Single-file Scripts#
Tip
Added in 2.16.0.
PDM can run single-file scripts with inline script metadata specified by PEP 723.
The following is an example of a script with embedded metadata:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
When you run it with pdm run test_script.py, PDM will create a temporary environment with the specified dependencies installed and run the script:
1 2 3 4 5 6 7 8 9 10 11 12 | |
--reuse-env option if you want to reuse the environment created last time.
You can also add [tool.pdm] section to the script metadata to configure PDM. For example:
1 2 3 4 5 6 7 8 9 10 11 12 | |
Read the specification for more details.
User Scripts#
PDM also supports custom script shortcuts in the optional [tool.pdm.scripts] section of pyproject.toml.
Confuse with [project.scripts]?
There is another field [project.scripts] in pyproject.toml, and the scripts can also be invoked with pdm run. It's used to define the console script entry points to be installed with the package. Therefore, the executables can only be run after the project itself is installed into the environment. That is to say, you must have distribution = true.
In contrast, [tool.pdm.scripts] defines some tasks to be run in your project. It works for projects regardless of whether the distribution is true or false. The tasks are primarily for development and testing purposes and support more types and settings, as will be shown later., you can regard it as a replacement for Makefile. It doesn't require the project to be installed but requires the existence of a pyproject.toml file.
See more explanations about [project.scripts] here.
You can then run pdm run <script_name> to invoke the script in the context of your PDM project. For example:
1 2 | |
And then in your terminal:
1 2 | |
Any following arguments will be appended to the command:
1 2 | |
Yarn-like script shortcuts
There is a builtin shortcut making all scripts available as root commands as long as the script does not conflict with any builtin or plugin-contributed command.
Said otherwise, if you have a start script, you can run both pdm run start and pdm start.
But if you have an install script, only pdm run install will run it, pdm install will still run the builtin install command.
PDM supports 4 types of scripts:
cmd#
Plain text scripts are regarded as normal command, or you can explicitly specify it:
1 2 | |
In some cases, such as when wanting to add comments between parameters, it might be more convenient. to specify the command as an array instead of a string:
1 2 3 4 5 6 7 | |
shell#
Shell scripts can be used to run more shell-specific tasks, such as pipeline and output redirecting.
This is basically run via subprocess.Popen() with shell=True:
1 2 | |
call#
The script can be also defined as calling a python function in the form <module_name>:<func_name>:
1 2 | |
The function can be supplied with literal arguments:
1 2 | |
composite#
This script kind execute other defined scripts:
1 2 3 4 | |
Running pdm run all will run lint first and then test if lint succeeded.
Tip
Added in 2.13.0.
To override the default behavior and continue the execution of the remaining scripts after a failure,
set the keep_going option to true:
1 2 3 4 5 | |
If keep_going set to true return code of composite script is either '0' if all succeeded or the code of last failed individual script.
You can also provide arguments to the called scripts:
1 2 3 4 | |
Note
Argument passed on the command line are given to each called task.
You can also use the composite script to combine multiple commands:
1 2 3 4 5 | |
Script Options#
env#
All environment variables set in the current shell can be seen by pdm run and will be expanded when executed.
Besides, you can also define some fixed environment variables in your pyproject.toml:
1 2 3 | |
Note how we use TOML's syntax to define a composite dictionary.
About environment variable substitution
Variables in script specifications can be substituted in all script types. In cmd scripts, only ${VAR}
syntax is supported on all platforms, however in shell scripts, the syntax is platform-dependent. For example,
Windows cmd uses %VAR% while bash uses $VAR.
Note
Environment variables specified on a composite task level will override those defined by called tasks.
env_file#
You can also store all environment variables in a dotenv file and let PDM read it:
1 2 3 | |
The variables within the dotenv file will not override any existing environment variables. If you want the dotenv file to override existing environment variables use the following:
1 2 3 | |
Environment variable loading order
Env vars loaded from different sources are loaded in the following order:
- OS environment variables
- Project environments such as
PDM_PROJECT_ROOT,PATH,VIRTUAL_ENV, etc - Dotenv file specified by
env_file - Env vars mapping specified by
env
Env vars from the latter sources will override those from the former sources. A dotenv file specified on a composite task level will override those defined by called tasks.
An env var can contain a reference to another env var from the sources loaded before, for example:
1 2 | |
FOO=hello-42. The reference can also contain a default value with the syntax ${VAR:-default}.
working_dir#
Tip
Added in 2.13.0.
You can set the current working directory for the script:
1 2 3 | |
Relative paths are resolved against the project root.
Tip
Added in 2.20.2.
To identify the original calling working directory, each script gets the environment variable PDM_RUN_CWD injected.
site_packages#
To make sure the running environment is properly isolated from the outer Python interpreter,
site-packages from the selected interpreter WON'T be loaded into sys.path, unless any of the following conditions holds:
- The executable is from
PATHbut not inside the__pypackages__folder. -s/--site-packagesflag is followingpdm run.site_packages = trueis in either the script table or the global setting key_.
Note that site-packages will always be loaded if running with PEP 582 enabled(without the pdm run prefix).
Shared Options#
If you want the options to be shared by all tasks run by pdm run,
you can write them under a special key _ in [tool.pdm.scripts] table:
1 2 3 4 | |
Besides, inside the tasks, PDM_PROJECT_ROOT environment variable will be set to the project root.
Arguments placeholder#
By default, all user provided extra arguments are simply appended to the command (or to all the commands for composite tasks).
If you want more control over the user provided extra arguments, you can use the {args} placeholder.
It is available for all script types and will be interpolated properly for each:
1 2 3 4 | |
will produce the following interpolations (those are not real scripts, just here to illustrate the interpolation):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
You may optionally provide default values that will be used if no user arguments are provided:
1 2 | |
will produce the following:
1 2 3 4 | |
Note
As soon a placeholder is detected, arguments are not appended anymore.
This is important for composite scripts because if a placeholder
is detected on one of the subtasks, none for the subtasks will have
the arguments appended, you need to explicitly pass the placeholder
to every nested command requiring it.
Note
call scripts don't support the {args} placeholder as they have
access to sys.argv directly to handle such complex cases and more.
{pdm} placeholder#
Sometimes you may have multiple PDM installations, or pdm installed with a different name. This
could for example occur in a CI/CD situation, or when working with different PDM versions in
different repos. To make your scripts more robust you can use {pdm} to use the PDM entrypoint
executing the script. This will expand to {sys.executable} -m pdm.
1 2 | |
will produce the following output:
1 2 3 4 5 | |
Note
While the above example uses PDM 2.8, this functionality was introduced in the 2.10 series and only backported for the showcase.
Show the List of Scripts#
Use pdm run --list/-l to show the list of available script shortcuts:
1 2 3 4 5 6 7 8 | |
You can add an help option with the description of the script, and it will be displayed in the Description column in the above output.
Note
Tasks with a name starting with an underscore (_) are considered internal (helpers...) and are not shown in the listing.
Pre & Post Scripts#
Like npm, PDM also supports tasks composition by pre and post scripts, pre script will be run before the given task and post script will be run after.
1 2 3 4 | |
In this example, pdm run compress will run all these 3 scripts sequentially.
The pipeline fails fast
In a pipeline of pre - self - post scripts, a failure will cancel the subsequent execution.
Hook Scripts#
Under certain situations PDM will look for some special hook scripts for execution:
post_init: Run afterpdm initpre_install: Run before installing packagespost_install: Run after packages are installedpre_lock: Run before dependency resolutionpost_lock: Run after dependency resolutionpre_build: Run before building distributionspost_build: Run after distributions are builtpre_publish: Run before publishing distributionspost_publish: Run after distributions are publishedpre_script: Run before any scriptpost_script: Run after any scriptpre_run: Run once before run script invocationpost_run: Run once after run script invocation
Note
Pre & post scripts can't receive any arguments.
Avoid name conflicts
If there exists an install scripts under [tool.pdm.scripts] table, pre_install
scripts can be triggered by both pdm install and pdm run install. So it is
recommended to not use the preserved names.
Note
Composite tasks can also have pre and post scripts. Called tasks will run their own pre and post scripts.
Skipping scripts#
Because, sometimes it is desirable to run a script but without its hooks or pre and post scripts,
there is a --skip=:all which will disable all hooks, pre and post.
There is also --skip=:pre and --skip=:post allowing to respectively
skip all pre_* hooks and all post_* hooks.
It is also possible to need a pre script but not the post one,
or to need all tasks from a composite tasks except one.
For those use cases, there is a finer grained --skip parameter
accepting a list of tasks or hooks name to exclude.
1 | |
This command will run the my-composite task and skip the pre_task1 hook as well as the task2 and its hooks.
You can also provide you skip list in PDM_SKIP_HOOKS environment variable
but it will be overridden as soon as the --skip parameter is provided.
There is more details on hooks and pre/post scripts behavior on the dedicated hooks page.