Search code examples
pythonpython-importpython-module

Can't import any functions from module named 'code'


I created a module named code: code.py and wrote a simple function:

def main():
    print("simple function")

I tried dropping this file inside site-packages and when I run:

import code
code.main()

I get the following error: AttributeError: module 'code' has no attribute 'main'.

Interestingly enough, if I rename the module to foo.py, this code works just fine:

import foo
foo.main()

Code isn't a reserved word so why can't I name a module code?


Solution

  • code is a Python module already, that is what is being imported, not your file