AttributeError: 'module' object has no attribute












154















I have two python modules:



a.py



import b

def hello():
print "hello"

print "a.py"
print hello()
print b.hi()


b.py



import a

def hi():
print "hi"


When I run a.py, I get:



AttributeError: 'module' object has no attribute 'hi'


What does the error mean? How do I fix it?










share|improve this question




















  • 13





    This is an awful design. WHy does b.py import a when it doesn't reference any part of a.py? Are you asking how to fix this problem? stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

    – S.Lott
    Aug 8 '09 at 23:40











  • Note that your questions is very similar to this answer. Apparently the code in this answer works just find, but yours does not? stackoverflow.com/a/7336880/565879

    – Buttons840
    May 2 '14 at 18:15
















154















I have two python modules:



a.py



import b

def hello():
print "hello"

print "a.py"
print hello()
print b.hi()


b.py



import a

def hi():
print "hi"


When I run a.py, I get:



AttributeError: 'module' object has no attribute 'hi'


What does the error mean? How do I fix it?










share|improve this question




















  • 13





    This is an awful design. WHy does b.py import a when it doesn't reference any part of a.py? Are you asking how to fix this problem? stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

    – S.Lott
    Aug 8 '09 at 23:40











  • Note that your questions is very similar to this answer. Apparently the code in this answer works just find, but yours does not? stackoverflow.com/a/7336880/565879

    – Buttons840
    May 2 '14 at 18:15














154












154








154


33






I have two python modules:



a.py



import b

def hello():
print "hello"

print "a.py"
print hello()
print b.hi()


b.py



import a

def hi():
print "hi"


When I run a.py, I get:



AttributeError: 'module' object has no attribute 'hi'


What does the error mean? How do I fix it?










share|improve this question
















I have two python modules:



a.py



import b

def hello():
print "hello"

print "a.py"
print hello()
print b.hi()


b.py



import a

def hi():
print "hi"


When I run a.py, I get:



AttributeError: 'module' object has no attribute 'hi'


What does the error mean? How do I fix it?







python attributeerror






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 7 '13 at 22:05









Eric Leschinski

89.7k40330281




89.7k40330281










asked Aug 8 '09 at 23:12









Stephen HsuStephen Hsu

2,27462438




2,27462438








  • 13





    This is an awful design. WHy does b.py import a when it doesn't reference any part of a.py? Are you asking how to fix this problem? stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

    – S.Lott
    Aug 8 '09 at 23:40











  • Note that your questions is very similar to this answer. Apparently the code in this answer works just find, but yours does not? stackoverflow.com/a/7336880/565879

    – Buttons840
    May 2 '14 at 18:15














  • 13





    This is an awful design. WHy does b.py import a when it doesn't reference any part of a.py? Are you asking how to fix this problem? stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

    – S.Lott
    Aug 8 '09 at 23:40











  • Note that your questions is very similar to this answer. Apparently the code in this answer works just find, but yours does not? stackoverflow.com/a/7336880/565879

    – Buttons840
    May 2 '14 at 18:15








13




13





This is an awful design. WHy does b.py import a when it doesn't reference any part of a.py? Are you asking how to fix this problem? stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

– S.Lott
Aug 8 '09 at 23:40





This is an awful design. WHy does b.py import a when it doesn't reference any part of a.py? Are you asking how to fix this problem? stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

– S.Lott
Aug 8 '09 at 23:40













Note that your questions is very similar to this answer. Apparently the code in this answer works just find, but yours does not? stackoverflow.com/a/7336880/565879

– Buttons840
May 2 '14 at 18:15





Note that your questions is very similar to this answer. Apparently the code in this answer works just find, but yours does not? stackoverflow.com/a/7336880/565879

– Buttons840
May 2 '14 at 18:15












9 Answers
9






active

oldest

votes


















159














You have mutual top-level imports, which is almost always a bad idea.



If you really must have mutual imports in Python, the way to do it is to import them within a function:



# In b.py:
def cause_a_to_do_something():
import a
a.do_something()


Now a.py can safely do import b without causing problems.



