简体   繁体   中英

How to resolve Python Module Not Found Error?

I am facing a problem in importing modules in python. I looked for a solution and found this . but this did not worked either.

My Directory is as follows

->MyScrapper
--->MyScrapper
----->db_connection.py
--->Video_Scrapper
-----> video_scrapper.py
--->Blogs_Scrapper
-----> blogs_scrapper.py

I want to import db_connection.py in video_scrapper.py as well as blogs_scrapper.py . I have tried to import it as from MyScrapper.db_connection import DBConnection . It throws a ModuleNotFoundError: No Module named 'MyScrapper' . I have also tried using from db_connection import DBConnection and import DBConnection but none of them worked.

Please Help!!

When importing a module in python, it will search the module in this order :

  • build-in module
  • script in the current directory
  • PATHPYTHON
  • installation-dependent default

So, you need to add the MyScrapper module to the search path. If you use a virtual environment, you can add your project root directory into the PYTHONPATH, by modify the PYTHONPATH in the Scripts\activate.bat file, like this:

set PYTHONPATH=%VIRTUAL_ENV%\src;%PYTHONPATH%

I use the virtual environment, and my project struct is:
|--Include
|--Lib
|--Scripts
|--src

My source files are in the src directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM