Import the module into another directory from the "parallel" subdirectory

I want to have a hierarchy that looks like this (and it should look like this)

main_folder\ main.py domain_sub_directory\ __init__.py domain.py ui_sub_direcotory\ __init__.py menu.py 

I need to activate ui.py frome main.py, but then it joins domain.py from menu.py. How can i do this?

I did this basically:

  import ui_sub_directory.ui 

This is in ui:

  import domain_sub_directory.domain 

But the UI module does not see the domain module.

What am I doing wrong?

Do I need to import the class I'm working with? and what is the difference between this and:

 from x import y 

?

* Change * for those who do not understand what I want to import from:

 folder1 /folder2 /folder3 /module1 

I want to import this:

 folder1 /folder2 /module2 
+6
source share
1 answer

You set the difference in import statements. This is partly a question of the namespace for which the object will be imported, as well as a way to limit the exact amount of imported code.

 import os from os import path 

Both os and os.path are modules. The first imports the entire os module and all its submodules. This may be more than you need, and for large libraries overhead may be unnecessary. Although you can still access the path through os.path

The second form is a way to selectively import a path module. Also, instead of entering your code under the os namespace, it is now at the root level as soon as path .

So far, this link Import Script from the parent directory tells you everything you need to know, here is one more specific information:

 # this will make your package available on your pythonpath sys.path.append("/path/to/main_folder") 

Then your various scripts can reference another module in relation to the main folder, for example:

 from ui_sub_direcotory import menu from domain_sub_directory import domain import main 

These are all valid imports within your package.

+7
source

Source: https://habr.com/ru/post/907721/


All Articles