0

I had already read other threads like this one an this other one, but I could not get an answer. I created a package with the following structure:

my_package (folder)
    __init__.py
    my_folder (folder)
        __init__.py
        my_file.py (contains f1 and f2 functions)
    my_other_folder (folder)
        __init__.py
        my_other_file.py (contains of1, of2, of3 functions)

at the beginning of my_other_file.py, I had put the import statements:

from ..my_file import f1
from ..my_file import f2

Inside of1 function, I cannnot call f1 directly, I need to call f1 as my_file.f1 (that's ok). On the other side, of2 function is a "shell" calling the function of of3 with a function parameter that, depending on the conditions, can be either my_file.f1 or my_file.f2. In other words, the code of function of2 is:

def of2:
    if ....:
        result = of3(my_file.f1)
    if ....:
        result = of3(my_file.f2)
    return result

the code above is is not working. However, if I use the below (without my_file), everything is ok:

def of2:
    if ....:
        result = of3(f1)
    if ....:
        result = of3(f2)
    return result

Why I need to call the same function once f1 and another time my_file.f1? Am I doing something wrong?

  • 1
    You say your imports look like `from ..function import f1`, but there is no module named `function` in your package. – Aran-Fey Jul 24 '18 at 10:03
  • thanks, it was my_file, edited – Giuseppe Bonavita Jul 24 '18 at 10:05
  • 3
    If you do `import my_file` you have to do `my_file.f1()` to call f1. If you do `from my_file import f1`, the code `f1()` will call `f1()` which is in `my_file`. If you never did `import my_file` I doubt you could do `my_file.f1()` in the `of1` function – T.Nel Jul 24 '18 at 10:06
  • 1
    Yes, I agree with the T.Nel, – JON Jul 24 '18 at 10:28
  • Thanks to both. After some tests, I believe my issue was due to the fact that in my real scenario the function had the same name of the file (maybe not a great idea). I changed the import statement to from ..my_folder.my_file import f1, this remove the ambiguity – Giuseppe Bonavita Jul 24 '18 at 11:15

0 Answers0