Explore your training options in 10 minutes

X
Python Tutorials

What is __init__.py? : A guide

<
Comments (2)
  1. Avatar 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. Avatar 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 *

Many careers in tech pay over $100,000 per year. With help from Career Karma, you can find a training program that meets your needs and will set you up for a long-term, well-paid career in tech.

Career Karma

Find the right bootcamp for you

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

X
A person sitting on a dark green chair with a laptop on their knee