2

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.

I appreciate any help I can get.

Thank you

ladygremlin
  • 382
  • 1
  • 13
user1462617
  • 355
  • 1
  • 8
  • 19
  • 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

2 Answers2

9

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.

  • 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
  • 3,938
  • 1
  • 13
  • 29