Search code examples
pythonpython-module

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. the main python file is pythonaslab.py and the module for the ascending and the descending is selectionmodule.py..the code:

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:

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'

Solution

  • You created a circular import; your pythonaslab module imports selectionmodule which imports the pythonaslab module. You end up with incomplete modules that way, don't do that.

    Remove the import pythonaslab line from selectionmodule; you are not using pythonaslab in that module.

    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.