0

Is it possible to call a function gui.settings.load() after from ... import gui? I am trying to set a folder like this.

main.py
gui/
    __init__.py
    settings.py

And in settings.py

def load():
    print('Hello') 

In main.py

import gui

gui.settings.load()

Do I have to mess with __init__.py?

For now, I have to name the file guiSettings.py instead, which I want to make it more simple.

Polv
  • 1,918
  • 1
  • 20
  • 31

2 Answers2

0

load according to your definition is not a function it is a class so yo can not call like this(class is meant to be instantiated you should review OOP cocepts). if you want to call function "load" you should define it as:

def load(parameters):
  #function body
  #optional return statement

and to interact with directories as modules e.i. using dot notation. the directory should include an __init__.py file. according to the docs:

Packages are a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name A.B designates a submodule named B in a package named A.

Also:

The __init__.py files are required to make Python treat the directories as containing packages;

so in your case you should define "__init__.py" (possibly empty) inside the outter gui directory and in the inner gui directory. then inside the settings module you could define your load function as above. you can read more about packages and modules here https://www.tutorialspoint.com/python/python_modules.htm

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
adnanmuttaleb
  • 3,388
  • 1
  • 29
  • 46
  • I find this one https://stackoverflow.com/questions/12229580/python-importing-a-sub-package-or-sub-module but can I from package import subpackage print sub package.module ? – Polv Jan 23 '18 at 09:26
  • I have read your link but I could not understand your question. @Polv – adnanmuttaleb Jan 23 '18 at 10:18
  • Succeeded, by putting import gui.settings inside `__init__.py` and it somehow doesn't work with an empty `__init__`. Also, I cannot seem to use the filename `gui/gui.py` – Polv Jan 23 '18 at 10:20
  • Congratulation, you may import all modules in init.py and then you only have to import the containing package to use the modules, but this is unrecommended. – adnanmuttaleb Jan 23 '18 at 10:26
  • So, `gui.settings.load()` should generally mean `gui.py` with class `settings` and function-in-class `load`? Not `settings.py`? – Polv Jan 23 '18 at 10:55
  • It means (if I have Interpreted your question correctly) call the function named load (e.i. execute it) that reside in the settings.py module that in turn reside in the gui package. – adnanmuttaleb Jan 23 '18 at 11:01
0

If I really want to call gui.settings.load(), I might structure my directory like this.

main.py
src/
    __init__.py
    gui/
        __init__.py
        settings.py

In main.py

from src import *

gui.settings.load()

In src/__init__.py

from . import gui

In src/gui/__init__.py

from . import settings

In src/gui/settings.py is still as same as above.

It seems that putting __all__ = ['gui'] and __all__ = ['settings'] in respective __init__.py doesn't work, unless I am misunderstanding how __all__ works.

Also, in this case __init__.py cannot be empty.

Also, it wouldn't work to just put import * without from in main.py.

Polv
  • 1,918
  • 1
  • 20
  • 31