What does '__init__.py' do in Python?
Skills:
Python for Data70%
Key Takeaways
The video explains the purpose and usage of the __init__.py file in Python, covering modules, packages, relative imports, and import errors.
Full Transcript
if you've read a fair amount of python code then you've probably seen this init.py file pop up quite a few times it's especially common in larger python projects well in today's video I'm going to break down exactly what this file is when you should use it and exactly how it works so stay tuned so first things first I've actually just removed that file and I'm going to explain to you something that's a prerequisite which is known as python modules now python module is really just some python code or python file that contains code that you'll import from another module now technically any python file is a module but we refer to something as a module usually when we're importing it from another script so for example I have my string util module I have my math util module and we can imagine that this contains some code that we might want to use in a larger program so maybe what I have is something like a main.py file and from main.py I want to import some of this code just to make my uh file a little bit cleaner right so I can say something like from string UT import capitalize and from math util import add and now I have access to these functions from my utilities and I can use them directly in my file if I run the code you see that I don't get any errors and this is perfectly fine so this is quite common and it's a great way to organize your scripts to make things easier to read and understand where you don't have all of your logic in a single file and you can imagine as the project is larger and larger you're going to have more and more modules and what's going to come is the need for a package now a package is simply a directory that contains python modules so I can do something like utils here and then I'm going to take my two utilities and put them inside of this newly created package now again a package is just a directory that contains Python scripts or python modules and previously in older versions of python you needed to create this uncore anitore dop file in order to mark that this directory was a package but in newer versions of python like all of the modern ones you're using today that's no longer necessary and any directory that contains Python scripts is able to be considered a package okay so all of that's great but now if we go to main.py notice that our Imports are no longer working it's saying we can't resolve the string util and the math util and that's because they're inside of this package so now if I wanted to import these various functions I would need to change this to reference the package so I would have to say from utils string UIL import capitalize from utils mathu import ad and all of that's fine and if I run the code now you can see that all of this works and by the way if you're wondering why these pie cache things keep popping up anytime you import a module from a script in Python it will automatically be cached inside of pyh so you don't need to keep recompiling all of these module scripts especially because a lot of times they're not changing don't worry too much about that but just a fun fact in case you were interested in that okay so now that we've got all of this it's time to finally talk about a nitpy so I'm going to go ahead and create this file uncore nitor dopy which is used inside of a python package now even though it's not necessary it's still often used because of its Rule now what the init.py file does is simply initialize the package by running some code whenever it's imported super quick pause here I want to share with you something that's super valuable and completely free and that is my coding newsletter in the coding newsletter I send you coding challenges obviously the solution to those challenges cool project ideas stories insights for me as a software engineer with over 10 years of experience and as a lead magnet for this I'll even send you an entire 14-page PDF breaking down all of the ways that you can make money from coding which you can see right here that's completely free you can sign up from the link in the description or Tech with.net newsletter and quickly if any of you guys are interested in some private mentorship from me I do have a program it's called Dev launch I've just opened a few more slots if you want toess you can apply and see if you're a good fit from the link in the description just ask you to fill out a short form just to make sure that I can actually help you and there's a video here that shares some great insights anyways thanks for listening now let's go back to the video so to see the simplest example of this I'm just going to make a print and I'm going to say imported utils like that and I'm going to go back to my main.py file and show you what happens in the terminal when I run this notice when I do that that it says imported utils and notice that it only says that one one time so again what the init.py file does is it will run some code to initialize the package the first time this package is imported so here it's imported twice right because we're importing the string util and the math util both from the utils package so the first time that we import this package and we initialize it we run this init.py file that's the use case so if you were importing a larger package so imagine importing something like tensorflow you would probably have some code that you'd want to run before that import is successful and that's why sometimes when you import code it takes a second before it actually loads in your script well there's other reasons as well because usually it's running some kind of setup operation so you can imagine that if you're writing a python package you would utilize this file to run some kind of setup or config before the package is fully ready or loaded and then it can be used by your python script awesome now as well as just doing something like you know printing some message or initializing something you can simplify the Imports of different components or dependencies in your package with this init.py file so right now if we look at main.py the Imports can be a little bit annoying and kind of weird to have to write out and you'd have to know to use the string util and math util library or module sorry in order to find capitalize and add if you just wanted these functions to be available by default then inside of your nitpy file you can actually write them out so I can say from and then I'm going to say matthu till import add and I could say from string UIL like this import and then I'm going to import capitalize and then from my main. Pi file I would be able to just directly import these from util so I could say from utils import add and capitalize that's because they're defined directly in the init.py and anything that's defined in the init.py even something like a variable for example like C can be directly imported when you import the package so here I can now import add capitalize and cut so let's run the code and I'm going to show you actually an error that pops up and how we can fix it so notice that when I run this we actually get an error and this is very common I cannot tell you how many times this has happened to me and people that I know it says module not found error no module named math utility now that's a little bit weird why didn't that work previously when I wrote that import inside of main.py everything was working fine but now when it's in init.py it's not working well the reason it's not working is because we've ran this package from outside of the package we have this main.py file this is the entry point to our script now when this entry point runs a nitpy by default Python's going to be looking in the directory where our entry point file was for these modules math util and string util so it's going to be looking outside of the utils package we need to tell python to look inside of details to find this code and in order to do that we need to use a relative import we can also use an absolute import but typically use a relative import now a relative import is simply this single dot or actually can be multiple dots but the single dot references that you're looking in the current package so if I say from math utility that means from the utils math utility module because we can have modules that have the same name that live in different directories or packages right same thing with string util means look in the current package which in this case is utils for this string util module and then import capitalize so now if I go ahead and run my code you'll see that this works fine and it prints out imported utils however I'm going to throw a spin on this again what if I now go into init.py and I directly execute the init.py file well when I run this notice now that we get an error and it says attempted relative import with no known parent package the reason we now get this error is because we're trying to import this math utility and string utility but we're not currently inside of a package the reason we're not currently inside of a package is because we've ran a nitpy directly and it just sees that it has these Python scripts beside it it doesn't see any directories that it's currently inside of and it doesn't know that it currently belongs to a package it would only be able to reference packages if we had a directory inside of this directory or alongside the init.py so this is why things get a little bit confusing depending on where you run the code the import is going to work differently okay so I'm hoping this is making a little bit of sense when I run main dop it Imports this package so then when we run the Imports here we have to use relative Imports because they're inside of the package whereas when we run a nitop pi which is not usually designed to be ran what happens is it doesn't know we're inside of a package so it doesn't know where to look for these modules and then we get this error saying we can't do a relative import if we don't know what the name of the parent package is so there are ways to fix this but it involves changing the Imports based on how the python code is executed where you can use something like the is ifame equal toore main convention we're going to talk about that in a different YouTube video so stay tuned but the point is you need to know how you're executing this code and where the entry point is because that's going to Define how these Imports work now what I'm going to do is I'm quickly going to set up a more advanced example with multiple packages that are nested inside of each other and you can see some more advanced import patterns and then that will wrap up the video so I've updated our python code now to include a few nested packages you can see that rather than just having utils I've now defined an mu util and an S util package inside of this package so now I've adjusted the Imports inside of my anipop pi to utilize these nested packages and you can see that if I want to import the ad function I now need to say from mtil mathu import ad right so I'm kind of walking through the package structure to know what it is that I'm importing now the question that I want to pose to you and that I'll answer obviously is that from this math utility file if I wanted to import something like the general U function from my general file how would I go about doing that well this is exactly where I can use an absolute import or a relative import that looks up a directory level sorry so from math util if I want to import this General util function what I can do is the following I can say import or I can say actually sorry from dot dot and then I'm going to say General import and then General UIL now this is a relative import and I'm looking two levels up or one level up the Parent Directory or the parent package so rather than looking inside of muil which is my current package I'm looking inside of the uil's package which is its parent package where I then find the general module and I import General util so if I run my code now from main.py you can see that I don't get any errors and everything is completely fine now a slightly different thing let's say from my string util I want to import my Matthew till. py which is in the same level but in a different package now it gets a little bit more complicated but what I can do is say something like from and I'm going to look in the parent package so I'm going to say from dot dot right this is going to look in parent then I'm going to look in the mcore UIL package then I'm going to look inside of what do we want the math util and I'm going to import at okay so if I want to import from a package that that's at the same level story I say okay I want to go up to my parent package which is utils inside of utils I know I have the M util package and then inside of M util sorry I have math UIL I should have named this something better and then I can import add and then same thing if I go back to main.py and I run this notice we don't get any errors and the Imports work properly okay so that's it for this video I wanted to cover all of the advanced import in how you use Python packages with a knit. py if you found this helpful make sure to leave a like subscribe to the channel and I will see you in the next one [Music]
Original Description
📬 Join my Free Newsletter: https://techwithtim.net/newsletter
🎓 Get private mentorship from me: https://training.techwithtim.net
If you've read a fair amount of Python code, then you've probably seen this "__init__.py" file pop up quite a few times. It's especially common in larger Python projects. I'm going to breakdown exactly what this file is, when you should use it, and exactly how it works.
🚀 My Software Development Program: https://coursecareers.com/a/techwithtim?course=software-dev-fundamentals&campaign=youtubedescription
🎞 Video Resources 🎞
Newsletter: https://www.techwithtim.net/newsletter
DevLaunch: https://training.techwithtim.net/
⏳ Timestamps ⏳
00:00 | Understanding Modules & Packages
03:21 | Free Newsletter
04:12 | __init__ Usage
06:40 | Relative Imports & Import Errors
10:06 | Multiple Packages
Hashtags
#Python #PythonFunctions #SoftwareDevelopment
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: Python for Data
View skill →Related Reads
📰
📰
📰
📰
MiniMax Raises HK$16B, Goldman Sachs Urges Pivot to China AI Value Chain
Dev.to AI
seq 14. Great Powers Climb, Then Pull the Ladder Up
Medium · AI
Why U.S. Stocks Keep Rising Through an Energy Shock
Medium · AI
L’IA di Google fa aumentare il consumo di energia elettrica del 37%
Medium · Data Science
Chapters (5)
| Understanding Modules & Packages
3:21
| Free Newsletter
4:12
| __init__ Usage
6:40
| Relative Imports & Import Errors
10:06
| Multiple Packages
🎓
Tutor Explanation
DeepCamp AI