Skip to content

Respect PYTHONSAFEPATH if set #11928

Open
@thejcannon

Description

@thejcannon

Docs links:

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 and python (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'

Metadata

Metadata

Assignees

No one assigned

    Labels

    linterRelated to the linterneeds-designNeeds further design before implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions