Import a list from parent folder

Question:

parent_folder
   | - folder_1
           |- a.py
   | - folder_2
           |- b.py
   | - C.py  

If I have declared a list in c.py , how can I access it in a.py & b.py.

No matter what I try, I keep running into ModuleNotFoundError.

C.py

def my_func():
    my_list = ["value_1", "value_2"]
    return my_list

Trial 1:

a.py

from parent_folder.C import my_func
list_a = my_func()

Trial 2:

a.py

from parent_folder import C
list_a = C.my_func()

Trial 3:

a.py

sys.path.append('../parent_folder')
from parent_folder.C import my_func
list_a = my_func()

How can I overcome this issue ?

Asked By: random_name

||

Answers:

Try with this:

a.py

import sys
sys.path.insert(1, os.path.join(sys.path[0], '..'))
from C import my_func

list_a = my_func()
Answered By: Will