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

Cameron Simpson cs at zip.com.au
Fri Feb 3 20:10:29 EST 2017


On 04Feb2017 10:13, Ben Finney <ben+python at benfinney.id.au> 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’.
>
>So, for the past ten years and more, Python supports import of modules
>from the current directory with an explicit *relative* path::
>
>    # Absolute imports, searching ‘sys.path’.
>    import datetime
>    from collections import namedtuple
>
>    # Relative imports, starting from this module's directory.
>    from . import foo
>    from .bar import baz
>
>See <URL:https://www.python.org/dev/peps/pep-0328/>, in particular
><URL:https://www.python.org/dev/peps/pep-0328/#guido-s-decision>.

I wrote:
>Yes, I know Python likes to search
>the current directory for modules, notmy favourite feature.

I stand corrected.

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list