python / cpython

The Python programming language
https://www.python.org/
Other
61.16k stars 29.52k forks source link

sys.path is incorrect when prefix is "" #52005

Closed 85145009-e053-4805-bc73-e98c4f443b9f closed 2 years ago

85145009-e053-4805-bc73-e98c4f443b9f commented 14 years ago
BPO 7757
Nosy @tiran, @bitdancer, @phmc
Files
  • getpath.patch: Correct prefix when python is built with prefix=" "
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields: ```python assignee = None closed_at = created_at = labels = ['interpreter-core', 'type-bug'] title = 'sys.path is incorrect when prefix is ""' updated_at = user = 'https://bugs.python.org/msmithcbncocom' ``` bugs.python.org fields: ```python activity = actor = 'christian.heimes' assignee = 'none' closed = True closed_date = closer = 'christian.heimes' components = ['Interpreter Core'] creation = creator = 'msmith@cbnco.com' dependencies = [] files = ['32293'] hgrepos = [] issue_num = 7757 keywords = ['patch'] message_count = 6.0 messages = ['98152', '98155', '98290', '189979', '200887', '404442'] nosy_count = 6.0 nosy_names = ['christian.heimes', 'r.david.murray', 'msmith@cbnco.com', 'pconnell', 'numerodix', 'sunfinite'] pr_nums = [] priority = 'normal' resolution = 'out of date' stage = 'resolved' status = 'closed' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue7757' versions = ['Python 2.6'] ```

    85145009-e053-4805-bc73-e98c4f443b9f commented 14 years ago

    I've built Python 2.6.2 with a prefix of "" for an embedded system, so it's installed into /bin/python, /lib/python2.6/, etc.

    If I run a script with "python /tmp/script.py" or by putting in a #!/bin/python and executing it directly, sys.path is missing the leading slashes:

    ['/tmp', 'lib/python26.zip', 'lib/python2.6/', 'lib/python2.6/plat-linux2', 'lib/python2.6/lib-tk', 'lib/python2.6/lib-old', 'lib/lib-dynload']

    This causes all module imports to fail. I can work around this by making /usr a symlink to / and running the script as "/usr/bin/python /tmp/script.py", or by setting PYTHONHOME=/ before starting Python.

    In Modules/getpath.c, search_for_prefix() calls reduce() on argv0_path at the end of a do-while loop, so "/bin" becomes "" and the loop terminates. Then there's a call to joinpath(PREFIX, "lib/python2.6"), where PREFIX is "", and this fails (no leading slash).

    calculate_path() warns:

    Could not find platform independent libraries \<prefix>

    and falls back to joinpath(PREFIX, "lib/python2.6") again, which still fails.

    I was thinking I could work around it by building with prefix="/" instead of "", but the behaviour is the same - I don't know why, yet.

    bitdancer commented 14 years ago

    I thought I remembered a bug that mentioned prefix="/". What I found was bpo-1676135, which might have something to do with your last question.

    85145009-e053-4805-bc73-e98c4f443b9f commented 14 years ago

    Yes, that does look related. The fix from bpo-1676135 seems to handle --prefix="/" properly, and from what I can tell PREFIX does get set to "/".

    There is also code in getpath.c to set sys.prefix to "/" if it's "".

    The correct prefix for configure is actually "", not "/", to avoid double-slashes - but neither one seems to work at the moment.

    83d2e70e-e599-4a04-b820-3814bbdb9bef commented 11 years ago

    There's so much detail provided that I think this should be easy for someone who understands C code.

    09320ff4-bc48-4cea-b5b3-47c4956288e3 commented 10 years ago

    I took a shot at this. Build is successful and imports happen. Tests are ok except test_sysconfig, and that is because of sys.prefix being set to '/'. I've raised bpo-19340 for that.

    About the patch, i'm not sure how to completely test for false positives(joinpath now returns \<SEP>\<suffix> if \<prefix> is null). I could not think of any issues from the current calls to joinpath.

    tiran commented 2 years ago

    This is a very old bug report. Python has changed a lot in the past decade. Please reopen the bug if you still can reproduce the issue with Python 3.9 or newer.