1

I get an error message when I run the following line of code

import requests

Here is the error message

Traceback (most recent call last):
  File "C:\Python27\test", line 1, in <module>
    import requests
ImportError: No module named requests

Based on what I've seen from other posts, the common reason for the problem is individuals download the requests module but have not installed it. I downloaded the tarball, unzipped it and installed the setup.py file via the computer command line. There is a requests-2.4.0-py2.7.egg folder in the Python27/Lib/site-packages folder.

Pacific Stickler
  • 944
  • 7
  • 20
Jerry Carson
  • 97
  • 2
  • 7
  • 1
    One possible cause might be having more than one Python installation on your computer. Perhaps check whether the Python you are trying to import requests from is the same Python as the one specified in your environment variables... – Alex Riley Sep 04 '14 at 21:07
  • 1
    It sounds like you followed the right steps but is that folder in your PYTHONPATH now? – Matthew Green Sep 04 '14 at 21:10
  • I don't see any other Python installations. I don't understand what you mean by the folder being in the PYTHONPATH. There is a "requests" folder in the Python27 folder. There is also a "requests-2.4.0-py2.7.egg-info" folder in the Python27\Lib\site-packages folder. – Jerry Carson Sep 04 '14 at 21:30
  • 1
    Check out this article on PYTHONPATH. It might give you some insight into your issue. http://www.stereoplex.com/blog/understanding-imports-and-pythonpath – Matthew Green Sep 04 '14 at 21:37
  • 1
    The traceback tells me that you're creating folders inside the Python installation folder to store your new projects. This is probably a bad idea. – Karl Knechtel Sep 04 '14 at 21:40

3 Answers3

0

For Windows, install pip. You'll probably need other packages later on, so it will pay off to have a proper package manager. Here is the documentation: https://pip.pypa.io/en/latest/index.html

ventsyv
  • 2,781
  • 2
  • 23
  • 44
0

You might actually be missing certifi module.

Overview: From your error stack trace, it appears that you are using Windows platform and have the native windows Python installation. So I will stick to Windows instructions here. But since I have Cygwin based python installation, I will provide here cygwin-based steps to resolve your issue.

However, you can very easily use these steps on the Windows command prompt as well, by installing pip or easy_install, pre-built binary, or source code.

Windows-Cygwin-Pip way:

  1. Add the directory that hosts python executable to your environment's PATH variable. Instructions here
  2. Get pip to easily install new python packages. Best way is to download get-pip.py in cygwin’s home directory and run python get-pip.py in cygwin bash shell command prompt. Detailed and Alternative instructions here
  3. Run pip install requests in the cygwin bash shell. It will install requests and certifi packages, which results in results, requests-2.4.0.dist-info, certifi, and certifi-14.05.14-py2.7.egg-info in the /lib/site-packages folder.
  4. Run python and execute your original line of code import requests. It will run without errors.

Alternatives Ways to Installing a New Package: There are several other alternatives of downloading this requests package, or for that matter any new python package. These include:

  1. Getting easy_install in Cygwin and Running easy_install requests. Get easy_install in cygwin by installing setuptools package or by following instructions here.
  2. Downloading a pre-built binary available here. And running it as executable. It will automatically install the module under the latest python installation available in windows registry.
  3. Downloading source code for requests from Github into the home directory and Running python setup.py install

Python-Requests Installation Doc: There is a brief list of ways to install requests available on the original python-requests project website as well. See here.

Community
  • 1
  • 1
Pacific Stickler
  • 944
  • 7
  • 20
0

For a more productive environment and saving lots of headaches, follow these steps:

  1. Install virtualenv
  2. Install virtualenvwrapper
  3. Always manage your environments with virtualenvwrapper
  4. Always use pip to install dependencies inside your virtual environment
  5. Use 'pip freeze --local' to see what's installed or to produce a requirements.txt file (pip freeze --local > requirements.txt )

If you have no idea what I'm talking about, you should spend some time reading up on these things, and you'll discover one of the many things that makes python so nice to work with (well, ok other programming languages have similar tools)

Hexatonic
  • 1,643
  • 2
  • 17
  • 25