[Python-ideas] Add 'module' module, similar to 'keyword' module

Steven D'Aprano steve at pearwood.info
Sat Oct 31 04:14:51 EDT 2015


On Fri, Oct 30, 2015 at 09:09:00PM -0400, Terry Reedy wrote:

> A module module would have at least liblist and islibmodule function. 
> Liblist would contain all directories with __init__.py and all .py 
> files.  

`liblist` cannot usefully be a statically created list, since the 
availability of modules on the path is dynamic: the path can change, and 
files can be added or removed.

Nor can it be limited to .py files, since .pyc and other extensions can 
be imported.

I don't know what `islibmodule` is supposed to do. Surely all libraries 
are modules?


> (I don't think files within package directories should be 
> included, as there is no direct shadowing problem.)

Sounds like you want a private "fix_shadowing_of_modules_for_IDLE" 
module, rather than a general-purpose "module" module. For a general 
purpose "module" module, whether or not there is a shadowing problem is 
irrelevant.

By the way, it should be clear from the above that the name "module" is 
atrocious -- it makes it awkward to talk about the module, and it will 
itself be shadowed by anyone (especially beginners) who create a 
"module.py" file. "modtools" might be a better name.


> A python oriented 
> editor could then warn on save requests "This name matches a stdlib name 
> in /Lib. If you run python in this directory, you will not be able to 
> import the stdlib module.  Continue?".

I don't think this is much of a solution to the shadowing problem. For 
starters, it relies on people using a specific editor. It assumes that 
files won't be renamed or moved outside of the editor. It depends on the 
user actually paying attention and reading the error message, which 
beginners notoriously don't do. Many people will either blindly continue 
(and hence shadow), or dismiss the dialog and then get into a flap that 
they can't save their work.

> The module should also have binlist and isbinmodule for builtin modules. 
>  (I do not know how to get such a list.  If necessary, an api could be 
> added.)  An editor could than warn "This name matches a builtin stdlib 
> name.  You will not be able to import this file.  Continue?".

binlist is easy: sys.builtin_module_names.

I don't know what `isbinmodule` means either. Presumably you don't 
actually mean "is binary module", but "is builtin module". I believe 
that the canonical way to do that is hasattr(module, "__file__").


-- 
Steve


More information about the Python-ideas mailing list