6

I am working with python flask's requests module. I have installed requests module using :

pip install requests

And verified that requests module exists when I run :

pip list

But when I run my python application , I receive import Error for requests module.

I noticed that pip is installing the module in C:\Users\xx\Documents\Projects\Python\Python3REST\lib\site-packages\ folder BUT the interpreter is looking for the module in C:\Users\xx\Documents\Projects\Python\Python3REST\lib\site-packages\flask\ folder.

I have tried running the command :

pip install --install-option="Path to install in" requests

But threw some other error.

The import error I am getting states :

ImportError: cannot import name 'requests' from 'flask'
(C:\Users\xx\Documents\Projects\Python\Python3REST\lib\site-packages\flask\__init__.py)

I am working in virtualenv in Windows 10.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
user1462617
  • 413
  • 1
  • 13
  • 23
  • What is your python path? It sounds like your python path doesn't match up with your site-packages and your interpreter can't find modules. – ladygremlin Jul 03 '19 at 01:03

3 Answers3

11

I recently had the same problem installing a self made package. I installed it with pip install <package> and checked it was actually installed with pip list but running the script with import <package> returned a ModuleNotFoundError: No module named <package>.

I solved the problem creating an empty file called __init__.py in the package directory.

Check https://pythontips.com/2013/07/28/what-is-init-py/ and https://docs.python.org/3/tutorial/modules.html#packages for better understanding.

esantix
  • 323
  • 5
  • 26
  • I read somewhere that from Python 3.6+ we wouldn't need the `__init__.py` files anymore. It is strange that that file is still necessary only at the custom module's root, but not within subfolders. – Sebastian Feb 16 '22 at 11:07
1

what if you add that folder to your path? using sys.path.extend?

Derek Eden
  • 4,403
  • 3
  • 18
  • 31
0

I solved it using python3 -m pip install <package name>. In the OP's case, it should be python3 -m pip install requests.

Note that I'm using python 3.10.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
zillBoy
  • 81
  • 2
  • 5