Title: `._pth` does not allow to populate `sys.path` with empty entry
Type: behavior Stage:
Components: Documentation, Windows Versions: Python 3.8, Python 3.7, Python 3.6
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, excitoon, paul.moore, ssapin, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2018-05-30 17:12 by excitoon, last changed 2018-09-29 07:18 by ssapin.

Pull Requests
URL Status Linked Edit
PR 7243 open excitoon, 2018-05-30 17:12
Messages (4)
msg318202 - (view) Author: Vladimir Chebotarev (excitoon) * Date: 2018-05-30 17:12
Moving from bpo-29326
Hi Steve.

I'll try to explain what is my motivation.

I need a reliable way to run Python (not matter embedded or not) in isolated mode, but still having current directory in `sys.path` (empty entry).

Ironically I could misuse normal mode to simulate isolated mode with specified %PYTHONPATH% (which is not much handy, by the way), but hiding Windows Registry keys would look pretty hard (it is impossible to make a bat/cmd wrapper). Possibility to search %PYTHONPATH% in HKLM entries makes it even worse.

Empty entry in `sys.path` is a extremely popular thing, and many software depend on it. It looks like a bug that isolated mode cannot support such entry.

# How would I fix it.

Possibly we should allow another keyword for adding an empty entry (like we have an exclusion for `import site`).

Ideas are appreciated.
msg318211 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2018-05-30 21:45
If your ._pth includes `import site`, then including a blank line in a `anything.pth` file should add the entry.

Alternatively, if you can add "import sys; sys.path.insert(0, '')" to the start of your code that depends on it you should be fine.
msg318267 - (view) Author: Vladimir Chebotarev (excitoon) * Date: 2018-05-31 08:45
Unfortunately this is not true: "Blank lines and lines beginning with # are skipped." (
I'm not sure we should change this behavior as it would make huge impact on usage of `pth` files by users (`._pth`, on the contrary, is the only one per distribution and managed by the distribution).

I could easily add `''` to `sys.path` manually if I had one script or even dozen of them, but as the solution that alternative is not acceptable. :(
msg318272 - (view) Author: Paul Moore (paul.moore) * (Python committer) Date: 2018-05-31 09:02
You could add the line

    import sys; sys.path.insert(0, '')

to a .pth file. The documentation you pointed out states that lines starting with "import" are executed...
Date User Action Args
2018-09-29 07:18:58ssapinsetnosy: + ssapin
2018-05-31 09:02:46paul.mooresetmessages: + msg318272
2018-05-31 08:45:08excitoonsetmessages: + msg318267
2018-05-30 21:45:56steve.dowersetmessages: + msg318211
2018-05-30 17:32:53excitoonsettitle: `._pth` does not allow to populate `sys.path` empty entry -> `._pth` does not allow to populate `sys.path` with empty entry
2018-05-30 17:12:41excitooncreate