Python Package Tutorial - Organize Your Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
here we want it's Anthony from pretty printed here in today's video I'll be talking about how to create packages in Python so in the last video I showed you how to create modules which basically means creating files in the same directory that allow you to import code from those modules and use it in a different source file or from the Python interpreter so a package is similar except that it has more in it so it can have like sub packages and multiple modules and it can also be used from a different path so you're not quite in the directory you're outside of the directory looking at the package and then the package would have all the sub directories so that's probably a weird explanation so it's better if I just show you what I'm talking about so the first thing I'll do also is I'll create a package I'll call this my package and inside of my package to make it a actual package I have to create one file and this one file is going to be called underbar underbar init underbar underbar dot pi so this file doesn't need to have anything in it it just tells python that this directory should be treated as a package so now I can add another file in here and let's say I'll call this file demo pi and now I have two files in here and I'll just create a one function so I'll say demo print now in the demo file so I'll save that and from the Python interpreter I will run this demo print file so we see here that I have my package and I'm not going to descend into that directory where demo is I'm going to be outside of it and I could be outside of it because this is a package so I'll demonstrate by running Python and I'll import my package dot demo and when I do that I now have access to the code in this demo file so I'll do my package dot demo demo print and we see that we are now in the demo file and if I didn't want such a verbose name I can do my package demo as de and I I can do d dot demo print and then I can also do from my package demo import demo print and then I can call it demo print directly so all those do the same thing so now if I wanted to create a sub package I'll create a new directory I'll call it sub package and then in here I'll add another file and if you guess underbar underbar and Nitz underbar underbar pie you are right and I'll add a file in this sub package so let's call it sub demo dot PI so go back to here whoops and then I will need to exit out just make sure it registers now I can import from my package dot sub package dot sub demo and let me just add a function here so demo sub demo print now in the sub demo package and file okay so back to the interpreter so import my package sub package stocks sub demo and then I can do my package dotsub package dot sub demo dot sub demo print and I see that I'm now in the sub demo package in file and of course if I wanted an a Lee I could do my package that sub package dot sub demo as SD SD dotsub demo print or I can do from my package dot sub package dot sub demo import sub demo print and then I do sub demo prints and I get the same result so this is how all of the packages that you're used to importing and python work based off this principle of course they're not in the directory that you're working in there in the general package directory for your actual Python installation or wherever those are kept but this is just a basic overview of how to use packages so if you want to organize your code into sub directories you can do so you don't have to have everything in the top-level directory to use it and also in this way it makes it a little easier to distribute your code if you ever choose to do so so that's it for this video if you have any questions about using packages in Python just leave a comment down below and I'll get to it if you like this video please give me a thumbs up and if you haven't subscribed to my channel already please subscribe thank you for watching this video and I will talk to you next time
Info
Channel: Pretty Printed
Views: 102,440
Rating: 4.839407 out of 5
Keywords: python, packages, __init__.py, programming, imports, organizing code, tutorial, beginner tutorial
Id: qmsTqQbcBNM
Channel Id: undefined
Length: 6min 10sec (370 seconds)
Published: Sun Jun 05 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.