Miumen
Miumen

Reputation: 85

Importing a python file from another directory but in same parent

I have this strucutre:

app/src/main.py

app/test/test.py

I want to import a function from main.py to test.py

I tried adding __init__.py to the parent lib and also the directories but it gave me an error.

the error is: ModuleNotFoundError: No module named 'app'

Upvotes: 1

Views: 80

Answers (1)

Will
Will

Reputation: 1845

Try this:

import sys; sys.path.insert(1, os.path.join(sys.path[0], '..'))
sys.path.append('../src/')
import src.main as mainfn
    
mainfn.function()

change function() with the name of your function

Upvotes: 2

Related Questions