[Python-ideas] Policy for altering sys.path
Thomas Güttler
guettliml at thomas-guettler.de
Thu May 7 16:51:36 CEST 2015
Am 07.05.2015 um 09:22 schrieb Florian Bruhin:
> * Thomas Güttler <guettliml at thomas-guettler.de> [2015-05-07 08:59:10 +0200]:
>>
>>
>> Am 07.05.2015 um 08:48 schrieb Florian Bruhin:
>>> * Thomas Güttler <guettliml at thomas-guettler.de> [2015-05-07 08:00:09 +0200]:
>>>> Am 06.05.2015 um 17:07 schrieb Paul Moore:
>>>>> 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?
>>>>
>>>> I forgot to explain the why I use a custom class. Sorry, here is the background.
>>>>
>>>> I want sys.path to ordered:
>>>>
>>>> 1. virtualenv
>>>> 2. /usr/local/
>>>> 3. /usr/lib
>>>>
>>>> We use virtualenvs with system site-packages.
>>>>
>>>> There are many places where sys.path gets altered.
>>>>
>>>> The last time we had sys.path problems I tried to write a test
>>>> which checks that sys.path is the same for cron jobs and web requests.
>>>> I failed. Too many places, I could not find all the places
>>>> and the conditions where sys.path got modified in a different way.
>>>
>>> It looks like you explained *how* you do what you do, but not *why* -
>>> what problem is this solving? Why can't you just invoke the
>>> virtualenv's python and let python take care of sys.path?
>>
>> I want the sys.path be ordered like it, since I want that packages of the inner
>> environment are tried first.
>>
>> Here "inner" means "upper" in the above sys.path order.
>>
>> Example: If a package is installed in the virtualenv with version 2.2 and
>> in global site packages with version 1.0, then I want the interpreter to
>> use the version from virtualenv.
>
> That's already the default virtualenv behaviour:
If this is the behaviour in your virtualenv, that's nice for you.
In my virtualenv it was not that way. There are a lot of modules which do
magic with sys.path. I guess non of them are installed in your virtualenv.
Andrew Barnet suggested to alter the module finder. That looks interesting.
Thomas
More information about the Python-ideas
mailing list