This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: sys.path[0] is already absolute path
Type: Stage: resolved
Components: Documentation Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: brett.cannon, docs@python, ksato9700, vstinner, xtreak
Priority: normal Keywords: patch

Created on 2020-01-18 07:46 by ksato9700, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 18561 merged Ananthakrishnan, 2020-02-19 16:16
Messages (6)
msg360239 - (view) Author: Ken Sato (ksato9700) * Date: 2020-01-18 07:46
In the "What’s New In Python 3.9" (Doc/whatsnew/3.9.rst), it says

> Python now gets the absolute path of the script filename specified on the command line (ex: python3 script.py): the __file__ attribute of the __main__ module and sys.path[0] become an absolute path, rather than a relative path.

However, I believe sys.path[0] is already absolute path since the previous versions. We can probably remove "and sys.path[0]" from the phrase to avoid possible confusions.
msg360273 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-19 22:34
Correct, it seems like sys.path[0] was already absolute and so it can be removed from What's New in Python 3.9.
msg360333 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2020-01-20 21:05
Is sys.path[0] always absolute, or is it just a side-effect of the site module (i.e. is it absolute even with -S)?
msg362976 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-02-29 12:25
New changeset 1f0cd3c61a5ae3aac5ebaccc75ae9828ca4f96c4 by Ananthakrishnan in branch 'master':
bpo-39379: Remove reference to sys.path[0] being absolute path in whatsnew (GH-18561)
https://github.com/python/cpython/commit/1f0cd3c61a5ae3aac5ebaccc75ae9828ca4f96c4
msg362977 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-02-29 12:26
I have merged the doc change since it's minor. I will leave this open for clarification regarding Brett's question.
msg363020 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-02-29 22:15
> Is sys.path[0] always absolute, or is it just a side-effect of the site module (i.e. is it absolute even with -S)?

The absolute path is computed way before the site module is imported.

In Python 3.8, _PyPathConfig_ComputeSysPath0() computes sys.path[0] from sys.argv[0]. If the command line contains a script filename, this function uses realpath() on Unix and GetFullPathNameW() on Windows to get the absolute path. If -m is used, getcwd() is called.
History
Date User Action Args
2022-04-11 14:59:25adminsetgithub: 83560
2020-02-29 22:15:37vstinnersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-02-29 22:15:28vstinnersetmessages: + msg363020
2020-02-29 12:26:25xtreaksetmessages: + msg362977
2020-02-29 12:25:34xtreaksetnosy: + xtreak
messages: + msg362976
2020-02-19 16:16:39Ananthakrishnansetkeywords: + patch
stage: patch review
pull_requests: + pull_request17941
2020-01-20 21:05:29brett.cannonsetnosy: + brett.cannon
messages: + msg360333
2020-01-19 22:34:15vstinnersetmessages: + msg360273
2020-01-18 08:42:56xtreaksetnosy: + vstinner
2020-01-18 07:46:57ksato9700create