relative importing in python

Viewed 38

I have a file 'db_builder.py' inside a subfolder called 'db_init' of my 'cm_project' parent folder. the parent folder has 'cm_date.py' file. I'd like to import cm_project\cm_date.py from inside the cm_project\db_init\db_builder.py file as shown below: file structure image

1 Answers

I've also had issues with relative imports in Python and thus I've created an experimental, new import library: ultraimport

It gives you more control over your imports and lets you do file system based imports.

You could then write in your cm_project\db_init\db_builder.py:

import ultraimport
cm_date = ultraimport('__dir__/../cm_date.py')

This will always work, no matter how you run your code.

Related