Open
Description
Docs links:
- https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSAFEPATH
- https://docs.python.org/3/using/cmdline.html#cmdoption-P
Don’t prepend a potentially unsafe path to sys.path:
python -m module
command line: Don’t prepend the current working directory.
python script.py
command line: Don’t prepend the script’s directory. If it’s a symbolic link, resolve symbolic links.
python -c code
andpython
(REPL) command lines: Don’t prepend an empty string, which means the current working directory.
Reproducer with ruff
(note foo
is an implicit namespace package in vanilla Python-speak, but totally not something we expected to import)
$ mkdir foo
$ cat <<EOF > example.py
import pathlib
import pytest
import anthem # other 1p
import foo
[pathlib, pytest, foo,, anthem]
EOF
$ export PYTHONSAFEPATH=1
$ ruff check --fix-only example.py
keeps foo
with 1p.
but:
$ python -c 'import foo'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'foo'