简体   繁体   中英

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?

code已经是一个Python模块,即正在导入的内容,而不是您的文件

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM