A question related to the PYTHONPATH

oyono adrienoyono at gmail.com
Mon Mar 26 19:52:46 EDT 2018


Le lundi 26 mars 2018 08:11:02 UTC+2, dieter a écrit :
> adrien oyono <adrienoyono at gmail.com> writes:
> > I have recently read the documentation about how imports work on python,
> > and I was wondering why, when you execute a python file, the current
> > directory is not added by default to the PYTHONPATH ?
> 
> Maybe, to avoid surprises?
> 
> You can invoke a script from different positions in your file system.
> If PYTHONPATH would automatically get ".", the script's behaviour
> could depend on the position from where it is called.
> 
> Prepending "." to "PYTHONPATH" could have dramatic effects:
> assume your script uses module "xxx" and your current working
> directory accidentally has a "xxx.py"; then this "xxx.py" would
> be used and it may behave much differently from another one.
> 
> The problem is not so severe when "." is appended to "PYTHONPATH".
> Nevertheless, it likely is not a good idea to have script behaviour
> depending by default on the execution location.

Thank you Dieter.
I was thinking, maybe it could have been done this way to enforce not running module files that are supposed to be bundled into packages as "independant" python scripts...Therefore, running "python script.py" should be reserved to effectively independant python scripts that do not import from a user-created package or module.
I think I need to learn a little bit more about the python cli.
Many thanks again



More information about the Python-list mailing list