How to import multiple class objects from seperate folders?

651 Views Asked by At

I built a python 2.7 application with the below directory structure for my relevant files. How do cal methods located in different folder locations?

Data-Wrangling-OpenStreetMap-data
 |
 +---- process_data
 |       |
 |       +---- __init__.py
 |       |
 |       +---- data_cleaner.py
 |
 +---- main_code.py
 |
 +---- audit _data
 |       |
 |       +---- __init__.py
 |       |
 |       +---- audit_file.py

I have succeeded in doing it correctly for one class referenced from main_code.py via the use of:

from process_data.data_cleaner import DataCleaner

However, if attempt a similar pattern for another class located in seperate folder referenced by main_code.py for via the use of the import statement

from audit_data.audit_file import AuditFile

I get the error:

ImportError: No module named audit_data.audit_file

Any ideas as to what I may be overlooking and/or guidance on what further details I need to post to help find the solution to my problem?

2

There are 2 best solutions below

2
On BEST ANSWER
from process_data.data_cleaner import data_cleaner

as data_cleaner is the file name data_cleaner.py and the second one data_cleaner is a class defined in it.

0
On

The cause of my problem was a silly one; The folder containing the class I was calling was named audit _file whilst the folder I was calling within my code was audit_file

What didnt work from audit_data.audit_file import AuditFile

What worked from audit _data.audit_file import AuditFile

For those reading this watch out for unintended spaces in your folder names