Skip to content

feat(run): add --env, --env-file and --working-dir options to pdm run - #3865

Merged
frostming merged 3 commits into
mainfrom
feat/run-env-options
Aug 28, 2026
Merged

feat(run): add --env, --env-file and --working-dir options to pdm run#3865
frostming merged 3 commits into
mainfrom
feat/run-env-options

Conversation

@frostming

Copy link
Copy Markdown
Collaborator

Pull Request Checklist

  • A news fragment is added in news/ describing what is new.
  • Test cases added for changed code.

Describe what you have changed in this PR.

Implements #3829 by extending pdm run to set/override the env, env_file and working_dir script options dynamically from the command line.

New options

--env KEY=VALUE       Set environment variables for the script. Can be supplied multiple times.
--env-file FILE       Read environment variables from the given dotenv file.
--working-dir DIR     Set the working directory for the script.

Behavior

  • Options take precedence over the values defined in pyproject.toml (script-level and shared _ options).
  • They apply to the requested task, to bare commands (e.g. pdm run --env FOO=bar python ...), and are propagated to every task run by a composite script.
  • An invalid --env value (missing =) raises a PdmUsageError.
  • --site-packages already existed as -s/--site-packages, so no new flag was needed for site_packages.

Notes

  • As with -s/--site-packages, these options must be placed before the script name: everything after the script name is forwarded verbatim to the script (args uses nargs=REMAINDER). This is documented.
  • Added docs section and 6 new test cases (all 98 tests in tests/cli/test_run.py pass, plus test_completion.py).

Extend `pdm run` to dynamically set or override the `env`, `env_file` and
`working_dir` script options from the command line. These options apply to
the requested task (and all tasks of a composite script) and take
precedence over the values defined in pyproject.toml.

Closes #3829

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9c9cd742d0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/pdm/cli/commands/run.py
Comment thread src/pdm/cli/commands/run.py Outdated
@codecov

codecov Bot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.75%. Comparing base (7a0d759) to head (c457ebc).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3865      +/-   ##
==========================================
+ Coverage   88.51%   88.75%   +0.23%     
==========================================
  Files         121      120       -1     
  Lines       13281    13300      +19     
  Branches     2257     2260       +3     
==========================================
+ Hits        11756    11804      +48     
+ Misses        960      931      -29     
  Partials      565      565              
Flag Coverage Δ
unittests 88.63% <100.00%> (+0.23%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Move the --site-packages handling from global_options to run_options so
that all command-line overrides are handled in a single place with the
same (highest) precedence.
… --env keys

- Resolve a bare .py script relative to the selected working directory
  before inspecting its inline metadata, so --working-dir works with
  self-contained scripts.
- Treat an empty key in --env assignments (=value) as invalid.
@frostming
frostming merged commit 50ca509 into main Aug 28, 2026
25 checks passed
@frostming
frostming deleted the feat/run-env-options branch August 28, 2026 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant