Importing installed package from script raises “AttributeError: module has no attribute” or...












35















I have a script named requests.py that imports the requests package. The script either can't access attributes from the package, or can't import them. Why isn't this working and how do I fix it?



The following code raises an AttributeError.



import requests

res = requests.get('http://www.google.ca')
print(res)




Traceback (most recent call last):
File "/Users/me/dev/rough/requests.py", line 1, in <module>
import requests
File "/Users/me/dev/rough/requests.py", line 3, in <module>
requests.get('http://www.google.ca')
AttributeError: module 'requests' has no attribute 'get'


The following code raises an ImportError.



from requests import get

res = get('http://www.google.ca')
print(res)




Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import get
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests import get
ImportError: cannot import name 'get'


Or code that imports from a module inside the requests package:



from requests.auth import AuthBase




Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests.auth import AuthBase
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests.auth import AuthBase
ImportError: No module named 'requests.auth'; 'requests' is not a package









share|improve this question




















  • 23





    Please note that this is intended as a canonical answer to a common question ... please don't close as duplicate unless you're sure the other question has a better, more complete answer.

    – Ffisegydd
    Mar 27 '16 at 18:01
















35















I have a script named requests.py that imports the requests package. The script either can't access attributes from the package, or can't import them. Why isn't this working and how do I fix it?



The following code raises an AttributeError.



import requests

res = requests.get('http://www.google.ca')
print(res)




Traceback (most recent call last):
File "/Users/me/dev/rough/requests.py", line 1, in <module>
import requests
File "/Users/me/dev/rough/requests.py", line 3, in <module>
requests.get('http://www.google.ca')
AttributeError: module 'requests' has no attribute 'get'


The following code raises an ImportError.



from requests import get

res = get('http://www.google.ca')
print(res)




Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import get
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests import get
ImportError: cannot import name 'get'


Or code that imports from a module inside the requests package:



from requests.auth import AuthBase




Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests.auth import AuthBase
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests.auth import AuthBase
ImportError: No module named 'requests.auth'; 'requests' is not a package









share|improve this question




















  • 23





    Please note that this is intended as a canonical answer to a common question ... please don't close as duplicate unless you're sure the other question has a better, more complete answer.

    – Ffisegydd
    Mar 27 '16 at 18:01














35












35








35


6






I have a script named requests.py that imports the requests package. The script either can't access attributes from the package, or can't import them. Why isn't this working and how do I fix it?



The following code raises an AttributeError.



import requests

res = requests.get('http://www.google.ca')
print(res)




Traceback (most recent call last):
File "/Users/me/dev/rough/requests.py", line 1, in <module>
import requests
File "/Users/me/dev/rough/requests.py", line 3, in <module>
requests.get('http://www.google.ca')
AttributeError: module 'requests' has no attribute 'get'


The following code raises an ImportError.



from requests import get

res = get('http://www.google.ca')
print(res)




Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import get
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests import get
ImportError: cannot import name 'get'


Or code that imports from a module inside the requests package:



from requests.auth import AuthBase




Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests.auth import AuthBase
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests.auth import AuthBase
ImportError: No module named 'requests.auth'; 'requests' is not a package









share|improve this question
















I have a script named requests.py that imports the requests package. The script either can't access attributes from the package, or can't import them. Why isn't this working and how do I fix it?



The following code raises an AttributeError.



import requests

res = requests.get('http://www.google.ca')
print(res)




Traceback (most recent call last):
File "/Users/me/dev/rough/requests.py", line 1, in <module>
import requests
File "/Users/me/dev/rough/requests.py", line 3, in <module>
requests.get('http://www.google.ca')
AttributeError: module 'requests' has no attribute 'get'


The following code raises an ImportError.



from requests import get

res = get('http://www.google.ca')
print(res)




Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import get
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests import get
ImportError: cannot import name 'get'


Or code that imports from a module inside the requests package:



from requests.auth import AuthBase




Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests.auth import AuthBase
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests.auth import AuthBase
ImportError: No module named 'requests.auth'; 'requests' is not a package






python exception python-module shadowing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 8 at 15:29









Martijn Pieters

712k13724842302




712k13724842302










asked Mar 27 '16 at 17:27









idjawidjaw

16.9k53557




16.9k53557








  • 23





    Please note that this is intended as a canonical answer to a common question ... please don't close as duplicate unless you're sure the other question has a better, more complete answer.

    – Ffisegydd
    Mar 27 '16 at 18:01














  • 23





    Please note that this is intended as a canonical answer to a common question ... please don't close as duplicate unless you're sure the other question has a better, more complete answer.

    – Ffisegydd
    Mar 27 '16 at 18:01








23




23





