[Python-ideas] Policy for altering sys.path

Paul Moore p.f.moore at gmail.com
Wed May 6 17:07:52 CEST 2015


On 6 May 2015 at 15:05, Thomas Güttler <guettliml at thomas-guettler.de> wrote:
> I am missing a policy how sys.path should be altered.

Well, the docs say that applications can modify sys.path as needed.
Generally, applications modify sys.path in place via sys.path[:] =
whatever, but that's not mandated as far as I know.

> We run a custom sub class of list in sys.path. We set it in sitecustomize.py

Can you explain why? It seems pretty risky to expect that no
applications will replace sys.path. I understand that you're proposing
that we say that applications shouldn't do that - but just saying so
won't change the many applications already out there.

> This instance get replace by a common list in lines like this:
>
> sys.path = glob.glob(os.path.join(WHEEL_DIR, "*.whl")) + sys.path
>
> The above line is from pip, it similar things happen in a lot of packages.

How does the fact that pip does that cause a problem? The sys.path
modification is only in effect while pip is running, and no code in
pip relies on sys.path being an instance of your custom class.

> Before trying to solve this with code, I think the python community should
> agree an a policy for altering sys.path.

I can't imagine that happening, and even if it does, it won't make any
difference because a new policy won't change existing code. It won't
even affect new code unless people know about it (which isn't certain
- I doubt many people read the documentation that closely).

> What can I do to this done?

I doubt you can.

A PR for pip that changes the above line to modify sys.path in place
would probably get accepted (I can't see any reason why it wouldn't),
and I guess you could do the same for any other code you find. But as
for persuading the Python programming community not to replace
sys.path in any code, that seems unlikely to happen.

> We use Python 2.7

If you were using 3.x, then it's (barely) conceivable that making
sys.path read-only (so people could only modify it in-place) could be
done as a new feature, but (a) it would be a major backward
compatibility break, so there would have to be a strong justification,
and (b) it would stop you from replacing sys.path with your custom
class in the first place, so it wouldn't solve your issue.

Which also raises the question, why do you believe it's OK to forbid
other people to replace sys.path when that's what you're doing in your
sitecustomize code? That seems self-contradictory...

Paul


More information about the Python-ideas mailing list