Importerror no module named requests linux

Importing requests module does not work

I’ll give 3:1 odds that (a) you’re on a Mac, (b) you installed a third-party Python 2.7, and are using its pip to install modules, but are running Apple’s pre-installed Python 2.7, which of course can’t see the modules installed for a completely different copy of 2.7. If (a) is true and you’re not sure about (b), what do which python and which pip say? And, if which pip is /usr/local/bin/pip , is there also a /usr/local/bin/python ?

abarnert spot on. I have exactly the situation that you’re talking about. I think I probably used brew to install python and got into this situation.

4 Answers 4

The most common reason for this is that you have two versions of Python 2.x, and the pip that comes first in your PATH doesn’t go with the python that comes first in your PATH .

There are two ways that can happen.

First, you may have, e.g., /usr/local/bin before /usr/bin on your PATH , but your /usr/local copy of Python doesn’t have pip . So, when you run pip install requests , that’s /usr/bin/pip , which installs into /usr/lib/python2.7/site-packages . But when you run python , that’s /usr/local/bin/python , which looks in /usr/local/lib/python2.7/site-packages .

Second, even though your two Python 2.x’s are in different locations, they may want to install pip (and other scripts and executables) to the same place. In particular, /usr/bin is usually reserved for stuff that comes with the OS or its package manager, so if you use /usr/bin/python ez_setup.py or /usr/bin/easy_install pip or many other common ways to install pip , it may end up in /usr/local/bin . In which case it will overwrite any earlier /usr/local/bin/pip that went with your /usr/local/bin/python . At any rate, the result is basically the same: pip now means /usr/local/bin/pip , but it still goes with your /usr Python, not your /usr/local Python, and installs into /usr/lib/python2.7/site-packages , which /usr/local/bin/python can’t see.

If your two versions are, e.g., 2.7 and 3.4, there’s no problem; per PEP 394, either the 3.x versions of everything have to be run with python3 and pip3 and so on, or the 2.x versions have to be run with python2 and pip2 and so on.

If your two versions are, e.g., 2.6 and 2.7, there is a problem, but you can easily work around it, because you should always have pip2.6 and python2.6 vs. pip2.7 and python2.7 . You can confuse yourself with python and pip , but you don’t have to.

If your two versions are both 2.7, however, there’s no way to disambiguate (except by using complete absolute paths all the time, which no one wants to do).

So, why would anyone ever install two copies of Python 2 without knowing what they’re doing?

The most common reason is that they’re on a Mac, which comes with Python 2.7, but they read a blog post that told them to install another Python and didn’t explain how to know what they’re doing. Apple’s pre-installed Python is in /usr/bin but installs scripts and binaries to /usr/local/bin . The most popular alternative Python versions are the python.org installer and Homebrew, both of which install to /usr/local/bin by default. The fact that Mac users tend to be less Unix-savvy than Linux or FreeBSD users probably doesn’t help, but even without that, this is a perfect way to end up with thousands of people who have a pip and a python that doesn’t match, and no idea why.

Читайте также:  Linux mint install icons

There used to be good reasons for almost all Mac Python users to installing a second Python. Until OS X 10.6, Apple’s pre-installed Python versions tended to be badly out of date, and sometimes broken. If Apple’s only giving you 2.4, it makes sense to install 2.6. And doing so is no problem, because python2.4 and python2.6 are easy to disambiguate. But Apple has been installing a working 2.7 for years now. There are sometimes good reasons why you need a different one (you need a bug fix in 2.7.7 but Apple gave you 2.7.5, you need a 32-bit build, you need an extra-batteries version like Enthought, you need to build py2app bundles out of it, …), but these reasons do not apply to most people anymore.

In fact, many people on StackOverflow seem to have three versions of Python 2.7. I’m not sure why this is so common, but they’ll use Homebrew to install Python 2.7, and then use an installer from Python.org or Enthought, and now they’ve got three Python 2.7 versions all fighting over ownership of /usr/local/bin.

  • If you can use Python 3.x, install that and just use pip3 and python3 (and ipython3 and so on), and paths aren’t an issue anymore.
  • If you don’t need a second Python 2.7, get rid of the non-Apple one and just use Apple’s.
  • Otherwise, do not ever use Apple’s Python, do not install things for it, do not touch it; just leave it alone for Apple’s own tools. If you use Homebrew, its Python should be higher on the PATH (make sure you’ve got /usr/local/bin before /usr/bin ), and it should let you pip install foo without sudo , while Apple’s won’t, which makes it hard to accidentally screw up and install to the wrong one.

