Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write python __init__.py file for module

Tags:

python

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()
like image 651
JokerMartini Avatar asked Feb 19 '26 04:02

JokerMartini


1 Answers

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.

like image 88
dkamins Avatar answered Feb 20 '26 16:02

dkamins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!