[Python-ideas] Idea: Importing from arbitrary filenames

Nick Coghlan ncoghlan at gmail.com
Sun Apr 15 03:12:35 EDT 2018

On 14 April 2018 at 19:22, Steve Barnes <gadgetsteve at live.co.uk> wrote:
> I generally love the current import system for "just working" regardless
> of platform, installation details, etc., but what I would like to see is
> a clear import local, (as opposed to import from wherever you can find
> something to satisfy mechanism). This is the one thing that I miss from
> C/C++ where #include <x> is system includes and #include "x" search
> differing include paths, (if used well).

For the latter purpose, we prefer that folks use either explicit
relative imports (if they want to search the current package
specifically), or else direct manipulation of package.__path__.

That is, if you do:

    from . import custom_imports # Definitely from your own project
    custom_imports.__path__[:] = (some_directory, some_other_directory)


    from .custom_imports import name

will search those directories for packages & modules to import, while
still cleanly mapping to a well-defined location in the module
namespace for the process as a whole (and hence being able to use all
the same caches as other imports, without causing name conflicts or
other problems).

If you want to do this dynamically relative to the current module,
then it's possible to do:

    global __path__
    __path__[:] = (some_directory, some_other_directory)
    custom_mod = importlib.import_module(".name", package=__name__)

The discoverability of these kinds of techniques could definitely
stand to be improved, but the benefit of adopting them is that they
work on all currently supported versions of Python (even
importlib.import_module exists in Python 2.7 as a convenience wrapper
around __import__), rather than needing to wait for new language level
syntax for them.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list