7

I want to have a hierarchy that looks like this (and it has to 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 acces domain.py from menu.py. How can I do that ?

I did this in main:

    import ui_sub_directory.ui

This in ui:

    import domain_sub_directory.domain

But the UI module does not see the domain module.

What am I doing wrong ?

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

from x import y 

?

* Edit * for those who don't understand I want to import from:

folder1 /folder2 /folder3 /module1 

I want to import this:

folder1 /folder2 /module2
Kalec
  • 2,681
  • 9
  • 30
  • 49
  • `import ui_sub_directory.ui()` is already wrong syntax and logic. – Gandaro Feb 05 '12 at 22:12
  • Nearly a duplicate of this: http://stackoverflow.com/questions/8951255/import-script-from-a-parent-directory – Johan Lundberg Feb 05 '12 at 22:13
  • 1
    I might also ask you "what is the difference of apples and pears," that would be a similar question. They are completely different. The first one is wrong, and the second one is not. Please [read about imports](http://docs.python.org/tutorial/modules.html). – Gandaro Feb 05 '12 at 22:14
  • Well. what is the difference of apples and pears is (also?) a relevant question. ;) – Johan Lundberg Feb 05 '12 at 22:15
  • Helpful comments or none is what I would say, but ... No the link you gave has nothing to do with this. I need from lower level to import higher level module, I don't know how to type what I'm looking for, I am sorry, but I still need help. Also I read about imports but for the life of me I have no idea how to get from a folder1/folder2/folder3/module1 to folder1/folder2/module2 – Kalec Feb 05 '12 at 22:18
  • If you review that other question link, you will see that the OP does actually want to import a script in a higher level – jdi Feb 05 '12 at 22:22
  • I dont see a ui module in your example – jdi Feb 05 '12 at 22:27
  • use sys.path It's all in the link that @Gandaro posted – Joel Cornett Feb 05 '12 at 22:28
  • [This question](http://stackoverflow.com/q/9032551/1132524) had a similar project structure, try to take a look at it. – Rik Poggi Feb 05 '12 at 22:37

1 Answers1

8

You asked the difference in the import statements. Its partially a matter of the namespace for which the object will be imported under, and also a way to limit the exact amount of code that is imported.

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 could be more than you need, and for big libraries might be unneeded overhead. Though you can still access path via os.path

The second form is a way to selectively only import the path module. Additionally, instead of coming into your code under the os namespace, it now lives at the root level as just path.

While this link Import Script from a Parent Directory does tell you everything you need to know, here is some more specific info:

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

Then your various scripts can reference other module all relative to under main_folder, such as:

from ui_sub_direcotory import menu

from domain_sub_directory import domain

import main

These are all valid imports inside your package.

Community
  • 1
  • 1
jdi
  • 90,542
  • 19
  • 167
  • 203
  • If you won't answer then please at least post a link, I know not enough english to convey my thoughts so I can't search for it. I have found normal imports, but not "backwards". Thank you at least for the second part I guess. – Kalec Feb 05 '12 at 22:24
  • The links have already been posted in the comments. But I updated with some more help – jdi Feb 05 '12 at 22:43
  • Ok, sorry for having so many simple problems but I get this error: "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 13-15: end of string in escape sequence" How sould my path look like ? D:\Info\Eclipse Workspace\Test\Test_src\ui ? or not ? I don't get it – Kalec Feb 05 '12 at 22:48
  • Post more code. That looks like you are just doing a malformed string. – jdi Feb 05 '12 at 23:14
  • Try using forward slashes or doing a raw string like r'\path\path' – jdi Feb 06 '12 at 16:14