Skip to content

Python Gotchas

Python Gotchas#

Do Not Name Modules after Python Built-in#

You may get an error like this:

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/", line 142, in _get_module_details
    return _get_module_details(pkg_main_name, error)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/", line 109, in _get_module_details
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/", line 59, in <module>
    from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf,
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/", line 6, in <module>
    import logging
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/", line 210, in <module>
    _lock = threading.RLock()
AttributeError: module 'threading' has no attribute 'RLock'

It will then say:

Exception ignored in: <module 'threading' from '/my/python/project/threading/'>
AttributeError: module 'threading' has no attribute '_shutdown'

This means the standard library is using my local version of threading. It is a naming conflict. You probably have a folder with a inside it that is called threading/ or you have a file called threading.

Generally, its not a good idea to name python scripts after builtin scripts.

You can get a list of keywords and builtins with:

import keyword
>>> keyword.kwlist

import builtins


or anything in the Lib folder of python