I’ve also seen at least one Windows user who had both C:\Python27 and D:\Python27 , both on the PATH , with the C: one first, but pip only installed for D: . This seems to be far less common than the Mac confusion (probably because Windows doesn’t come with Python, and there are no package managers, so the only way you’re going to get any Python is by running an installer). And the solution is even simpler here: Windows doesn’t need Python, so you can delete whichever one you want.

Finally, on non-Mac *nix systems, especially RHEL/CentOS Linux, you may have a Python 2.6 or 2.4 that’s needed by the OS plus a Python 2.7 that you installed because you needed it, or a 2.7 that’s needed by the OS and a 2.5 installed as a dependency for some «compatibility» package, or similar. Either way, you can easily accidentally install the pip for the one you don’t actually use (especially if you install it with the pip bootstrap instead of your package manager).

Читайте также:  Linux get directory size

The solution here is pretty simple: uninstall that pip , and use yum or apt or whatever to install the python-pip that goes with the Python 2.7 you want to use. And get in the habit of using python2.7 and pip2.7 —or just add aliases to your profile so that python or py or whatever you prefer runs python2.7 .

Источник

ImportError: No module named requests — Python

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.

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.

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.

Check out this article on PYTHONPATH. It might give you some insight into your issue. stereoplex.com/blog/understanding-imports-and-pythonpath

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.

3 Answers 3

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

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
Читайте также:  Linux see file permissions

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.

Источник

ImportError: No module named requests

In Python, if you try to import Requests without installing the module using pip, you will get ImportError: no module named requests error.

In this tutorial, let’s look at installing the Requests module correctly in different operating systems and solve no module named requests error.

ImportError: No module named requests

Requests are not a built-in module (it doesn’t come with the default python installation) in Python, you need to install it explicitly using the pip installer and then use it.

If you are getting an error installing pip checkout pip: command not found to resolve the issue.

Install Requests in OSX/Linux

The recommended way to install the requests module is using pip or pip3 for Python3 if you have pip installed already.

Using Python 2

$ sudo pip install requests

Using Python 3

$ sudo pip3 install requests

Alternatively, if you have easy_install in your system, you can install requests using the below command.

Using easy install

$ sudo easy_install -U requests
$ yum install python-requests

To install requests module on Debian/Ubuntu for Python2:

$ sudo apt-get install python-requests

And for Python3, the command is:

$ sudo apt-get install python3-requests

Install Requests in Windows

In the case of windows, you can use pip or pip3 based on the Python version you have to install the requests module.

If you have not added the pip to the environment variable path, you can run the below command in Python 3, which will install the requests module.

$ py -m pip install requests

Источник

‘No module named requests’ even if I installed requests with pip

pip is not pointing to the same environment as your python . You need to look at your PATH variable. see if you have pip installed for your python , e.g. /Library/Frameworks/Python.framework/Versions/2.7/bin/pip or ( pip2 , pip2.7 ).

6 Answers 6

In general, you should get into the habit of working in a virtualenv . I find the documentation here to be helpful.

If you install all of your dependencies within the virtual environment, you’ll be (mostly) sure that you are installing those deps. in the same environment that you’re running the jobs in.

For your case, on the command line go to the directory where your code lives and run

pip install virtualenv virtualenv my_project source my_project/bin/activate 

Now that the virtualenv is active you can

Only what is installed in the virtualenv will be available. This will keep your system clean. Each project should get its own virtualenv, meaning only the dependencies needed for each project will be available to them. This way you could, say, have version 1 of some dependency installed for one project and version 2 for another. They won’t come into conflict.

After you have installed all the dependencies, run

pip freeze > requirements.txt 

To get a list of all the dependencies for the project saved. Next time you need to install these, you simply run

pip install -r requirements.txt 

Once you are done working in the virtualenv, run

Источник

Оцените статью
Adblock
detector