4

If I have a module folder structure like this:

studio
    tools
        __init__.py
        camera
            __init__.py
            camera.py
        element
            __init__.py
            element.py

How do I write the __init__.py files to make it so I can write my import statements like the following? There is a class object in the camera module called CameraNode that I would like to call like shown in the sample below.

from studio import tools
    
tools.camera.fn()
tools.CameraNode('')
tools.element.fn()
Jacob Lee
  • 4,405
  • 2
  • 16
  • 37
JokerMartini
  • 5,674
  • 9
  • 83
  • 193
  • 2
    They merely have to exist. I tend to put a hash tag `#` just so I don't have empty files. – Tim Roberts Apr 15 '21 at 00:47
  • I'm not sure i understand what you are saying – JokerMartini Apr 15 '21 at 00:50
  • Does this answer your question? [How do I write good/correct package \_\_init\_\_.py files](https://stackoverflow.com/questions/1944569/how-do-i-write-good-correct-package-init-py-files) – theProcrastinator Apr 15 '21 at 01:05
  • 1
    Apart from the stackoverflow post, you can go through [this](https://www.reddit.com/r/Python/comments/1bbbwk/whats_your_opinion_on_what_to_include_in_init_py/) link that explains many use cases. – theProcrastinator Apr 15 '21 at 01:07

2 Answers2

8

If you have a studio/tools/__init__.py file like this:

from .camera import *
from .element import *

Then you can do a single import elsewhere like:

from studio import tools

and then reference everything as if it were directly in studio.tools.

Not sure if this is answering your question exactly, but hopefully it gets you going in the right direction.

dkamins
  • 21,450
  • 7
  • 55
  • 59
  • What do i need to write for the __init__.py file inside of the camera and element folders? – JokerMartini Apr 15 '21 at 01:20
  • @JokerMartini In Python 3, you don't even need them. "implicit namespace packages means that the requirement to provide an __init__.py file can be dropped completely" -- https://www.python.org/dev/peps/pep-0420/ – dkamins Apr 16 '21 at 03:37
4

An __init__.py file allows you to treat any directory as a Python package. It is not necessary to write anything in the file itself, just creating it in your directory is enough.

So camera, element, and tools can behave as packages.

However, if you want to access Python files in sub-directories as well, then you must import those in the __init__.py of the parent directory.

For example, if you want to do tools.camera then you must import camera in the __init__.py in the tools directory.

Otherwise, you will have to import camera separately to access all of the functions in it.

Melebius
  • 6,183
  • 4
  • 39
  • 52
pykam
  • 1,223
  • 6
  • 16
  • but how do i write the init files to make it so i can access the methods and classes directly in the submodules. For example camera.py contains a calls called CameraNode i want to access like tools.CameraNode – JokerMartini Apr 15 '21 at 12:01