bpo-33053: -m now adds *starting* directory to sys.path (GH-6231) (#6236)
Historically, -m added the empty string as sys.path
zero, meaning it resolved imports against the current
working directory, the same way -c and the interactive
prompt do.
This changes the sys.path initialisation to add the
*starting* working directory as sys.path[0] instead,
such that changes to the working directory while the
program is running will have no effect on imports
when using the -m switch.
(cherry picked from commit d5d9e02dd3c6df06a8dd9ce75ee9b52976420a8b)
diff --git a/Doc/library/test.rst b/Doc/library/test.rst
index 6041f52..0746fcf 100644
--- a/Doc/library/test.rst
+++ b/Doc/library/test.rst
@@ -1332,8 +1332,8 @@
.. function:: run_python_until_end(*args, **env_vars)
Set up the environment based on *env_vars* for running the interpreter
- in a subprocess. The values can include ``__isolated``, ``__cleavenv``,
- and ``TERM``.
+ in a subprocess. The values can include ``__isolated``, ``__cleanenv``,
+ ``__cwd``, and ``TERM``.
.. function:: assert_python_ok(*args, **env_vars)
diff --git a/Doc/whatsnew/3.7.rst b/Doc/whatsnew/3.7.rst
index 0b5ad00..e0c19cf 100644
--- a/Doc/whatsnew/3.7.rst
+++ b/Doc/whatsnew/3.7.rst
@@ -421,6 +421,12 @@
writable.
(Contributed by Nathaniel J. Smith in :issue:`30579`.)
+* When using the :option:`-m` switch, ``sys.path[0]`` is now eagerly expanded
+ to the full starting directory path, rather than being left as the empty
+ directory (which allows imports from the *current* working directory at the
+ time when an import occurs)
+ (Contributed by Nick Coghlan in :issue:`33053`.)
+
New Modules
===========
@@ -1138,6 +1144,11 @@
parentheses can be omitted only on calls.
(Contributed by Serhiy Storchaka in :issue:`32012` and :issue:`32023`.)
+* When using the ``-m`` switch, the starting directory is now added to sys.path,
+ rather than the current working directory. Any programs that are found to be
+ relying on the previous behaviour will need to be updated to manipulate
+ :data:`sys.path` appropriately.
+
Changes in the Python API
-------------------------