Skip to content

Latest commit

 

History

History
172 lines (131 loc) · 5.87 KB

pep_path0.rst

File metadata and controls

172 lines (131 loc) · 5.87 KB

Don't add sys.path[0] by default

History

Bikeshedding on the option name

Option:

  • --path0 and --nopath0
  • --mainpath and --nomainpath
  • -p and -P

Notes

Python 3.10 sys.path[0]

Command sys.path[0]
python -m module os.getcwd()
python -c code ''
python script.py os.path.realpath('script.py')
REPL: python ''

XXX what if os.chdir() is called?

Python 3.X changed __file__ to make it absolute

Unix permissions

On Unix, modifying the standard library requires the administrator permission. Python supports user directory which can be modified by the current user.

Windows administrator and DLL injection

The Windows installer installed Python 2.7 in C:\Python27 by default which can be modified by regular users. The Windows installer of Python 3 now installs Python in C:\Program Files which can only be modified by the administrator.

On Windows 8.1 and older, the Windows installer is vulnerable to DLL injection: if a malicious DLL is created in the same download directory that the Python installer, the DLL is loaded by the Windows installer.

Moreover, the installation of pip by the Windows installer can also loads malicious DLL installed in of the PATH directories.

Emit a warning if

XXX sys.stdlib_module_names XXX

Unix shebang

Multiple long options cannot be used.

Perl 5.26

In May 2017, the Perl 5.26 release removes the current directory from the default module search path: Removal of the current directory (".") from @INC.

Python options controlling sys.path

  • argv[0] of the C main() function: name and path of the Python program
  • Command line options:
    • -s: don't add the user site directory
    • -E: ignore environment variables
    • -I: isolated mode (imply -E -s)
  • Environment variables:
    • PATH (to get argv[0] absolute path)
    • PYTHONEXECUTABLE (macOS)
    • PYTHONHOME
    • PYTHONNOUSERSITE
    • PYTHONPATH
    • PYTHONPLATLIBDIR
    • __PYVENV_LAUNCHER__ (macOS)
  • Configuration files:
    • pybuilddir.txt
    • python._pth
    • pyvenv.cfg
  • On Windows, application paths in the registry under SoftwarePythonPythonCoreX.YPythonPath of HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE (where X.Y is the Python version).

Moreover, a path is prepended to sys.path: see: Python 3.10 sys.path[0].

See the Python Path Configuration for more details.

Use Cases

Override stdlib module

xxx

Override 3rd party module

xxx