I cannot import any Python module

Asked

Viewed 7,060 times

0

I want to import the module requests from Python to a program I’m doing, but it doesn’t stop giving the following error, so I run the program:

ModuleNotFoundError: No module named 'requests'

I have the latest version of Python installed (reinstalled direct from the official website yesterday) and already entered the command sudo pip install requests in the Terminal of my Macos to install the module. The module is already installed.

I don’t know if maybe I need to put the module folder in the same folder as my program... I don’t know where to find the module folder anyway.

I’m new, so I’m sorry for the ignorance.

Follow the code of my program.

import requests

#meu programa 

And my program (the archive .py) is located directly on the Desktop (Desktop) of my Mac.

Where is my mistake?

Thank you!

  • It is likely that you have more than one python environment and Pip is installing in a different one. To get rid of problems with managing envs, consider installing anacoda. For installation on mac, see this answer., to supplement the introduction to anaconda, see this one.

  • I agree with Sidon and if the "anacoda" or "anaconda" doesn’t work you can try the virtualenv.

  • Ever tried to spin pip install request without sudo and see if it works?

4 answers

0

Santos, I went through a similar difficulty a while ago, because I have several different versions of Python installed on my Mac.

I solved the problem in a specific script as follows:

Observação: verifique se possui o módulo instalado com o comando: $ pip list (python2) $ pip3 list (python3+)

In case he doesn’t show up at pip list, possibly not installed, so give the command: pip install <nomeModulo> and make sure that the module is installed on your machine.

1. I gave a python3 to check where the main folder of my python3 is located

$ which python3

He returned me something like:

/Library/Frameworks/Python.framework/Versions/3.7/bin/python3

2. So I declared the interpreter path in my Python script

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3

PS.: Above the # -*- coding: utf-8 -*-

And my problem was solved.

0

I will give my contribution, but with a limitation, but still hope to help ! As I use Macbook and need to install the programs in python version 3.8 which is compatible with root-CERN, I use the macports. First, test to know if you have any version::

$ sudo port installed |  grep -i pandas

If you don’t have :

$ sudo port install py38-pandas

In place of pandas you can put any library of interest like tensorflow, Keras, h5py and etc.

0

This is Santos speaking! Man, this looks like Pip might be installing in its old python version. Do the following. Try to execute the command by passing the version down, like this:

pip3.6 install requests

I assume that it is version 3.6 that you have there. If not, change the number to the corresponding version.

0

In the Linux, Mac OS X and other POSIX systems, use the Python version commands in combination with the -m option to perform the appropriate Pip copy

python2   -m pip install NOME DO PACOTE  # Python 2 padrão
python2.7 -m pip install NOME DO PACOTE  # especificamente Python 2.7
python3   -m pip install NOME DO PACOTE  # Python 3 padrão
python3.8 -m pip install NOME DO PACOTE  # especificamente Python 3.8
python3.9 -m pip install NOME DO PACOTE  # especificamente Python 3.9

Browser other questions tagged

You are not signed in. Login or sign up in order to post.