Explore your training options and get exclusive tuition discounts Get Started

X

Back

Global navigation

What is __init__.py? : A guide

Comments (2)
  1. Geert Vancompernolle says:

    Hi,

    Just read your article about __init__.py since it’s not clear to me what the purpose is of that “mysterious” file.

    I have the following python project tree (not all given, but it makes the idea clear):

    main.py
    canbus/canbus.py
    canparser/canparser.py
    heartbeat/hearbeat.py
    .
    .
    .

    As you can see, not one of them currently has the __init__.py.

    According to your article, it should NOT be possible to do an “from canbus import canbus” without the file __init__.py being part of the canbus directory, in main.py.

    Well, it looks like I am able to do that. Whether I put a __init__.py file or not in the directory canbus, it works equally fine.

    I can also write “from canbus.canbus import CANbus” in main.py (which is a class within canbus.py) without having the __init__.py present.

    So, I’m even more confused then before about the purpose of this __init__.py file.

    Hope you can explain why I can do this all without having not a single __init__.py in my project…

  2. Geert Vancompernolle says:

    OK… I just now saw that with Python3 there’s no need anymore for __init__.py files. That explains why it’s working for me, since I’m using Python3…

Leave a Reply

Your email address will not be published. Required fields are marked *

Start your Tech training
Find, compare, and enroll in the right training program. Save on Tuition.
View my matches