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.
BS, you will be completely fine if you just use absolute imports. Relative imports only save you a tiny bit of time when moving modules to different packages. Otherwise, it's almost entirely up to personal preference.
not really about the "MUST use relative imports statement within the package". According to PEP 8, "Absolute imports are recommended, as they are usually more readable and tend to be better behaved". Unless absolute import is very verbose (which should never occur in the first place as you should never yank something really deep out of another sub-package. Instead, you should have an API init script for the package and only import the ones from its init script). You almost should always use absolute import, because it explicitly states where the object comes from. This also makes it easier to move sub-packages around.
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 !
Yeah, from Python 3.3, the namespace package won't need __init__.py file to import modules. But if you want to create a regular package, you'd include __init__.py file in your directory to make a better and maintainable package. The empty __init__.py file can be used to mark a directory as a package, which allows importing relative modules within the same package.
__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.
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.
Hi 😅 I'm a bit confused about one thing, sometimes I do "from math import sin, pi", so that I could use it as "sin(pi)", instead of "math.sin(pi)". Your example seems a bit different to me, as it doesn't import the function "say_hello()", but the whole python greetings.py file. Is there a way to only import the "say_hello()" function without needing to suffix it with "greetings.say_hello()"? (Sorry I'm a newb 😂)
Yes you can do it. You just need to import only the say_hello() function from the greetings module. Like that👇 from my_package.greetings import say_hello Now you can directly call the say_hello() function. I hope that'll help you and sorry for late comment.
Just BTW. . Use namespace . . Pep 420 . . __init__.Py is not used after python 3.3 for package imports over namespace. . It has some functionality, but a pretty old change.
I did not become more clear at all. Sometimes importing works and sometimes it does not. It would be more useful to explain how python interpreter searches for the packages and makes them available for importing. I have a code base that works in 3 ways: as a FastApi app, as pytest testing the web app and also as part of a databricks job. I was able to make first two cases to work more or kess clean (although I had to follow a particular folder structure and if I change it everything breaks, so I cannot say I understand how it works). And for databricks i ended up with adding some ugly "sys.path.add" or like that to make it work. 4 years of working with python and those init files are still a mystery for me.
Well, how this video is useful that's just basically says put an __init__.py!? You can literally get the info on the internet in less than 2 mins. I need to know what I can do by adding lines of codes in __init__
Emmm... That "init" shit never works for me... How tf any python file outside "my_package" can know about that package? It's not even in sys.path... I'm really confused. EVERYTHING I try to make sense of this init file just never works. For me its just a garbage. Personally I just create and activate venv in ".venv" folder and inside this folder create file "any_name.pth" with "../" content. So I have root directory with ".venv" subdirectory. And when I activate my venv, my root folder is inside my sys.path. so, in project, I can use absolute imports as I want. And if I want to use it as a package - I just create pyproject.toml file, configure it and use pip install with path\to\ package. And that's much better, because you can do the same thing even using git repos... I don't know wtf is __init__.py... Just a garbage
Why should we add an additional and ridiculous file called __init__.py in every folder in the project to recognize a folder as a package ?!!!! It's really ridiculous! STUPID PYTHON.
In general I agree! Not everyone is cut out be a narrator. But the information density of this content is so low that it feels straight out of chat gpt, and the text-to-speech emphasizes that appearance.