我的模块无法加载

2024-10-02 22:25:21 发布

您现在位置:Python中文网/ 问答频道 /正文

对不起,我只是一个python语言的初学者,我很困在这个问题上很长。实际上,我想创建一个由降序和上升。但是我不能让它工作。 python的主文件是pythonaslab.py上升和下降的模块是选择模块.py…代码:

这是选择模块:

import pythonaslab
def ascendingselection():
        for q in range(len(b)):
                w=q+1
                for w in range(len(b)):
                        if b[q]>b[w]:
                                f=b[q]
                                b[q]=b[w]
                                b[w]=f
        print b
def descendingselection():
        for q in range(len(b)):
                w=q+1
                for w in range(len(b)):
                        if b[q]<b[w]:
                                f=b[q]
                                b[q]=b[w]
                                b[w]=f
        print b

这是主文件,Python实验室:

^{pr2}$

你能告诉我这些错误的原因在哪里吗?在

Traceback (most recent call last):
  File "E:\Coding\pythonaslab.py", line 1, in <module>
    import selectionmodule
  File "E:\Coding\selectionmodule.py", line 1, in <module>
    import pythonaslab
  File "E:\Coding\pythonaslab.py", line 16, in <module>
    selectionmodule.descendingselection()
AttributeError: 'module' object has no attribute 'descendingselection'

Tags: 模块文件inpyimportforlenline
2条回答

您创建了一个循环导入;您的pythonaslab模块导入selectionmodule,该模块导入pythonaslab模块。你最终得到的是不完整的模块,不要这样做。在

selectionmodule中删除import pythonaslab行;该模块中没有使用pythonaslab。在

另外,另一个模块无法读取您的全局参数;您需要将它们作为参数传入:

# this function takes one argument, and locally it is known as b
def ascendingselection(b):
    # rest of function ..

那就叫它:

^{pr2}$

请注意,变量名不限于一个字母。使用较长的描述性名称可以使代码更具可读性。在

如果不想使用模块名称,例如:

selectionmodule.ascendingselection(b)

您应该导入:

^{pr2}$

你可以打电话给:

ascendingselection(b) # without module name

也可以导入模块并指定别名:

import selectionmodule as o
o.ascendingselection(b) # with alias name

更多信息请阅读:import confusaion

相关问题 更多 >