Skip to content

Commit 28b1ba2

Browse files
yhay81claude
andcommitted
gh-131484: Document that a directory or zipfile script stays on sys.path
-P, -I and PYTHONSAFEPATH drop the implicit sys.path[0] entries (current directory, script directory, empty string), but a directory or zipfile named as the script argument is still prepended: that entry is used to locate __main__.py and makes other modules stored in the target importable. The -P section currently enumerates only the -m, script and -c/REPL cases. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 8b048eb commit 28b1ba2

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

Doc/using/cmdline.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,11 @@ source.
163163
added to the start of :data:`sys.path` and the ``__main__.py`` file in
164164
that location is executed as the :mod:`__main__` module.
165165

166-
:option:`-I` option can be used to run the script in isolated mode where
167-
:data:`sys.path` contains neither the script's directory nor the user's
168-
site-packages directory. All ``PYTHON*`` environment variables are
169-
ignored, too.
166+
:option:`-I` can be used to run the script in isolated mode. For a Python
167+
file, the containing directory is then omitted from :data:`sys.path`, as
168+
is the user's site-packages directory; a directory or zipfile target
169+
remains, as described under :option:`-P`. All ``PYTHON*`` environment
170+
variables are ignored, too.
170171

171172
.. audit-event:: cpython.run_file filename
172173

@@ -362,6 +363,13 @@ Miscellaneous options
362363
* ``python -c code`` and ``python`` (REPL) command lines: Don't prepend an
363364
empty string, which means the current working directory.
364365

366+
If the script name refers to a directory or zipfile (see
367+
:ref:`using-on-interface-options`), the script name is still added to the
368+
start of :data:`sys.path`. This also applies when safe-path mode is
369+
enabled by :option:`-I` or :envvar:`PYTHONSAFEPATH`. The entry is used to
370+
locate ``__main__.py`` and makes other modules stored in the target
371+
importable.
372+
365373
See also the :envvar:`PYTHONSAFEPATH` environment variable, and :option:`-E`
366374
and :option:`-I` (isolated) options.
367375

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Clarify that a directory or zipfile passed as the script argument remains on
2+
:data:`sys.path` in safe-path and isolated modes.

0 commit comments

Comments
 (0)