Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python import requests results in Traceback - "Partially initialized module 'requests' has no attribute 'post'"

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!

like image 234
Rüdiger Avatar asked Nov 25 '19 15:11

Rüdiger


2 Answers

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

like image 98
ASAD HAMEED Avatar answered Nov 09 '22 06:11

ASAD HAMEED


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.

like image 18
unixia Avatar answered Nov 09 '22 07:11

unixia