Home > Software design >  Importing module from subfolder
Importing module from subfolder

Time:09-01

This is the structure of my project

final
├── common
    ├── __init__.py
    ├── batch_data_processing.py
    ├── processing_utility.py
    ├── post_processing.py
├── Processing_raw_data
    ├── batch_process_raw_data.py

so i want to import from common.batch_data_processing in batch_process_raw_data.py

but when I try it I get ModuleNotFoundError: No module named 'common'

is there a way to import this module without the need to install it ?

Note : this is intended to be used by "non python users"

here is pictures to better discribe the problem. enter image description here enter image description here

CodePudding user response:

Add the following code above your import code to indicate the path:

# The following is a relative path, 
# it can also be replaced with the absolute path 
# of the directory where common is located.
# sys.path.append("C:\\Users\\Admin\\Desktop\\Final")

import sys
sys.path.append("./")

enter image description here

When all your scripts are in the same folder, importing modules is almost impossible to go wrong. If you need to import scripts from external folders, you can specify the path using the above method.

  • Related