15

I am trying to fire http posts via python. The requests module is installed via pip3 install requests - it also says now "Requirement satisfied", so it is installed.

I am using Python version 3.8.0.

The Code:

import requests as r
headers = {'Accept' : 'application/json', 'Content-Type' : 'application/json'}
url = 'http://localhost:8083/push/message'
jsons = {"test"}
r.post(url, json=jsons, headers=headers)

Error:

Traceback (most recent call last):
  File "http.py", line 1, in <module>
    import requests as r
  File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\__init__.py", line 43, in <module>
    import urllib3
  File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\__init__.py", line 7, in <module>
    from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
  File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 11, in <module>
    from .exceptions import (
  File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\exceptions.py", line 2, in <module>
    from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 618, in _load_backward_compatible
  File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\packages\six.py", line 199, in load_module
    mod = mod._resolve()
  File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\packages\six.py", line 113, in _resolve
    return _import_module(self.mod)
  File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\packages\six.py", line 82, in _import_module
    __import__(name)
  File "C:\scripts\http.py", line 5, in <module>
    r.post(url, json=jsons, headers=headers)
AttributeError: partially initialized module 'requests' has no attribute 'post' (most likely due to a circular import)

When I close the commandline and start a new one and then go into python I can import it:

C:\Windows\system32>python
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.post
<function post at 0x000001A4F7B9D310>
>>>

Same thing happens (only sometimes) when I execute the script - sometimes it actually works. (Btw, when it works it also posts to the server like it is supposed to)

Did anyone face a similar problem and might come up with a solution? Python 3 is the only Python-Version i have installed on this machine btw - yet facing a similar problem on other machines!

Rüdiger
  • 893
  • 5
  • 27
  • 56

6 Answers6

31

Thanks Unixia your answer help me in some way :)

But I have some improvement,I got the same error and it was because I had named my file to requests.py which produced a conflict with the original requests library

ASAD HAMEED
  • 2,296
  • 1
  • 20
  • 36
  • 1
    So it sounds like you need to rename the Python .py file. My file was unfortunately named token.py. – cona Jan 01 '21 at 22:04
  • @cona seems to be a very sensitive area. my file was called http.py and was causing problems. – webs Aug 13 '22 at 11:12
18

There may be other problems with your script that I have not checked but the error you're getting is because your file is named http.py. It seems like it is being used elsewhere and you're having circular import issues. Change the name of the file.

unixia
  • 4,102
  • 1
  • 19
  • 23
13

Circular dependency : It occurs when two or more modules depend on each other. This is due to the fact that each module is defined in terms of the other.

If you are getting circular import error similar to below request module error.

AttributeError: partially initialized module 'requests' has no attribute 'post' (most likely due to a circular import)

Please try to rename the file. The error for this error is usually due to conflict with the file name where you are trying to import the requests module.

I was also having same problem, where my file name was email.py and I was trying to import the requests module. So, it was having some conflict with email.parser. So, I changed the file name from email.py to email1.py and it worked.

For more info on circular dependency: https://stackabuse.com/python-circular-imports/

Tushar
  • 880
  • 9
  • 17
  • 1
    My file name was token.py. After rename, it is working well. It is a good idea to recommend the Requests team provide a list of forbidden names for files. – F.Tamy Aug 11 '21 at 07:18
1

This issue also happens if you have a file named token.py in your path. That is the case with Python 3.8.9 and Requests Version: 2.28.1 on Mac OS 12.4.

Walker Rowe
  • 953
  • 1
  • 12
  • 24
0

As per above discussion, We need to check the filename that we are executing and it should not match with any attributes the module has.

We can check this using below:

import requests \
print(dir(requests))

After we know that we are not using any of the attributes for the concerned module, We also need to make sure that any other file in the current directory should also not have any of the attribute name. I was facing the same error.

cottontail
  • 10,268
  • 18
  • 50
  • 51
-1

Same issue here, definitely a file name problem. Remember never to name your .py scripts with names of modules which already exist, especially if you're sure you're using them in your project.

E.g. In my case I had a requests.py importing the module requests, same can happen for http.py, etc.

unamednada
  • 57
  • 3