简体   繁体   English

无法从名为“代码”的模块中导入任何功能

[英]Can't import any functions from module named 'code'

I created a module named code: code.py and wrote a simple function: 我创建了一个名为code:code.py的模块,并编写了一个简单的函数:

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'. 我收到以下错误:AttributeError:模块“代码”没有属性“ main”。

Interestingly enough, if I rename the module to foo.py, this code works just fine: 有趣的是,如果我将模块重命名为foo.py,则此代码可以正常工作:

import foo
foo.main()

Code isn't a reserved word so why can't I name a module code? 代码不是保留字,为什么我不能命名模块代码?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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