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: os.chdir breaks result of os.path.abspath(__file__) and os.path.realpath(__file__)
Type: behavior Stage: resolved
Components: Interpreter Core, Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: LordBlick, eric.snow, ncoghlan, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2015-07-19 14:26 by LordBlick, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_os_path.py LordBlick, 2015-07-19 14:26
Messages (4)
msg246942 - (view) Author: Daniel al. LordBlick (LordBlick) * Date: 2015-07-19 14:26
The use of methods path.chdir () corrupts the subsequent ability to detect the file path which is interpreted.
I've made simple example, which is atached:
$ cd ~/tmp
$ ./test_os_path.py 
abspath:        ~/tmp/test_os_path.py
weak abspath:   ~/tmp/test_os_path.py
realpath:       ~/tmp/subtemp/test_os_path.py
weak realpath:  ~/tmp/subtemp/test_os_path.py
$ cd /usr
$ ~/tmp/test_os_path.py
abspath:        ~/tmp/test_os_path.py
weak abspath:   ~/tmp/test_os_path.py
realpath:       ~/tmp/subtemp/test_os_path.py
weak realpath:  ~/tmp/subtemp/test_os_path.py
$ cd ~
$ tmp/test_os_path.py
abspath:        ~/tmp/test_os_path.py
weak abspath:   ~/tmp/tmp/test_os_path.py
realpath:       ~/tmp/subtemp/test_os_path.py
weak realpath:  ~/tmp/tmp/test_os_path.py
msg246972 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-07-20 08:45
The issue is not in os.path, but in __file__ been relative path. If you change current work directory, __file__ is no longer valid path to source file. Things are even worse with zipimport. When you will archive the script in the ZIP file and run this ZIP file, __file__ will not be a path to the source file from the start.

See also issue18416.
msg246978 - (view) Author: Daniel al. LordBlick (LordBlick) * Date: 2015-07-20 11:44
If so, then should be internally __file__ edit by zipimport and/or os.cwd?
It's simple string in <file>.__dict__['__file__']…
Is exist some class representing internal file? Then any cwd operation should be wraped by it.
msg373077 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-07-06 08:16
I interpret Serhiy's comment as 'not a bug'.  In any case, the linked issue changed __file__ to an absolute path, and 2.7 is frozen.
History
Date User Action Args
2022-04-11 14:58:19adminsetgithub: 68858
2020-07-06 08:16:03terry.reedysetstatus: open -> closed

nosy: + terry.reedy
messages: + msg373077

resolution: not a bug
stage: resolved
2020-06-05 19:27:10brett.cannonsetnosy: - brett.cannon
2015-07-20 11:44:37LordBlicksetmessages: + msg246978
components: + Interpreter Core
2015-07-20 08:45:21serhiy.storchakasetnosy: + brett.cannon, ncoghlan, eric.snow
messages: + msg246972
2015-07-20 07:23:59LordBlicksetnosy: + serhiy.storchaka
2015-07-19 14:26:33LordBlickcreate