简体   繁体   English

我的模块无法加载

[英]my module won't load

i am sorry,i am just a beginner in python language,i am quite stuck in this problem quite long.actually,i want to make a descending and ascending of list that the user input by creating a module of the descending and the ascending.but i couldn't get it work. 我很抱歉,我只是python语言的初学者,我在这个问题上困扰了很长时间。实际上,我想通过创建降序和升序模块来使用户输入的列表进行降序和升序。但我无法正常工作。 the main python file is pythonaslab.py and the module for the ascending and the descending is selectionmodule.py..the code: 主要的python文件是pythonaslab.py,升序和降序的模块是selectionmodule.py ..代码:

this is the selectionmodule: 这是选择模块:

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

And this is the main file,the pythonaslab: 这是主文件pythonaslab:

import selectionmodule
a = int(input())
b = [int(input()) for _ in range(a)]
print b
print "1.ascending 2.descending"
c=input()
if c==1:
        selectionmodule.ascendingselection()
if c==2:
        selectionmodule.descendingselection()

can you point me where's the cause of all this error i got? 您能指出我所有这些错误的原因吗?

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'

You created a circular import; 您创建了一个循环导入; your pythonaslab module imports selectionmodule which imports the pythonaslab module. 您的pythonaslab模块将导入selectionmodule ,该模块将导入pythonaslab模块。 You end up with incomplete modules that way, don't do that. 这样最终会导致模块不完整,请不要这样做。

Remove the import pythonaslab line from selectionmodule ; selectionmodule删除import pythonaslab行; you are not using pythonaslab in that module. 您没有在该模块中使用pythonaslab

Also, another module cannot read your globals; 另外,另一个模块无法读取您的全局变量; you need to pass those in as arguments: 您需要将它们作为参数传递:

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

then call that with: 然后调用:

selectionmodule.ascendingselection(b)

Note that you are not limited to one-letter variable names. 请注意,您不仅限于一个字母的变量名。 Using longer, descriptive names makes your code more readable. 使用更长的描述性名称可以使您的代码更具可读性。

if you don't want to use module name such as: 如果您不想使用模块名称,例如:

selectionmodule.ascendingselection(b)

you should import : 您应该导入:

from selectionmodule import *

then you can call: 那么您可以致电:

ascendingselection(b) # without module name

Or you can import your module and assigne a alias name: 或者,您可以导入模块并分配别名:

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

for more information read: import confusaion 有关更多信息,请阅读: import confusaion

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

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