Kamel O.L.
Kamel O.L.

Reputation: 38

Importing module from subfolder

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

Upvotes: -1

Views: 53

Answers (1)

JialeDu
JialeDu

Reputation: 9747

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.

Upvotes: 1

Related Questions