[Python-ideas] Idea: Importing from arbitrary filenames

Nick Coghlan ncoghlan at gmail.com
Sat Apr 14 01:27:28 EDT 2018


On 14 April 2018 at 13:28, Ken Hilton <kenlhilton at gmail.com> wrote:
> Hi all,
>
> First of all, please excuse me if I'm presenting this idea in the wrong way
> or at the wrong time - I'm new to this mailing list and haven't seen anyone
> propose a new idea on it yet, so I don't know the customs.
>
> I have an idea for importing files with arbitrary names. Currently, the
> "official" way to import arbitrary files is to use the "imp" module, as
> shown by this answer: https://stackoverflow.com/a/3137914/6605349
> However, this method takes two function calls and is not as (aesthetically
> pleasing? is that the word?) as a simple "import" statement.

Modules aren't required to be stored on the filesystem, so we have no
plans to offer this.

`runpy.run_path()` exists to let folks run arbitrary Python files and
collect the resulting namespace, while if folks really want to
implement pseudo-imports based on filenames we expose the necessary
building blocks in importlib
(https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly)

The fact that run_path() has a nice straightforward invocation model,
and the import emulation recipe doesn't is intended as a hint :)

Cheers,
Nick.

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


More information about the Python-ideas mailing list