0
I’m trying to use the module requests with python but I’m having the following errors:
File "/home/santana/string.py", line 35, in <module>
except requests.exceptions.RequestException as e:
AttributeError: 'module' object has no attribute 'exceptions'
I already installed the requests module in python3.6 and python2.7 using
pip3 install requests
pip2 install requests
and also
sudo pip3 install requests --upgrade
but in all alternatives I have received the same error.
I tried to force import using "from requests import exception"
but the error changes to:
File "/home/santana/string.py", line 34, in <module>
AttributeError: 'module' object has no attribute 'post'
Which makes me wonder if the installation of the module was done correctly. Can anyone tell me if the problem is in the installation of the module or in the code?
from __future__ import print_function
import sys
import requests
import random
from requests import exceptions
string_api_url = "http://string-db.org/api"
output_format = "tsv-no-header"
method = "get_string_ids"
## contruct params dictionary
### URL FORMAT ###
#https://string-db.org/api/[output-format]/get_string_ids?identifiers=[your_identifiers]&[optional_parameters]
params = {
"identifiers" : "\r".join(["p53", "BRCA1", "cdk2", "Q99835"]), # your protein list
"species" : 9606, # species NCBI identifier
"limit" : 1, # only one (best) identifier per input protein
"echo_query" : 1, # see your input identifiers in the output
"caller_identity" : "www.awesome_app.org" # your app name
}
## contruct method URL
request_url = string_api_url + "/" + output_format + "/" + method
## Call STRING
try:
response = requests.post(request_url, params=params)
except requests.exceptions.RequestException as e:
print(e)
sys.exit()
## Read and parse the results
c = 0
for line in response.text.strip().split("\n"):
l = line.split("\t")
print(l)
input_identifier, string_identifier = l[0], l[2]
print("Input:", input_identifier, "STRING:", string_identifier, sep="\t")
There must be an error in your Nvironment, for sure. On my computer (Python 3.7.3, x64 and requests 2.21.0) it worked perfectly.
– Breno
If made
from requests import exceptions
, why not just useexceptions.RequestException
?– Woss
And if you’re using Python 3, there’s no reason to
from __future__ import print_function
. By chance you copied this code from somewhere?– Woss
Code running in Python 3 and running in Python 2, which makes your question non-reproducible. Seek to edit it and add more details of the problem, preferably by creating a [mcve].
– Woss
Anderson Carlos, first of all, I apologize for the non-replicable question. I talked about the versions because I tried to use both, but the errors were the same. About exceptions.Requestexception, I’m using it anyway and the error didn’t occur anymore, but now I’m getting a new error just like I commented. About the from Future import print_function I used because as I said I tried it in different versions of python, not only the 3 version. This code I obtained on the string-db platform, follows the link https://string-db.org/cgi/help.pl?sessionId=lb0oZGkX9I3j&subpage=api
– Matheus Santanna