python / cpython

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

Mismatch between the manipulation of `sys.path` by `runpy` and by the Python command-line interface #85980

Open 53cd72ed-7768-4f34-9566-9995ab0e9dae opened 4 years ago

53cd72ed-7768-4f34-9566-9995ab0e9dae commented 4 years ago
BPO 41814
Nosy @ncoghlan, @maggyero

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 = None created_at = labels = ['3.8', 'type-bug', 'library'] title = 'Mismatch between the manipulation of `sys.path` by `runpy` and by the Python command-line interface' updated_at = user = 'https://github.com/maggyero' ``` bugs.python.org fields: ```python activity = actor = 'maggyero' assignee = 'none' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = creator = 'maggyero' dependencies = [] files = [] hgrepos = [] issue_num = 41814 keywords = [] message_count = 1.0 messages = ['377171'] nosy_count = 2.0 nosy_names = ['ncoghlan', 'maggyero'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue41814' versions = ['Python 3.8'] ```

53cd72ed-7768-4f34-9566-9995ab0e9dae commented 4 years ago

Nicholas, I have noticed that runpy.run_path alters sys.path as expected for a file_path argument which is a valid sys.path entry (typically a directory or zip file). That is to say it adds the file_path argument to the beginning of sys.path, like python <valid sys.path entry>.

However, I have also noticed that runpy.run_path does not alter sys.path as expected for a file_path argument which is a Python source or bytecode file path. That is to say it does not add the parent path of the file_path argument to the beginning of sys.path, contrary to python <source or bytecode file path>.

Likewise, I have also noticed that runpy.run_module (with the alter_sys argument set to True of course) does not alter sys.path as expected. That is to say it does not add the path of the current directory to the beginning of sys.path, contrary to python -m <module>.

Only the first of the three previous sys.path manipulations is documented in https://docs.python.org/3/library/runpy.html though, so the runpy implementation is at least compliant with its specification. So is the mismatch between the manipulation of sys.path by runpy and by the Python command-line interface a specification bug or is it the intended behaviour?