How to do absolute import (a parent folder from the child folder)

The structure of my package is given below


The contains something

from folder_1.test_1 import params
if __name__ == "__main__":
    X, y = dataset
    model = LASSO(**params)

I want to import X, y to the file. The file contains

from package_test.main import X, y
def func():
return param

params = func()

I am running the program from the parent folder (from my desktop)

khali@akkas:~/Desktop$ python3 package_test/ --dataset=dataset1

But I am getting an error

ModuleNotFoundError: No module named 'package_test'

Could you tell me how can I solve this issue?

The directory of the current module main, which is package_test, would have to be included on the environment-variable PYTHONPATH for your import to work. This is not the best solution.

Usually, you current working directory and some library directories are in PYTHONPATH. Assuming your current working directory is the one containing package_test, you would need to change your import to:

from package_test.folder_1.test_1 import params

Instead, if you’re happy with relative imports, you could do:

from .folder_1.test_1 import params