0

I have this directory

---App
-----Main.py
-----SubFolder1
-------Function1.py
-----SubFolder2
-------Function2.py
-----SubFolder3
-------Utility.py

I have some functions in Utility, which I need to import to Main and to Function1

In main.py i use this pseudocode

from SubFolder3.Utility import SomeFunction

SomeFunction()

How I can import SomeFunction from Utility.py into Function1.py I don't have any idea how import work in python, despite of reading documentation and countless stackoverflow question treads.

P.S. I can't move Utility.py into SubFolder1 + I need to import SomeFuncion2 from Utility.py to Function2.py

I used literaly everything, that i could find in the internet. Some sys.path nonsense and various staff with init.py files. Maybe I do this wrong, idk

buran
  • 13,682
  • 10
  • 36
  • 61
  • What do you intend to be the top-level package(s)? `App`? The directories inside of `App`? And remember, Python imports _do not navigate directories_. Imports are only ever resolved by searching the Python path. If you want your packages to be importable, you _must_ put the directory containing them on the Python path. This is usually done by either installing your code as a distribution package or by ensuing that the containing directory is your CWD. – Brian61354270 Feb 23 '23 at 21:27
  • I need a working import inside the App folder, I have no intention of importing anything from App outside of that folder, so I assume the top-level package will be App. But I still haven't figured out how relative import works – Игорь Михатайкин Feb 23 '23 at 21:31
  • 1
    There are no relative imports in your question. But in any case, this question may be helpful: [Relative imports for the billionth time](https://stackoverflow.com/q/14132789/11082165) – Brian61354270 Feb 23 '23 at 21:34
  • Did you see [https://docs.python.org/3/reference/import.html#relativeimports](https://docs.python.org/3/reference/import.html#relativeimports)? `from ..SubFolder3.Utility import SomeFunction`? But I imagine you need some `__init__.py`'s in the directories. – wwii Feb 23 '23 at 21:34
  • I recive this error message while i try this: "attempted relative import with no known parent package" i have __ init __.py file in App, and in every SubFolder for sure – Игорь Михатайкин Feb 23 '23 at 21:40

2 Answers2

0

Why don't you set a current work directory to 'SubFolder3':

import  os
os.chdir('SubFolder3')

and then import the function

from Utility import SomeFunction

?

0

This works.. As Brian mentioned in the comments - no relative imports.

APP
__init__.py
main.py
---Sub1
     __init__.py
     Func1.py
---Sub2
     __init__.py
---Sub3
     __init__.py
     Utility.py

main.py

from Sub1 import Func1

Func1.py

from Sub3.Utility import somefunc

somefunc()

Utility.py

def somefunc():
    print(__name__,'somefunc')

PS C:\App> py -m main
Sub3.Utility somefunc
PS C:\App> 

As suggested in this answer to Relative imports in Python 3 (for the billionth time): If you edit each __init__.py to modify sys.path then Func1 will run standalone. I only chose that answer's solution as it was very easy but I suspect that solutions in other answers might be better or more robust depending on your circumstances.

All __init__.py's

import os, sys; sys.path.append(os.path.dirname(os.path.abspath(__file__)))

PS C:\App> py  -m main      
Sub3.Utility somefunc
PS C:\App> py  -m Sub1.Func1
Sub3.Utility somefunc
PS C:\App> 
wwii
  • 23,232
  • 7
  • 37
  • 77
  • Thanks, it worked! But there some strange bug - it works only when i import function1 to main, but when i try to run Function1 - it dont see utility It's not nessasry to get rid of it, but i would like why it's happening – Игорь Михатайкин Feb 23 '23 at 22:19
  • Ahh well I guess I need to finish reading through [Relative imports for the billionth time](https://stackoverflow.com/q/14132789/11082165). – wwii Feb 23 '23 at 22:32