2

I'm trying to import the requests module to get familiar with bs4, but the request module in the file I'm currently working in is grayed out so it isn't being recognized as a module. When I run the almost empty program, I get an error for an unrelated python file within my project.

Should I individually store each python file I make inside of a separate folder? Both of these files are inside of the same project folder.

import requests

response = get('https://www.newegg.ca/p/N82E16868105274')

print(response.raise_for_status())

Error:

  Traceback (most recent call last):
      File "C:\Users\Denze\MyPythonScripts\Webscraping learning\beautifulsoup tests.py", line 1, in <module>
        import requests
      File "C:\Users\Denze\MyPythonScripts\requests.py", line 3, in <module>
        res = requests.get('')
    AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import)
    
    Process finished with exit code 1

The other code in question that I think is causing my error:

import requests

res = requests.get('')

playFile = ('TestDownload.txt', 'wb')

for chunk in res.iter_content(100000):
    playFile.write(chunk)

playFile.close()
Błażej Michalik
  • 4,474
  • 40
  • 55
  • It's just the space I think kindly remove spaces before `import`. Also in your second program, It will throw an error cause you've only typed `import` & that clearly is a syntax error, if you're going to import something it should be `import module`. – Ice Bear Dec 19 '20 at 04:11
  • Yeah but why do I get errors for a file that I'm not currently working on? If I add the requests module in my second program, I get a circular import error. – Johnny Silverhand Dec 19 '20 at 04:17
  • The first code you have & the error it's having is a syntax error too cause the ` import reqeusts` code is not well aligned, so you can kindly and easily fix it and the second one is also a syntax `error` as stated on the `error` `message` cause you only typed `import`. – Ice Bear Dec 19 '20 at 04:21
  • You can kindly test again the edits :) – Ice Bear Dec 19 '20 at 04:26
  • I've ran this code 10x. There isn't a syntax error. Did you even look at the code? I don't understand why I'm getting an error from a totally unrelated program. How do I not run 2 programs at once. – Johnny Silverhand Dec 19 '20 at 04:28
  • Yeep I looked at it , before the edited one your `import requests` has `spaces` in it, it was like this ` import requests` that is why earlier you had those syntax errors. – Ice Bear Dec 19 '20 at 04:29
  • Kindly take a look at here after you edited the question with the `circular import error` [link](https://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python) – Ice Bear Dec 19 '20 at 04:31
  • Yeah but my problem is more than syntax errors now. I shouldn't be getting a syntax error from an unrelated program – Johnny Silverhand Dec 19 '20 at 04:33
  • Does this answer your question? ['Module object has no attribute 'get' Python error Requests?](https://stackoverflow.com/questions/12258816/module-object-has-no-attribute-get-python-error-requests) – yadayada Dec 19 '20 at 05:17

1 Answers1

0

You have a name collision. You're not importing the requests library, you're importing your script.

You wanted to do the following with your imports:

MyPythonScripts\beautifulsoup tests.py 
    → requests.get() (the library)

What you're doing instead is:

MyPythonScripts\beautifulsoup tests.py 
    → MyPythonScripts\requests.py 
    → MyPythonScripts\requests.py .get() (the same file again)

That's the "circular import" that is mentioned in the traceback. The module imports itself and tries to use an attribute that isn't there before it finishes "executing", so the interpreter thinks it's due to the unfinished initialization

Raname MyPythonScripts\requests.py to something else and it should work.

Błażej Michalik
  • 4,474
  • 40
  • 55