Why __init__.py File is Used in Python Projects | 2MinutesPy

preview_player
Показать описание
#__init__ #init #coding #script #python #pythonprogramming #pythonpackage #package #2minutespy

🖐Hey, want to know about __init__.py file in Python? In this Python tutorial, we dive into a fundamental concept: __init__.py file in Python.

Have you ever wondered about that mysterious "__init__.py" file lurking in your Python projects? In this video, we're peeling back the layers to reveal the secrets of the "__init__.py" file and why it plays a crucial role in your Python packages.

No jargon, no confusion – just a straightforward explanation of what "__init__.py" is, what it does, and why it's an essential component in your Python projects. Whether you're a beginner or a seasoned developer, understanding this file is key to mastering Python development. Let's dive in and demystify the "__init__.py" file together! 👩‍💻👨‍💻

More 2 Minutes Python Tutorial

@2MinutesPy
Рекомендации по теме
Комментарии
Автор

I love finding hidden gems like these on YouTube

catbeatzzz
Автор

for some reason this showed up in my feed, good stuff my guy. keep it up

leez
Автор

Important detail that mixes people up a lot (and results in a lot of repeat questions on StackOverflow) --- If the scripts in your package rely on elements of other files in the same package via imports, they MUST use Relative imports.
However, if you also have a main script or test script within your package that you want to run directly (as opposed to having an external main.py import your package), then that script within the package must use Absolute imports. Python determines whether or not it should be using relative or absolute imports based on whether or not the script it is processing is in a package, and the top-level script (the one invoked with 'python my_script.py') is never considered to be in a package while it is interpreted.
If you insist on having scripts in your package that you want to run directly, put the intra-package imports in a try/except block, where you first try the relative import, catch an ImportError, and then try absolute imports.

nigh_anxiety
Автор

I just found your channel from the Youtube recommendations. The 2 minute videos are a great idea!

dad
Автор

I do a similar import without __init__.py file and it works fine. What is an example of import which wouldn't work without that empty file?

patryk
Автор

So it's basically Python's way of declaring namespaces with optional super constructor functionality. That's what I understood.

Great vid btw! :)

有賀_竜樹
Автор

Somehow saw your vid by youtube recommended algorithm! I think I like the concept of explaining something really important and widely used in a short amount of time like 2-minutes! It was really good, maybe if more complex topics need to be explained then I think you dont need to keep a 2-min constraint. It can be extended up to 10min but not more than 10min. Maybe just keep it compact like this. Anyways great work !

quangnhatle
Автор

__init__.py currently can be used to expose class or function. Taking the example in the video, if you want to expose the function "say_hello" in "greeting.py" to "my_package" level (i.e. in other arbitrary script, you want to "import say_hello from my_package"), you can write the line: "from .greeting import say_hello" in "__init__.py" to achieve this goal.

lienjerry
Автор

explanation is clear concise to the point.. no redundancy. love it.

wamimuswamimus
Автор

Great video, keep making new ones. The way you explain things in 2 minutes is amazing ! Great work !

Fizzulko
Автор

Also can help in abstraction and encapsulation(__all__) too

arvindh
Автор

High clarity, Excellent Presentation and ultimate communication very interesting video. Thanks and Regards.

vasutke
Автор

I can't thank you enough for this. This is the best explanation ever. Thank you very much

dheeraj
Автор

Absolutely hidden information, I couldn’t found out this in udemy courses. Thank you🎉

MissingTricks
Автор

This needs so much more engagement, also i might comment another just for engagement purposes

epokal
Автор

Omg what an amazing video to come across while in my first weeks of using python. 👍👍👍

moralfuxery
Автор

Greatly explained in the most simplistic way possible. Thanks

Kothwalashivakumar
Автор

Great startups! Keep on making more videos.

markmilan
Автор

Almost a day of searching why I'm getting "No module error" and I'm just missing init file for the imports folder, Udemy and coursera is missing these in their python selenium courses. Great video brother, you save my ass.

baderhelsing
Автор

Thank you so much! That was so easy to understand ✨

-exe