Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import module in another directory from a "parallel" sub-directory

Tags:

python

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
like image 261
Kalec Avatar asked Feb 05 '12 22:02

Kalec


People also ask

How can I import modules if file is not in same directory?

We can use sys. path to add the path of the new different folder (the folder from where we want to import the modules) to the system path so that Python can also look for the module in that directory if it doesn't find the module in its current directory. As sys.

How do I import a module from the outside directory?

Method 1: Using sys. The sys. path variable of the module sys contains the list of all directories in which python will search for a module to import. We can directly call this method to see the directories it contains. So for importing mod.py in main.py we will append the path of mod.py in sys.

Can a module import another module?

Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).

Can import module from another folder Python?

The most Pythonic way to import a module from another folder is to place an empty file named __init__.py into that folder and use the relative path with the dot notation. For example, a module in the parent folder would be imported with from .. import module .


1 Answers

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.

like image 79
jdi Avatar answered Oct 14 '22 07:10

jdi