5

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.

1
  • 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. Jul 3, 2019 at 1:03

3 Answers 3

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.

1
  • 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, 2022 at 11:07
2

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

0
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.