0

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!!

Asad Mehmood
  • 292
  • 2
  • 10
  • 20

1 Answers1

0

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.

WENJUN CHI
  • 507
  • 3
  • 11