Search code examples
pythonnamingconvention

Python Same Module Name Convention


In reference to this question:
python: Two modules and classes with the same name under different packages

Should all modules in a package be uniquely named, regardless of nesting? PEP8 and PEP423 do not seem to address this.


Solution

  • No, there is no requirement that names at different levels must be different. Each level is a separate namespace. If foo.utils and foo.bar.utils make sense in your project, just do so.

    For example, the Python standard library has email.message and email.mime.message, and multiprocessing.connection, as well as multiprocessing.dummy.connection, and many more:

    $ ls ~/Development/Library/cpython/Lib/**/*.py  | grep -v __ | grep -v test_ | xargs basename | sort | uniq -c | grep -v '  1 ' | sort
       2 abc.py
       2 ascii.py
       2 client.py
       2 connection.py
       2 constants.py
       2 dump.py
       2 errors.py
       2 filelist.py
       2 handlers.py
       2 log.py
       2 message.py
       2 parse.py
       2 parser.py
       2 process.py
       2 queues.py
       2 server.py
       2 spawn.py
       2 text.py
       2 tree.py
       3 main.py
       4 config.py
       5 support.py
       6 util.py
    

    That's all modules that appear inside packages, appear more than once, excluding tests, __init__.py and __main__.py.