I am trying to use the requests module in a python script. I start by the import command and write the script however it is telling me that requests is not a module. Can anyone explain this to me? Is this not built in like "re" or do I have to import it in? I am new to python so any help would be great. Using Windows!
Thanks
Deirdre
No, requests is a 3rd party library. You can install it using pip.
pip install requests
And even if re is built-in, you have to import it in order to use it.
Related
I am trying to import urllib.request for python 2.7.10 on PyCharm 4.5.4 on Window 10 but getting the error "ImportError: No module named request".
The urllib.request modules have been deprecated ..
just use
import urllib
And for your function if you were earlier writing say
urllib.request.urlretrieve
Now you just write
urllib.urlretrieve
I have also faced the same error and Googled to solve it.
urlib.request is for Python 3.0.
You may use the code below:
import urllib
urllib.urlopen(url)
You'll get this error if you try running a python 3 file with python 2.
Try to use this in Python3
try:
x = urllib.request.urlopen('https://www.google.com/search?q=test')
print(x.read())
except Exception as e:
print(str(e))
It may happen sometimes, usually in the Linux environment. You have both 2.x and 3.x versions of python installed.
So, in that case, if you are using the command python "file.py"
then by default, it will python 2.x will run the file.
So, use the command python3 "file.py"
I was facing this issue. Maybe it can resolve someone's issue.
Try this:
#!/usr/bin/env python3
import urllib
response = urllib.urlopen("http://google.com")
html = response.read()
print(html)
The first line "#!/usr/bin/env python3" is important, as it let python know that this is Python 3 file, not 2.
Hope everything works.
Use > Path\easy_install.exe requests if you have a windows machine, where easy_install can be found in your Python*\Scripts folder, if it was installed. (Note Path\easy_install.exe is an example, mine is C:\Python32\Scripts\easy_install.exe)
If you don't have easy install and are running on a windows machine, you can get it here:
http://www.lfd.uci.edu/~gohlke/pythonlibs/#distribute
If you manually want to add a library to a windows machine, you can download the compressed library, uncompress it, and then place it into the Lib folder of your python path.
OR
You need to install pip first and then install django-request using pip
pip install django-request
also install,
python setup.py install
then import
from urllib.request import urlopen
Helpful Tips:to chech this
I am new to python on Visual Studio.
-----Here is my code-------------------------------
import urllib2
I need urllib2, but when I use PIP to get it, PIP times out.
What do I do? Do I setup a proxy? or can I manually import it somehow??
urllib2 is only available in Python 2, and is included as part of the standard library, which is why pip can't find it. For Python 3, you can use the various components of urllib, or for a much easier time of it, use the excellent requests module, which is available via pip.
I opened python code from github. I assumed it was python2.x and got the above error when I tried to run it. From the reading I've seen Python 3 has depreciated urllib itself and replaced it with a number of libraries including urllib.request.
It looks like the code was written in python 3 (a confirmation from someone who knows would be appreciated.)
At this point I don't want to move to Python 3 - I haven't researched what it would do to my existing code.
Thinking there should be a urllib module for Python 2, I searched Google (using "python2 urllib download") and did not find one. (It might have been hidden in the many answers since urllib includes downloading functionality.) I looked in my Python27/lib directory and didn't see it there.
Can I get a version of this module that runs on Python27? Where and how?
Try using urllib2:
https://docs.python.org/2/library/urllib2.html
This line should work to replace urlopen:
from urllib2 import urlopen
Tested in Python 2.7 on Macbook Pro
Try posting a link to the git in question.
You can program defensively, and do your import as:
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
and then in the code, just use:
data = urlopen(MIRRORS).read(AMOUNT2READ)
from urllib.request import urlopen, Request
Should solve everything
Instead of using urllib.request.urlopen() remove request for python 2.
urllib.urlopen() you do not have to request in python 2.x for what you are trying to do. Hope it works for you.
This was tested using python 2.7 I was receiving the same error message and this resolved it.
Change
from urllib.request import urlopen
to
from urllib import urlopen
I was able to solve this problem by changing like this. For Python2.7 in macOS10.14
You are right the urllib and urllib2 packages have been split into urllib.request , urllib.parse and urllib.error packages in Python 3.x. The latter packages do not exist in Python 2.x
From documentation -
The urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error.
From urllib2 documentation -
The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error.
So I am pretty sure the code you downloaded has been written for Python 3.x , since they are using a library that is only present in Python 3.x .
There is a urllib package in python, but it does not have the request subpackage. Also, lets assume you do lots of work and somehow make request subpackage available in Python 2.x .
There is a very very high probability that you will run into more issues, there is lots of incompatibility between Python 2.x and Python 3.x , in the end you would most probably end up rewriting atleast half the code from github (and most probably reading and understanding the complete code from there).
Even then there may be other bugs arising from the fact that some of the implementation details changed between Python 2.x to Python 3.x (As an example - list comprehension got its own namespace in Python 3.x)
You are better off trying to download and use Python 3 , than trying to make code written for Python 3.x compatible with Python 2.x
For now, it seems that I could get over that by adding a ? after the URL.
I am trying to authenticate jawbone api in python. In the code there is a line:
import requests
How can I add this. I have very little knowledge on python. Just manipulating the code. Can any one please help? The library is already present in python 3.3
This is only for the users using python33 and in windows platform,,,,
download Requests packages from any site.
Copy the folder Requests from the downloaded package and place it on C://python33/LIB/ folder....
now you are able to import Requests package to your program
I've unpacked BeautifulSoup into c:\python2.6\lib\site-packages, which is in sys.path, but when I enter import BeautifulSoup I get an import error saying no such module exists. Obviously I'm doing something stupid... what is it?
You might have more than one python version installed? Check the version you are running.
Also, I found using easy_install worked well for installing BeautifulSoup.