best way to ensure './' is at beginning of sys.path?

Steve D'Aprano steve+python at pearwood.info
Fri Feb 3 20:46:04 EST 2017


On Sat, 4 Feb 2017 10:13 am, Ben Finney wrote:

> Neal Becker <ndbecker2 at gmail.com> writes:
> 
>> I want to make sure any modules I build in the current directory
>> overide any others. To do this, I'd like sys.path to always have './'
>> at the beginning.
> 
> The ‘sys.path’ list is used only for *absolute* imports. Modules in the
> current directory should not be imported with an absolute path[0]. So, the
> current directory should not be in ‘sys.path’.
[...]
> [0]: Why not? See the rationale for forcing absolute import by default
>     
<URL:https://www.python.org/dev/peps/pep-0328/#rationale-for-absolute-imports>,
>      “[I]t is not clear whether ‘import foo’ refers to a top-level
>      module or to another module inside the package.[…] It's a
>      particularly difficult problem inside packages because [without
>      explicit relative import syntax] there's no way to specify which
>      module is meant.”


I'm not sure that this reasoning applies to Neal's situation. He doesn't
seem to be talking about a single package, as such, more like he has a
bunch of unrelated libraries in a single directory. My *guess* is that he
probably has something like a ~/python or ~/scripts directory, and he likes
to cd into that directory before running one of the scripts.

I don't know what problem Neal thinks he is solving by always inserting ./
at the front of sys.path. But the rationale above doesn't seem relevant to
the *pure technical question* of inserting ./ at the start of sys.path.

 

-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list