[Python-ideas] Idea: Importing from arbitrary filenames
Steve Barnes
gadgetsteve at live.co.uk
Sun Apr 15 13:45:20 EDT 2018
On 15/04/2018 08:12, Nick Coghlan wrote:
> 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)
>
> then:
>
> 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.
>
> Cheers,
> Nick.
>
Thanks Nick,
As you say not too discoverable at the moment - I have just reread
PEP328 & https://docs.python.org/3/library/importlib.html but did not
find any mention of these mechanisms or even that setting an external
__path__ variable existed as a possibility.
Maybe a documentation enhancement proposal would be in order?
--
Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect
those of my employer.
---
This email has been checked for viruses by AVG.
http://www.avg.com
More information about the Python-ideas
mailing list