0

I am trying to understand how to structure python projects but am encountering some problems.

using __init__.py for bundling imports

The way I understood it is that a packages __init__.py is run once I import said package (here customQtWidgets)

from scriptcommander.util import TOKENS
from scriptcommander.widgets import customQtWidgets

# defining
SCRIPTS = {
    "material set opaque" : {
        TOKENS.FUNCTION_LABEL: "material set opaque",
        TOKENS.FUNCTION: setMaterialOpaque,
        TOKENS.FUNCTION_ARGS: [
            comboBox.ComboboxConfig(itemList=OBJECT_SELECTION),
            checkBox.CheckboxConfig(label="set opaque", checked=True)
        ]
    }
}

customQtWidgets's __init__.py looks like this.

from scriptcommander.widgets.customQtWidgets import checkBox, comboBox, boxSlider

As this list of imports might grow over the course of the project, I want to define them centrally using init.py.

However, if I run my project I get:

    comboBox.ComboboxConfig(itemList=OBJECT_SELECTION),
NameError: name 'comboBox' is not defined

If I replace the import in init.py with a simple print, it get's run.

Vortek
  • 13
  • 5
  • You will still need to do `scriptcommander.widgets.customQtWidgets.comboBox`... – Tomerikoo Jun 02 '20 at 08:49
  • 2
    Please clarify – the error precisely tells you the problem: The name ``comboBox`` has not been defined. Python does not know what you mean by it – and we don't either. What do *you* think the name ``comboBox`` should mean? Why do you think it should mean this? – MisterMiyagi Jun 02 '20 at 08:49
  • You need to `import comboBox` in every file that you want to use it in. Importing it once in an `__init__.py` doesn't mean that name will now automatically be available everywhere else. Perhaps you want `from scriptcommander.widgets.customQtWidgets import *`? – deceze Jun 02 '20 at 08:58
  • @MisterMiyagi I assumed that the customQtWidgets packages init.py is run on import. So if I declare imports in the init.py there, it should be added to sys.path. The question is why the the module that imports the package customQtWidgets has no access to the imports defined by the packages init.py – Vortek Jun 02 '20 at 09:06
  • 1
    `sys.path` has nothing to do with this, leave it out of the picture. Python is extremely explicit about names, no names will ever be created implicitly. If you do `import foo`, then the name `foo` is available in your file now. Nothing more, nothing less. If you `import scriptcommander.widgets.customQtWidgets`, then the name `scriptcommander.widgets.customQtWidgets` is now available in your file. Again, the only somewhat exception here is `from ... import *`, which explicitly creates all names from that module in your file. – deceze Jun 02 '20 at 09:08
  • All ``__init__.py`` files of the ``scriptcommander.widgets.customQtWidgets`` path are run. This does not make any of their names available in your ``__init__.py`` file, though. Similarly, doing ``import os`` does not give you direct access to ``chdir``, for example – its full path from the imported ``os`` name, i.e. ``os.chdir``, is required. – MisterMiyagi Jun 02 '20 at 09:10
  • "The first time that a package or one of its modules is imported, Python will execute the __init__.py file in the root folder of the package if the file exists. All objects and functions defined in __init__.py are considered part of the package namespace." https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html – Vortek Jun 02 '20 at 09:17
  • Notice how the author imports and uses objects defined in `__init__.py` in that paragraph: `import packA` `packA.packA_func()`… – deceze Jun 02 '20 at 09:19

0 Answers0