0

I have the following structure

my_scripts
    group_A
        group_A1
            main.py
            dbFunctions.py
        group_A2
            main.py
            dbFunctions.py
    group_B
        scripting_Im_running_things_from.py
        dbFunctions.py

I want to load one function from each of the main.pys, and am trying to do it with sys.path.append. But because the folder I am running my main script from (group_B) has dbFunctions.py as well, none of the other two models' functions (ie. group_A1\main.py and group_A2\main.py) can utilise functions from their respective dbFunctions.py modules so I am getting an import error:

ImportError: cannot import name 'my_function_in_groupA1_dbFunctions' from 'dbFunctions' (C:\my_scripts\group_B\dbFunctions.py)

(I also tried renaming group_B\dbFunctions.py to group_B\_dbFunctions.py and adding the group_A1 and group_A2 to sys.path does allow me to import functions from whichever of group_A1's and group_A2's main.py I choose to import from first, but then for the second one it is impossible to make it look at the second folder; because it has loaded one function from a given main.py, seemingly no other main.py > can be considered.)

Is it preferebale to add init.pys in all folders or can this be done by using importlib ?

Apologies for not creating a re-producible example, I cannot do it I believe for this question.

Tony
  • 781
  • 6
  • 22
  • It is not only preferable, but necessary, to add `__init__.py` files in order to use the source files as packages. – mkrieger1 Jan 22 '20 at 21:14
  • So this cannot be resolved by making explicit imports using `importlib`? – Tony Jan 22 '20 at 21:16
  • Does this answer your question? [How to write a Python module/package?](https://stackoverflow.com/questions/15746675/how-to-write-a-python-module-package) – mkrieger1 Jan 22 '20 at 21:17
  • It depends; this post explains how to convert a script into a package. If this is the only way to resolve what I am asking in this question then it might. If however there is a straightforward way to explicitly ask it to 'first import a function specifically from group_A1\main.py, then import a function specifically from group_A2\main.py' then it doesn't. But I am not sure what is the case, the former or the latter? – Tony Jan 22 '20 at 21:25

1 Answers1

0

Use relative imports. For example:

# ../my_scripts/group_A/group_A1/main.py

# Import foo from ../my_scripts/group_A/group_A1/dbFunctions.py
from .dbFunctions import foo

# Import bar from ../my_scripts/group_A/group_A2/dbFunctions.py
from ..group_A2.dbFunctions import bar

# Import baz from ../my_scripts/group_B/dbFunctions.py
from ...dbFunctions import baz
Jordan Brière
  • 1,045
  • 6
  • 8
  • I can't even make `from .dbFunctions import foo` run (for the module in the same folder) as I am getting `ImportError: attempted relative import with no known parent package`. Is adding the dot the only change that needs to be done in order for this to work? – Tony Jan 22 '20 at 21:11
  • Add empty `__init__.py` files to your directories, so they becomes packages. – Jordan Brière Jan 22 '20 at 21:12