(At first glance it might appear that cause_a_to_do_something() would be hugely inefficient because it does an import every time you call it, but in fact the import work only gets done the first time. The second and subsequent times you import a module, it's a quick operation.)






share|improve this answer
























  • Thank you! I know for the first time that importing in different places make such a difference.

    – Stephen Hsu
    Aug 9 '09 at 0:58











  • Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.

    – Rebs
    Nov 20 '15 at 3:03






  • 4





    Interesting; I wonder why the interpreter does not give a proper error message in this case?

    – Haroldo_OK
    May 17 '16 at 1:07



















77














I have also seen this error when inadvertently naming a module with the same name as one of the standard Python modules. E.g. I had a module called commands which is also a Python library module. This proved to be difficult to track down as it worked correctly on my local development environment but failed with the specified error when running on Google App Engine.






share|improve this answer



















  • 6





    this is the answer that solved my problem.

    – Tommy
    Aug 17 '15 at 18:46








  • 2





    I used abc.py to write a test to demonstrate the import behavior in python, that bites me a lot...

    – Bin
    Sep 23 '16 at 20:57






  • 2





    I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.

    – Echelon
    Apr 26 '17 at 21:23











  • I created a math module , which is already standard module.

    – refactor
    Nov 8 '17 at 16:31



















40














The problem is the circular dependency between the modules. a imports b and b imports a. But one of them needs to be loaded first - in this case python ends up initializing module a before b and b.hi() doesn't exist yet when you try to access it in a.






share|improve this answer
























  • Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?

    – Stephen Hsu
    Aug 8 '09 at 23:25






  • 1





    @Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

    – S.Lott
    Aug 8 '09 at 23:39











  • @S.Lott: Thank you. I just know that it is a circular dependencies problem.

    – Stephen Hsu
    Aug 9 '09 at 1:01



















16














I got this error by referencing an enum which was imported in a wrong way, e.g.:



from package import MyEnumClass
# ...
# in some method:
return MyEnumClass.Member


Correct import:



from package.MyEnumClass import MyEnumClass


Hope that helps someone






share|improve this answer































    7














    I experienced this error because the module was not actually imported. The code looked like this:



    import a.b, a.c

    # ...

    something(a.b)
    something(a.c)
    something(a.d) # My addition, which failed.


    The last line resulted in an AttributeError. The cause was that I had failed to notice that the submodules of a (a.b and a.c) were explicitly imported, and assumed that the import statement actually imported a.






    share|improve this answer































      3














      I ran into this problem when I checked out an older version of a repository from git. Git replaced my .py files, but left the untracked .pyc files. Since the .py files and .pyc files were out of sync, the import command in a .py file could not find the corresponding module in the .pyc files.



      The solution was simply to delete the .pyc files, and let them be automatically regenerated.






      share|improve this answer
























      • You can use this command to delete all .pyc files: find . -name "*.pyc" -exec rm -f {} ;

        – Ollie
        Jan 24 at 2:26



















      2














      All the above answers are great, but I'd like to chime in here. If you did not spot any issue mentioned above, try clear up your working environment. It worked for me.






      share|improve this answer































        0














        Not sure how but the below change sorted my issue:



        i was having the name of file and import name same for eg i had file name as emoji.py and i was trying to import emoji. But changing the name of file solved the issue .



        Hope so it helps






        share|improve this answer































          0














          Circular imports cause problems, but Python has ways to mitigate it built-in.



          The problem is when you run python a.py, it runs a.py but not mark it imported as a module. So in turn a.py -> imports module b -> imports module a -> imports module b. The last import a no-op since b is currently being imported and Python guards against that. And b is an empty module for now. So when it executes b.hi(), it can't find anything.



          Note that the b.hi() that got executed is during a.py -> module b -> module a, not in a.py directly.



          In your specific example, you can just run python -c 'import a' at top-level, so the first execution of a.py is registered as importing a module.






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1250103%2fattributeerror-module-object-has-no-attribute%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            9 Answers
            9






            active

            oldest

            votes








            9 Answers
            9






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            159














            You have mutual top-level imports, which is almost always a bad idea.



            If you really must have mutual imports in Python, the way to do it is to import them within a function:



            # In b.py:
            def cause_a_to_do_something():
            import a
            a.do_something()


            Now a.py can safely do import b without causing problems.



            (At first glance it might appear that cause_a_to_do_something() would be hugely inefficient because it does an import every time you call it, but in fact the import work only gets done the first time. The second and subsequent times you import a module, it's a quick operation.)






            share|improve this answer
























            • Thank you! I know for the first time that importing in different places make such a difference.

              – Stephen Hsu
              Aug 9 '09 at 0:58











            • Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.

              – Rebs
              Nov 20 '15 at 3:03






            • 4





              Interesting; I wonder why the interpreter does not give a proper error message in this case?

              – Haroldo_OK
              May 17 '16 at 1:07
















            159














            You have mutual top-level imports, which is almost always a bad idea.



            If you really must have mutual imports in Python, the way to do it is to import them within a function:



            # In b.py:
            def cause_a_to_do_something():
            import a
            a.do_something()


            Now a.py can safely do import b without causing problems.



            (At first glance it might appear that cause_a_to_do_something() would be hugely inefficient because it does an import every time you call it, but in fact the import work only gets done the first time. The second and subsequent times you import a module, it's a quick operation.)






            share|improve this answer
























            • Thank you! I know for the first time that importing in different places make such a difference.

              – Stephen Hsu
              Aug 9 '09 at 0:58











            • Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.

              – Rebs
              Nov 20 '15 at 3:03






            • 4





              Interesting; I wonder why the interpreter does not give a proper error message in this case?

              – Haroldo_OK
              May 17 '16 at 1:07














            159












            159








            159







            You have mutual top-level imports, which is almost always a bad idea.



            If you really must have mutual imports in Python, the way to do it is to import them within a function:



            # In b.py:
            def cause_a_to_do_something():
            import a
            a.do_something()


            Now a.py can safely do import b without causing problems.



            (At first glance it might appear that cause_a_to_do_something() would be hugely inefficient because it does an import every time you call it, but in fact the import work only gets done the first time. The second and subsequent times you import a module, it's a quick operation.)






            share|improve this answer













            You have mutual top-level imports, which is almost always a bad idea.



            If you really must have mutual imports in Python, the way to do it is to import them within a function:



            # In b.py:
            def cause_a_to_do_something():
            import a
            a.do_something()


            Now a.py can safely do import b without causing problems.



            (At first glance it might appear that cause_a_to_do_something() would be hugely inefficient because it does an import every time you call it, but in fact the import work only gets done the first time. The second and subsequent times you import a module, it's a quick operation.)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 8 '09 at 23:27









            RichieHindleRichieHindle

            200k40302370




            200k40302370













            • Thank you! I know for the first time that importing in different places make such a difference.

              – Stephen Hsu
              Aug 9 '09 at 0:58











            • Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.

              – Rebs
              Nov 20 '15 at 3:03






            • 4





              Interesting; I wonder why the interpreter does not give a proper error message in this case?

              – Haroldo_OK
              May 17 '16 at 1:07



















            • Thank you! I know for the first time that importing in different places make such a difference.

              – Stephen Hsu
              Aug 9 '09 at 0:58











            • Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.

              – Rebs
              Nov 20 '15 at 3:03






            • 4





              Interesting; I wonder why the interpreter does not give a proper error message in this case?

              – Haroldo_OK
              May 17 '16 at 1:07

















            Thank you! I know for the first time that importing in different places make such a difference.

            – Stephen Hsu
            Aug 9 '09 at 0:58





            Thank you! I know for the first time that importing in different places make such a difference.

            – Stephen Hsu
            Aug 9 '09 at 0:58













            Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.

            – Rebs
            Nov 20 '15 at 3:03





            Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.

            – Rebs
            Nov 20 '15 at 3:03




            4




            4





            Interesting; I wonder why the interpreter does not give a proper error message in this case?

            – Haroldo_OK
            May 17 '16 at 1:07





            Interesting; I wonder why the interpreter does not give a proper error message in this case?

            – Haroldo_OK
            May 17 '16 at 1:07













            77














            I have also seen this error when inadvertently naming a module with the same name as one of the standard Python modules. E.g. I had a module called commands which is also a Python library module. This proved to be difficult to track down as it worked correctly on my local development environment but failed with the specified error when running on Google App Engine.






            share|improve this answer



















            • 6





              this is the answer that solved my problem.

              – Tommy
              Aug 17 '15 at 18:46








            • 2





              I used abc.py to write a test to demonstrate the import behavior in python, that bites me a lot...

              – Bin
              Sep 23 '16 at 20:57






            • 2





              I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.

              – Echelon
              Apr 26 '17 at 21:23











            • I created a math module , which is already standard module.

              – refactor
              Nov 8 '17 at 16:31
















            77














            I have also seen this error when inadvertently naming a module with the same name as one of the standard Python modules. E.g. I had a module called commands which is also a Python library module. This proved to be difficult to track down as it worked correctly on my local development environment but failed with the specified error when running on Google App Engine.






            share|improve this answer



















            • 6





              this is the answer that solved my problem.

              – Tommy
              Aug 17 '15 at 18:46








            • 2





              I used abc.py to write a test to demonstrate the import behavior in python, that bites me a lot...

              – Bin
              Sep 23 '16 at 20:57






            • 2





              I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.

              – Echelon
              Apr 26 '17 at 21:23











            • I created a math module , which is already standard module.

              – refactor
              Nov 8 '17 at 16:31














            77












            77








            77







            I have also seen this error when inadvertently naming a module with the same name as one of the standard Python modules. E.g. I had a module called commands which is also a Python library module. This proved to be difficult to track down as it worked correctly on my local development environment but failed with the specified error when running on Google App Engine.






            share|improve this answer













            I have also seen this error when inadvertently naming a module with the same name as one of the standard Python modules. E.g. I had a module called commands which is also a Python library module. This proved to be difficult to track down as it worked correctly on my local development environment but failed with the specified error when running on Google App Engine.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 26 '10 at 14:18









            lucrusselllucrussell

            4,08312636




            4,08312636








            • 6





              this is the answer that solved my problem.

              – Tommy
              Aug 17 '15 at 18:46








            • 2





              I used abc.py to write a test to demonstrate the import behavior in python, that bites me a lot...

              – Bin
              Sep 23 '16 at 20:57






            • 2





              I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.

              – Echelon
              Apr 26 '17 at 21:23











            • I created a math module , which is already standard module.

              – refactor
              Nov 8 '17 at 16:31














            • 6





              this is the answer that solved my problem.

              – Tommy
              Aug 17 '15 at 18:46








            • 2





              I used abc.py to write a test to demonstrate the import behavior in python, that bites me a lot...

              – Bin
              Sep 23 '16 at 20:57






            • 2





              I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.

              – Echelon
              Apr 26 '17 at 21:23











            • I created a math module , which is already standard module.

              – refactor
              Nov 8 '17 at 16:31








            6




            6





            this is the answer that solved my problem.

            – Tommy
            Aug 17 '15 at 18:46







            this is the answer that solved my problem.

            – Tommy
            Aug 17 '15 at 18:46






            2




            2





            I used abc.py to write a test to demonstrate the import behavior in python, that bites me a lot...

            – Bin
            Sep 23 '16 at 20:57





            I used abc.py to write a test to demonstrate the import behavior in python, that bites me a lot...

            – Bin
            Sep 23 '16 at 20:57




            2




            2





            I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.

            – Echelon
            Apr 26 '17 at 21:23





            I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.

            – Echelon
            Apr 26 '17 at 21:23













            I created a math module , which is already standard module.

            – refactor
            Nov 8 '17 at 16:31





            I created a math module , which is already standard module.

            – refactor
            Nov 8 '17 at 16:31











            40














            The problem is the circular dependency between the modules. a imports b and b imports a. But one of them needs to be loaded first - in this case python ends up initializing module a before b and b.hi() doesn't exist yet when you try to access it in a.






            share|improve this answer
























            • Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?

              – Stephen Hsu
              Aug 8 '09 at 23:25






            • 1





              @Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

              – S.Lott
              Aug 8 '09 at 23:39











            • @S.Lott: Thank you. I just know that it is a circular dependencies problem.

              – Stephen Hsu
              Aug 9 '09 at 1:01
















            40














            The problem is the circular dependency between the modules. a imports b and b imports a. But one of them needs to be loaded first - in this case python ends up initializing module a before b and b.hi() doesn't exist yet when you try to access it in a.






            share|improve this answer
























            • Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?

              – Stephen Hsu
              Aug 8 '09 at 23:25






            • 1





              @Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

              – S.Lott
              Aug 8 '09 at 23:39











            • @S.Lott: Thank you. I just know that it is a circular dependencies problem.

              – Stephen Hsu
              Aug 9 '09 at 1:01














            40












            40








            40







            The problem is the circular dependency between the modules. a imports b and b imports a. But one of them needs to be loaded first - in this case python ends up initializing module a before b and b.hi() doesn't exist yet when you try to access it in a.






            share|improve this answer













            The problem is the circular dependency between the modules. a imports b and b imports a. But one of them needs to be loaded first - in this case python ends up initializing module a before b and b.hi() doesn't exist yet when you try to access it in a.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 8 '09 at 23:19









            sthsth

            168k42248335




            168k42248335













            • Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?

              – Stephen Hsu
              Aug 8 '09 at 23:25






            • 1





              @Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

              – S.Lott
              Aug 8 '09 at 23:39











            • @S.Lott: Thank you. I just know that it is a circular dependencies problem.

              – Stephen Hsu
              Aug 9 '09 at 1:01



















            • Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?

              – Stephen Hsu
              Aug 8 '09 at 23:25






            • 1





              @Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

              – S.Lott
              Aug 8 '09 at 23:39











            • @S.Lott: Thank you. I just know that it is a circular dependencies problem.

              – Stephen Hsu
              Aug 9 '09 at 1:01

















            Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?

            – Stephen Hsu
            Aug 8 '09 at 23:25





            Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?

            – Stephen Hsu
            Aug 8 '09 at 23:25




            1




            1





            @Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

            – S.Lott
            Aug 8 '09 at 23:39





            @Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

            – S.Lott
            Aug 8 '09 at 23:39













            @S.Lott: Thank you. I just know that it is a circular dependencies problem.

            – Stephen Hsu
            Aug 9 '09 at 1:01





            @S.Lott: Thank you. I just know that it is a circular dependencies problem.

            – Stephen Hsu
            Aug 9 '09 at 1:01











            16














            I got this error by referencing an enum which was imported in a wrong way, e.g.:



            from package import MyEnumClass
            # ...
            # in some method:
            return MyEnumClass.Member


            Correct import:



            from package.MyEnumClass import MyEnumClass


            Hope that helps someone






            share|improve this answer




























              16














              I got this error by referencing an enum which was imported in a wrong way, e.g.:



              from package import MyEnumClass
              # ...
              # in some method:
              return MyEnumClass.Member


              Correct import:



              from package.MyEnumClass import MyEnumClass


              Hope that helps someone






              share|improve this answer


























                16












                16








                16







                I got this error by referencing an enum which was imported in a wrong way, e.g.:



                from package import MyEnumClass
                # ...
                # in some method:
                return MyEnumClass.Member


                Correct import:



                from package.MyEnumClass import MyEnumClass


                Hope that helps someone






                share|improve this answer













                I got this error by referencing an enum which was imported in a wrong way, e.g.:



                from package import MyEnumClass
                # ...
                # in some method:
                return MyEnumClass.Member


                Correct import:



                from package.MyEnumClass import MyEnumClass


                Hope that helps someone







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 16 '15 at 23:32









                StoyanStoyan

                16912




                16912























                    7














                    I experienced this error because the module was not actually imported. The code looked like this:



                    import a.b, a.c

                    # ...

                    something(a.b)
                    something(a.c)
                    something(a.d) # My addition, which failed.


                    The last line resulted in an AttributeError. The cause was that I had failed to notice that the submodules of a (a.b and a.c) were explicitly imported, and assumed that the import statement actually imported a.






                    share|improve this answer




























                      7














                      I experienced this error because the module was not actually imported. The code looked like this:



                      import a.b, a.c

                      # ...

                      something(a.b)
                      something(a.c)
                      something(a.d) # My addition, which failed.


                      The last line resulted in an AttributeError. The cause was that I had failed to notice that the submodules of a (a.b and a.c) were explicitly imported, and assumed that the import statement actually imported a.






                      share|improve this answer


























                        7












                        7








                        7







                        I experienced this error because the module was not actually imported. The code looked like this:



                        import a.b, a.c

                        # ...

                        something(a.b)
                        something(a.c)
                        something(a.d) # My addition, which failed.


                        The last line resulted in an AttributeError. The cause was that I had failed to notice that the submodules of a (a.b and a.c) were explicitly imported, and assumed that the import statement actually imported a.






                        share|improve this answer













                        I experienced this error because the module was not actually imported. The code looked like this:



                        import a.b, a.c

                        # ...

                        something(a.b)
                        something(a.c)
                        something(a.d) # My addition, which failed.


                        The last line resulted in an AttributeError. The cause was that I had failed to notice that the submodules of a (a.b and a.c) were explicitly imported, and assumed that the import statement actually imported a.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jun 24 '16 at 20:26









                        Dag HøidahlDag Høidahl

                        4,76843657




                        4,76843657























                            3














                            I ran into this problem when I checked out an older version of a repository from git. Git replaced my .py files, but left the untracked .pyc files. Since the .py files and .pyc files were out of sync, the import command in a .py file could not find the corresponding module in the .pyc files.



                            The solution was simply to delete the .pyc files, and let them be automatically regenerated.






                            share|improve this answer
























                            • You can use this command to delete all .pyc files: find . -name "*.pyc" -exec rm -f {} ;

                              – Ollie
                              Jan 24 at 2:26
















                            3














                            I ran into this problem when I checked out an older version of a repository from git. Git replaced my .py files, but left the untracked .pyc files. Since the .py files and .pyc files were out of sync, the import command in a .py file could not find the corresponding module in the .pyc files.



                            The solution was simply to delete the .pyc files, and let them be automatically regenerated.






                            share|improve this answer
























                            • You can use this command to delete all .pyc files: find . -name "*.pyc" -exec rm -f {} ;

                              – Ollie
                              Jan 24 at 2:26














                            3












                            3








                            3







                            I ran into this problem when I checked out an older version of a repository from git. Git replaced my .py files, but left the untracked .pyc files. Since the .py files and .pyc files were out of sync, the import command in a .py file could not find the corresponding module in the .pyc files.



                            The solution was simply to delete the .pyc files, and let them be automatically regenerated.






                            share|improve this answer













                            I ran into this problem when I checked out an older version of a repository from git. Git replaced my .py files, but left the untracked .pyc files. Since the .py files and .pyc files were out of sync, the import command in a .py file could not find the corresponding module in the .pyc files.



                            The solution was simply to delete the .pyc files, and let them be automatically regenerated.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 21 '18 at 2:43









                            craqcraq

                            4011723




                            4011723













                            • You can use this command to delete all .pyc files: find . -name "*.pyc" -exec rm -f {} ;

                              – Ollie
                              Jan 24 at 2:26



















                            • You can use this command to delete all .pyc files: find . -name "*.pyc" -exec rm -f {} ;

                              – Ollie
                              Jan 24 at 2:26

















                            You can use this command to delete all .pyc files: find . -name "*.pyc" -exec rm -f {} ;

                            – Ollie
                            Jan 24 at 2:26





                            You can use this command to delete all .pyc files: find . -name "*.pyc" -exec rm -f {} ;

                            – Ollie
                            Jan 24 at 2:26











                            2














                            All the above answers are great, but I'd like to chime in here. If you did not spot any issue mentioned above, try clear up your working environment. It worked for me.






                            share|improve this answer




























                              2














                              All the above answers are great, but I'd like to chime in here. If you did not spot any issue mentioned above, try clear up your working environment. It worked for me.






                              share|improve this answer


























                                2












                                2








                                2







                                All the above answers are great, but I'd like to chime in here. If you did not spot any issue mentioned above, try clear up your working environment. It worked for me.






                                share|improve this answer













                                All the above answers are great, but I'd like to chime in here. If you did not spot any issue mentioned above, try clear up your working environment. It worked for me.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jun 6 '18 at 18:21









                                JianJian

                                211




                                211























                                    0














                                    Not sure how but the below change sorted my issue:



                                    i was having the name of file and import name same for eg i had file name as emoji.py and i was trying to import emoji. But changing the name of file solved the issue .



                                    Hope so it helps






                                    share|improve this answer




























                                      0














                                      Not sure how but the below change sorted my issue:



                                      i was having the name of file and import name same for eg i had file name as emoji.py and i was trying to import emoji. But changing the name of file solved the issue .



                                      Hope so it helps






                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        Not sure how but the below change sorted my issue:



                                        i was having the name of file and import name same for eg i had file name as emoji.py and i was trying to import emoji. But changing the name of file solved the issue .



                                        Hope so it helps






                                        share|improve this answer













                                        Not sure how but the below change sorted my issue:



                                        i was having the name of file and import name same for eg i had file name as emoji.py and i was trying to import emoji. But changing the name of file solved the issue .



                                        Hope so it helps







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 13 '18 at 6:31









                                        MD5MD5

                                        472411




                                        472411























                                            0














                                            Circular imports cause problems, but Python has ways to mitigate it built-in.



                                            The problem is when you run python a.py, it runs a.py but not mark it imported as a module. So in turn a.py -> imports module b -> imports module a -> imports module b. The last import a no-op since b is currently being imported and Python guards against that. And b is an empty module for now. So when it executes b.hi(), it can't find anything.



                                            Note that the b.hi() that got executed is during a.py -> module b -> module a, not in a.py directly.



                                            In your specific example, you can just run python -c 'import a' at top-level, so the first execution of a.py is registered as importing a module.






                                            share|improve this answer




























                                              0














                                              Circular imports cause problems, but Python has ways to mitigate it built-in.



                                              The problem is when you run python a.py, it runs a.py but not mark it imported as a module. So in turn a.py -> imports module b -> imports module a -> imports module b. The last import a no-op since b is currently being imported and Python guards against that. And b is an empty module for now. So when it executes b.hi(), it can't find anything.



                                              Note that the b.hi() that got executed is during a.py -> module b -> module a, not in a.py directly.



                                              In your specific example, you can just run python -c 'import a' at top-level, so the first execution of a.py is registered as importing a module.






                                              share|improve this answer


























                                                0












                                                0








                                                0







                                                Circular imports cause problems, but Python has ways to mitigate it built-in.



                                                The problem is when you run python a.py, it runs a.py but not mark it imported as a module. So in turn a.py -> imports module b -> imports module a -> imports module b. The last import a no-op since b is currently being imported and Python guards against that. And b is an empty module for now. So when it executes b.hi(), it can't find anything.



                                                Note that the b.hi() that got executed is during a.py -> module b -> module a, not in a.py directly.



                                                In your specific example, you can just run python -c 'import a' at top-level, so the first execution of a.py is registered as importing a module.






                                                share|improve this answer













                                                Circular imports cause problems, but Python has ways to mitigate it built-in.



                                                The problem is when you run python a.py, it runs a.py but not mark it imported as a module. So in turn a.py -> imports module b -> imports module a -> imports module b. The last import a no-op since b is currently being imported and Python guards against that. And b is an empty module for now. So when it executes b.hi(), it can't find anything.



                                                Note that the b.hi() that got executed is during a.py -> module b -> module a, not in a.py directly.



                                                In your specific example, you can just run python -c 'import a' at top-level, so the first execution of a.py is registered as importing a module.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Jan 9 at 15:12









                                                Hot.PxLHot.PxL

                                                8371925




                                                8371925






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1250103%2fattributeerror-module-object-has-no-attribute%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                                    Calculate evaluation metrics using cross_val_predict sklearn

                                                    Insert data from modal to MySQL (multiple modal on website)