Please note that this is intended as a canonical answer to a common question ... please don't close as duplicate unless you're sure the other question has a better, more complete answer.

– Ffisegydd
Mar 27 '16 at 18:01





Please note that this is intended as a canonical answer to a common question ... please don't close as duplicate unless you're sure the other question has a better, more complete answer.

– Ffisegydd
Mar 27 '16 at 18:01












2 Answers
2






active

oldest

votes


















43














This happens because your local module named requests.py shadows the installed requests module you are trying to use. The current directory is prepended to sys.path, so the local name takes precedence over the installed name.



An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name of your script in question is matching the module you are trying to import:



Notice the name you used in your script:



File "/Users/me/dev/rough/requests.py", line 1, in <module>


The module you are trying to import: requests



Rename your module to something else to avoid the name collision.



Python may generate a requests.pyc file next to your requests.py file (in the __pycache__ directory in Python 3). Remove that as well after your rename, as the interpreter will still reference that file, re-producing the error. However, the pyc file in __pycache__ should not affect your code if the py file has been removed.



In the example, renaming the file to my_requests.py, removing requests.pyc, and running again successfully prints <Response [200]>.






share|improve this answer

































    5














    For the writer of the original question, and for those people searching on the “AttributeError: module has no attribute” string, then the common explanation as per the accepted answer, is that a user-created script has a name-clash with a library filename. Note, however, that the trouble might not be in the name of the script that generates the error (as it was in the above case), nor in any of the names of the library modules explicitly imported by that script. It might take a little detective work to figure out which file is causing the problem.



    As an example to illustrate the problem, imagine that you are creating a script that uses the "decimal" library for accurate floating-point calculations with decimal numbers, and you name your script "mydecimal.py" that contains the line "import decimal". There's no problem with any of that but you find that it raises this error:



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


    This would happen if you had previously written a script called "numbers.py" because the "decimal" library calls on the standard library "numbers" but finds your old script instead. Even if you had deleted that, it might not end the problem because python might have converted that into bytecode and stored it in a cache as "numbers.pyc", so you'd have to hunt that down as well.






    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%2f36250353%2fimporting-installed-package-from-script-raises-attributeerror-module-has-no-at%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      43














      This happens because your local module named requests.py shadows the installed requests module you are trying to use. The current directory is prepended to sys.path, so the local name takes precedence over the installed name.



      An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name of your script in question is matching the module you are trying to import:



      Notice the name you used in your script:



      File "/Users/me/dev/rough/requests.py", line 1, in <module>


      The module you are trying to import: requests



      Rename your module to something else to avoid the name collision.



      Python may generate a requests.pyc file next to your requests.py file (in the __pycache__ directory in Python 3). Remove that as well after your rename, as the interpreter will still reference that file, re-producing the error. However, the pyc file in __pycache__ should not affect your code if the py file has been removed.



      In the example, renaming the file to my_requests.py, removing requests.pyc, and running again successfully prints <Response [200]>.






      share|improve this answer






























        43














        This happens because your local module named requests.py shadows the installed requests module you are trying to use. The current directory is prepended to sys.path, so the local name takes precedence over the installed name.



        An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name of your script in question is matching the module you are trying to import:



        Notice the name you used in your script:



        File "/Users/me/dev/rough/requests.py", line 1, in <module>


        The module you are trying to import: requests



        Rename your module to something else to avoid the name collision.



        Python may generate a requests.pyc file next to your requests.py file (in the __pycache__ directory in Python 3). Remove that as well after your rename, as the interpreter will still reference that file, re-producing the error. However, the pyc file in __pycache__ should not affect your code if the py file has been removed.



        In the example, renaming the file to my_requests.py, removing requests.pyc, and running again successfully prints <Response [200]>.






        share|improve this answer




























          43












          43








          43







          This happens because your local module named requests.py shadows the installed requests module you are trying to use. The current directory is prepended to sys.path, so the local name takes precedence over the installed name.



          An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name of your script in question is matching the module you are trying to import:



          Notice the name you used in your script:



          File "/Users/me/dev/rough/requests.py", line 1, in <module>


          The module you are trying to import: requests



          Rename your module to something else to avoid the name collision.



          Python may generate a requests.pyc file next to your requests.py file (in the __pycache__ directory in Python 3). Remove that as well after your rename, as the interpreter will still reference that file, re-producing the error. However, the pyc file in __pycache__ should not affect your code if the py file has been removed.



          In the example, renaming the file to my_requests.py, removing requests.pyc, and running again successfully prints <Response [200]>.






          share|improve this answer















          This happens because your local module named requests.py shadows the installed requests module you are trying to use. The current directory is prepended to sys.path, so the local name takes precedence over the installed name.



          An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name of your script in question is matching the module you are trying to import:



          Notice the name you used in your script:



          File "/Users/me/dev/rough/requests.py", line 1, in <module>


          The module you are trying to import: requests



          Rename your module to something else to avoid the name collision.



          Python may generate a requests.pyc file next to your requests.py file (in the __pycache__ directory in Python 3). Remove that as well after your rename, as the interpreter will still reference that file, re-producing the error. However, the pyc file in __pycache__ should not affect your code if the py file has been removed.



          In the example, renaming the file to my_requests.py, removing requests.pyc, and running again successfully prints <Response [200]>.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 6 '17 at 13:23

























          answered Mar 27 '16 at 17:27









          idjawidjaw

          16.9k53557




          16.9k53557

























              5














              For the writer of the original question, and for those people searching on the “AttributeError: module has no attribute” string, then the common explanation as per the accepted answer, is that a user-created script has a name-clash with a library filename. Note, however, that the trouble might not be in the name of the script that generates the error (as it was in the above case), nor in any of the names of the library modules explicitly imported by that script. It might take a little detective work to figure out which file is causing the problem.



              As an example to illustrate the problem, imagine that you are creating a script that uses the "decimal" library for accurate floating-point calculations with decimal numbers, and you name your script "mydecimal.py" that contains the line "import decimal". There's no problem with any of that but you find that it raises this error:



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


              This would happen if you had previously written a script called "numbers.py" because the "decimal" library calls on the standard library "numbers" but finds your old script instead. Even if you had deleted that, it might not end the problem because python might have converted that into bytecode and stored it in a cache as "numbers.pyc", so you'd have to hunt that down as well.






              share|improve this answer






























                5














                For the writer of the original question, and for those people searching on the “AttributeError: module has no attribute” string, then the common explanation as per the accepted answer, is that a user-created script has a name-clash with a library filename. Note, however, that the trouble might not be in the name of the script that generates the error (as it was in the above case), nor in any of the names of the library modules explicitly imported by that script. It might take a little detective work to figure out which file is causing the problem.



                As an example to illustrate the problem, imagine that you are creating a script that uses the "decimal" library for accurate floating-point calculations with decimal numbers, and you name your script "mydecimal.py" that contains the line "import decimal". There's no problem with any of that but you find that it raises this error:



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


                This would happen if you had previously written a script called "numbers.py" because the "decimal" library calls on the standard library "numbers" but finds your old script instead. Even if you had deleted that, it might not end the problem because python might have converted that into bytecode and stored it in a cache as "numbers.pyc", so you'd have to hunt that down as well.






                share|improve this answer




























                  5












                  5








                  5







                  For the writer of the original question, and for those people searching on the “AttributeError: module has no attribute” string, then the common explanation as per the accepted answer, is that a user-created script has a name-clash with a library filename. Note, however, that the trouble might not be in the name of the script that generates the error (as it was in the above case), nor in any of the names of the library modules explicitly imported by that script. It might take a little detective work to figure out which file is causing the problem.



                  As an example to illustrate the problem, imagine that you are creating a script that uses the "decimal" library for accurate floating-point calculations with decimal numbers, and you name your script "mydecimal.py" that contains the line "import decimal". There's no problem with any of that but you find that it raises this error:



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


                  This would happen if you had previously written a script called "numbers.py" because the "decimal" library calls on the standard library "numbers" but finds your old script instead. Even if you had deleted that, it might not end the problem because python might have converted that into bytecode and stored it in a cache as "numbers.pyc", so you'd have to hunt that down as well.






                  share|improve this answer















                  For the writer of the original question, and for those people searching on the “AttributeError: module has no attribute” string, then the common explanation as per the accepted answer, is that a user-created script has a name-clash with a library filename. Note, however, that the trouble might not be in the name of the script that generates the error (as it was in the above case), nor in any of the names of the library modules explicitly imported by that script. It might take a little detective work to figure out which file is causing the problem.



                  As an example to illustrate the problem, imagine that you are creating a script that uses the "decimal" library for accurate floating-point calculations with decimal numbers, and you name your script "mydecimal.py" that contains the line "import decimal". There's no problem with any of that but you find that it raises this error:



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


                  This would happen if you had previously written a script called "numbers.py" because the "decimal" library calls on the standard library "numbers" but finds your old script instead. Even if you had deleted that, it might not end the problem because python might have converted that into bytecode and stored it in a cache as "numbers.pyc", so you'd have to hunt that down as well.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 3 '18 at 23:52

























                  answered Sep 11 '18 at 8:16









                  Dave RoveDave Rove

                  13515




                  13515






























                      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%2f36250353%2fimporting-installed-package-from-script-raises-attributeerror-module-has-no-at%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

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      Refactoring coordinates for Minecraft Pi buildings written in Python