Skip to content

CWD is not in sys.path when "-P" is used #15214

@frenzymadness

Description

@frenzymadness

I've got a report from a user after the fresh release of Fedora 44, where IPython 9.9 is included. You can read it here, but I'll sum it up below as well: https://bugzilla.redhat.com/show_bug.cgi?id=2479711

Since IPython 9.7.0, CWD is not in sys.path when you use a system-installed IPython from RPM packages.

The problem is caused by this change: bb8b2ba

and the fact that "-P" flag is by default in all Python shebangs for all executable files in Fedora. For IPython, /usr/bin/ipython looks like:

#! /usr/bin/python3 -P
import sys
from IPython import start_ipython
if __name__ == '__main__':
    if sys.argv[0].endswith('.exe'):
        sys.argv[0] = sys.argv[0][:-4]
    sys.exit(start_ipython())

The commit linked above mentions this:

sys.flags.safe_path is python's equivalent to of the ipython flag
ignore_cwd. It is most often set by PYTHONSAFEPATH or by running
python with -P.

which is not entirely true because what is omitted from sys.path when you use "-P" depends on how the python/script/module is run, see: https://docs.python.org/3/using/cmdline.html#cmdoption-P

We've implemented the default "-P" option in this Fedora change because we wanted to avoid /usr/bin to be in the sys.path which caused strange behavior sometimes.

So now, with "-P" in the shebang, we don't have CWD in sys.path but when we remove the flag, we do have /usr/bin/ there, which we want to avoid.

Because the commit message and the docstring of the init_path function are not entirely correct, I consider this to be a bug in IPython. Whether CWD should be omitted or not depends on how the Python is executed, not just on the "-P" flag or the corresponding env variable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions