classification
Title: add support for path-like objects in sys.path
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, chris.jerdonek, jayyin11043, yan12125
Priority: normal Keywords: patch

Created on 2018-01-24 05:27 by chris.jerdonek, last changed 2018-02-15 05:42 by jayyin11043.

Pull Requests
URL Status Linked Edit
PR 5691 open jayyin11043, 2018-02-15 05:42
Messages (7)
msg310560 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2018-01-24 05:27
This issue is to suggest enhancing sys.path to recognize path-like objects, per PEP 519.

I recently ran into an issue where a path was getting added to sys.path, but the corresponding imports weren't working as they should, even though sys.path showed the path as being present. Eventually I tracked this down to the path being a pathlib.Path object rather than a string. This wasn't obvious because Path objects appear as strings in normal debug output, etc.

The sys.path docs currently say this:

> Only strings and bytes should be added to sys.path; all other data types are ignored during import.
msg311693 - (view) Author: Jay Yin (jayyin11043) * Date: 2018-02-05 21:23
This looks a lot like https://bugs.python.org/issue32446, I'd like to tackle this, if we are going through with it.
msg311864 - (view) Author: Jay Yin (jayyin11043) * Date: 2018-02-09 03:49
what file(s) is/are the sys.path code located in?
msg311895 - (view) Author: Chih-Hsuan Yen (yan12125) * Date: 2018-02-09 16:11
> what file(s) is/are the sys.path code located in?

If I understand it correctly, sys.path is handled in importlib._bootstrap_external.PathFinder.find_spec(). I can patch PathFinder for handling path-like objects: https://github.com/yan12125/cpython/commit/e3fd473b54cbb368533e651fd896bbc813a43924

Here's an example usage:

# t.py
import pathlib
import sys

sys.path.append(pathlib.Path('foo'))

import s

# foo/s.py
print(123)
msg311986 - (view) Author: Jay Yin (jayyin11043) * Date: 2018-02-11 08:49
Thanks for the reply
msg312070 - (view) Author: Jay Yin (jayyin11043) * Date: 2018-02-12 19:44
ok, so I found the PathFinder class you referenced, just making sure, this issue pertains to changing "self.path"'s usage and declaration to be a path-like object instead of the hard coded 'sys', 'path' returns? or is that part of it already?, also since this uses a tuple to keep track of the parent path, is there functionality in the path-like objects to find parent paths?
msg312072 - (view) Author: Jay Yin (jayyin11043) * Date: 2018-02-12 19:51
https://github.com/python/cpython/blob/3c34aad4e7a95913ec7db8e5e948a8fc69047bf7/Lib/importlib/_bootstrap_external.py#L1069-L1090

those are the particular class and lines I'm referring to
History
Date User Action Args
2018-02-15 05:42:58jayyin11043setkeywords: + patch
stage: patch review
pull_requests: + pull_request5484
2018-02-12 19:51:22jayyin11043setmessages: + msg312072
2018-02-12 19:44:51jayyin11043setmessages: + msg312070
2018-02-11 08:49:54jayyin11043setmessages: + msg311986
2018-02-09 16:11:11yan12125setnosy: + yan12125
messages: + msg311895
2018-02-09 03:49:23jayyin11043setmessages: + msg311864
2018-02-05 21:23:01jayyin11043setnosy: + jayyin11043
messages: + msg311693
2018-01-24 05:27:12chris.jerdonekcreate