Python Django - Create and deploy packages to PyPI - Python Package Index

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to another tutorial so in this tutorial we're going to build a standalone python django package which we're then going to be able to install into other django applications so i've got a pre-built application here that i'll show you so that's already made we're going to prepare it and package it up in preparation to then upload it to the python package index so we're going to need to go ahead and register an account there now there are two versions of the python package index the tess python packet index and also the the normal index so we'll be utilizing here the test python packet index and not uploading to the actual python package index but it's the same process but the test package here or the test site just allows us to practice and work with the package python package index in preparation for then for us to then actually upload it to the actual python package index so once we've done that and our package is available we can go ahead and start a new django project and download our package as if we're downloading any other package django or whatever package we download so just in case you haven't gone to the python package index the python package index abbreviated as pi pi now i call it pi pi although it's pronounced pi p i this is an official kind of third-party software repository for python so here you're going to find like it says here 292 000 projects packages which you could potentially use to integrate with other python software and that you're running so so far on this channel we've been utilizing this to download django so we'll find django in this package index and then we've also installed other packages onto django that are obviously dedicated to be installed onto or integrated in django so there's a widening range of software packages here on the index as well as obviously django packages so in short it's a software library where we're going to grab software that's been uploaded now what we want to do is we're going to set up an account upload our package to the package index so that we and other people can download that package so while we're talking about packages let's just remind ourselves about package managers so we have this package repository now we're utilizing package managers to access and download these packages from for example the python package index now we're not limited to that most of these packages here will also make it available or it would be possible to download from other resources maybe other private resources github for example or just locally so we're going to use these python package managers like said to in help us install applications uninstall packages that's actually going to manage or help manage packages on your system or within your environment so that's really the the main benefit of packages and why we're going to be utilizing why you're using package managers to download these packages from these different sources so packages need packaging so what we're going to do here in this tutorial is obviously package up an application so packages are designed to exist or packages exist to be deployed and installed so when we think about packaging we need to think about developing our application for packaging so that's an important step which i'll take you through when we start looking at the code just some basic overview of some things to think about because we want to ensure that our package has everything we need to actually run that piece of software that package when someone else downloads it so there's a few things that we just need to think about so some other considerations to think about here when we're packaging something is who's going to actually run this are they going to be technically minded etc how they're going to run it and things like compatibility what versions of python or django does it require and then where is it going to be deployed what type of environment is going to be deployed in so those are just some things that we need to think about when we're packaging a package and that's generally covered by documentation that we can create and then also another consideration we can also integrate here with packaging is testing so just looking at a basic packaging requirement here and the steps that we're going to go through so obviously we're going to need python installed so these are double dash version and then obviously we're going to be utilizing pip here i say obviously we are going to be utilizing pip here and then we're going to need some other tools so setup tools which i'll explain shortly and will and also twine so what we'll find is if you're using a virtual environment pip already has installed for you the setup tools and will and then all that needs us then is to install twine so twine is basically a utility for publishing python packages onto the python package index so it's a tool that actually will allow us to actually upload to the python package index so just to guide you on a brief overview of here the setup tools so the setup tools this is what's going to facilitate the packaging of the python projects or a project that we're going to package so the setup tool that's where it's all directed here so that's going to actually help us package the python project now i've left you a link here to have a little read because there's lots of documentation here with this software we're just going to go through the basic setup introduction here so we don't really need to know much about it other than we need to use it to package up our project so finally will so will is another one of those utilities um to create these while files l files so we're always going to help us uh in terms of the insulation support so will is going to create um a a package that is fast to install um that thinks about stability um and just generally supports the insulation process that's how i'm going to generalize it here so there are different uh packaging we could also think about wheel and as a different packaging format and we can compare will with something called egg and i've left you a a link here to a a comparison of those two tools so the workflow is going to look like this we make the application again we've already pre-built it here we then create some documentation that's going to support the user so they know how to install and integrate it within their application and then we're going to run the setup tools to build the package and then run will to think about and to support the installation of that package and then we're going to run twine to actually then upload to the package the python package index okay so go ahead and download the repository that's linked in the video description of course you could just use your own project if you wanted to adapt your project into a package maybe just uh make a backup of it first before you start making a package so ideally you would have a small scale type of application here which maybe has um database dependency um that would be useful so like i said have a look in the link in the description we download this as a simple application here an inline form set application so once you've downloaded standard practice we're just going to create a new virtual environment and like i said before that's going to install the setup tools and the wheel tools so we'll just do an update just to make sure they're updated once we get to that point so just go ahead and activate that and then there isn't a requirements file here so all you're going to need to do is i'm going to just uh pip install django so that obviously comes from the package the python package index so upon actually installing and running the server you can see i've end up with this small application here where i can add um well there's already a database here in actual fact so we've got some authors here so we can add an author you're not seeing this and then we can edit the books for that author it's just a showcase in inline form sets so this is the application that we're going to build into a package so just thinking about building an application into a package here's what we need to think about now inside of here for example we may have a separate template folder and also a static folder now what we're going to do is we're going to take this application that we're building here in this case books and we're going to turn that into a package so what we just want to make sure in terms of a basic case here is we want to make sure that all the resources are inside of the books folder so here you can see i'm using templates here now if we think about what actually needs to be connected so imagine we were to package this up now and install it i guess there's just two things that we need to make sure that we have now this has a model so once we've installed this package you would imagine we would need to follow the instructions if you've installed any package before you'll know that sometimes you need to add it into the installed apps so we need to create some instructions for the user to tell them to do that and then else what else we need to do like we have in the core we're going to need to provide a path to the actual books application so those are the two kind of setup things that we're going to need to document when we're creating the documentation for users and also that outlines those initial points to consider when you're creating an application here or packaging up an application so you can see that the templates are being served within the package in this case so maybe now you can start to see or get a feeling of um how potentially he might want to work with django because you may build an application in django which can be a standalone application which you can then reuse in other applications that you build and if you start thinking and designing your applications in that way django working with django becomes incredibly quick potentially to build applications because as you build more applications and build more apps you can then start to think about making them into packages smaller package packages which you can then use on other applications so let's just go ahead in the console here and we'll start to run some commands first so let's just uh close the server control c and let's just um oh let's just make sure we've got the tool so this is a pip install uh upgrade pip setup tools and will so it looks like um we're doing some updates there potentially and installing all the dependencies we need now it says here could not install package due to environment error access is denied consider using dash dash user option check okay so let's just uh pip install uh let's just do this menu pip install the setup tools okay so now it says uh module not found so okay let's do python minus m no module name pip okay i'm not too sure what's happened there but there seems to be a problem uh with that found existing insulation pip uninstalling what did i just do i um pip install upgrade pip so i upgraded pip setup tools and wheel and it looks like what's happened here is that it's uninstalled pip and then there was an error um and i couldn't install it again okay okay so i won't do that again um i just took the easy option there and just uh reinstalled the virtual environment so let's go ahead again then and see what we've got here so let's go ahead and and try that again so we're running the same commands again so we could not install packages to pip okay so pip install setup tools okay so setup tools are okay and it's up to date and then we'll there we go so we've installed uh will there we go so i've just individually installed those setup tools and will so one thing i do apologize then i thought our wheel and setup tools automatically installed when you start up a virtual machine but that doesn't seem to be the case so the problem here looked like i was trying to update will and it wasn't installed and that's why that command just wasn't working so when i run it again you can see um everything is now okay so that's uh seems to be the root of the problem there will isn't installed okay so now we have everything in place um let's go ahead now and actually create a new folder we're going to call this django books so create a new folder called djangobooks and we're now going to put our books app directly into this new folder so let's move it in okay so we now have our new app folder our standalone app folder obviously your system isn't going to be working now because we've moved the books from the project folder into the new folder django books so just to confirm we've tried to start the server you can see that here we get an error here the file name directory file volume label is incorrect so let's go ahead and build some documentation so i've already gone ahead and done that so inside the django's books folder i've created a new file here called readme.rst and you can see inside of here i've got a simple readme file where i'm just telling the user about how to install or the steps to install the app so they first of all need to go to installed apps and use books and then they need to go to the the urls and add this new path i remember i'm using namespaces for this application so i've included that in and then what you could then need to do is obviously run the migrate or make migrations migrate in order to migrate the model tables that we're using for this product and then they can go ahead and actually then serve this from the books url okay so that's the instructions there in the readme.rst now i have made a mistake here on purpose just to show you this so next up we're going to need to create a new file and this time we're going to create a setup so a setup dot uh it's going to be a cfg config file so setup file so this is the file that's going to be needed for the package index this is additional information we're going to pass over to the package index so that it has all the information needs in order to correctly display and serve your application so there's a whole host of different setup information for this file and i've left you a link in the description for this but let's just go through like a basic setup or look through some of the options that we can define here so here we're going to set out some metadata so data about data so this is going to be data about our application so the name um the version maybe 1.0 whatever it is at the moment the author author's email we can also have a maintainer as well um name and their email um so for admin for example and then we can also set out a basic description and also have a longer description which is going to link into our readme file that we've just created so we continue with a url and then also we can set a license uh some keywords for example for application make it easier to find and search for and then we create some classifiers so here we're setting out the environment kind of variables uh the frameworks intended audience the license again uh the operating system and programming language and at the end here we've um kind of identified where this potentially should be index topic software development libraries python modules so like i expressed earlier there's lots of different options here and if you want to kind of learn those i have left a link in the description for you to learn a little bit more about some of the other options we've got available here so in addition to the metadata we also have options and what i'm going to do here is in use the include package data equals true so this is a setting which essentially tells the setup tools to also install or copy over any data files it finds in in your package so for example here we can have a readme file and then potentially maybe a setup file for example in the license file uh we'll set that up too so yeah here we're basically just saying to the package manager yep also include that in the package that gets distributed so lastly we have package find so this is just a quick setup we can specify the packages we want to find here fine colon is just going to kind of automate and speed up that process so that's a basic setup file that we're going to need in order to upload to the python package index so next up then let's just uh why i mentioned that let's just talk sort out our license right so let's uh sort that out so this is just going to be an mit license so i'm just going to copy one in i've got already there we go so that's my license sorted now remember i'm here specifying that to also include that in the setup now this is only going to be available if i also include a manifest so let's create a new file i'm going to call that manifest dot it's going to be an im file so we're also going to need this too and then in here we just need to specify the include so we want to include the uh the license that's what we just uh created right so and we also want to include the uh the readme file that's a rst and then we also potentially are going to include uh for example templates so we can also kind of specify that too so books uh templates so we're going to include those in there we go so lastly we're just going to point setup tools to our setup config so it can then perform all the actions so what we also need here the last file is going to be the setup.pi file and in here basically we're just going to tell um so uh from setup tools uh let's import setup and we'll just yep we're just gonna then basically set it up or import so that just uh brings in the setup tools okay and then sets everything up okay so now we're ready to actually package our application um so let's go ahead so we're going to use a setup tool so pysetup.pi so we're running the setup pi file okay before i forget remember the directory that we're currently in here so we're currently in the main core directory directory of our application so we want to go into to make this easier we can go into this folder here so let's just change the directory into the django books folder and then run the commands from here so here we're going to run the python command uh so we're going to run the setup file uh okay so let's go ahead and do that so you can see what's happened now if i go into my books here i've created a new folder here and it looks like we've tried to kind of package this up we've got this uh gz file here this compressed file which probably contains um our application right so let's go ahead now and i run the wheel so pi setup.pi and we're going to do be dist uh wheel so we run will and there we go so we have now two files uh two core files here um one's a whi file uh and we've got the payload so uh if we go based upon what i've told you already we could say potentially uh we've now got a a setup file and a payload file um i'm gonna go with that um so we've got these two files now so that now sets up the uh the distribution happy days so in actual fact we're now ready to upload it so at this point i just want to say that i'm just showing you to start off with a basic setup that's going to just get this working okay so there's a number of different steps and flags that we could explain here just don't want to make it too overbearing just want you to kind of get this working so we can get through that initial stage and then we can start thinking about some of the the different parameters and like i said testing before and so on so one thing to do actually before um before we run this i think we need to pip install twine that's something we need to do next remember twine is going to be the utility that's going to allow us to upload it to the python packet injects index so let's go ahead and install that and this comes with a number of different tools that we can use and one thing that we can definitely use before we upload it and waste any time is to run twine and then we can just basically check it so we can just check the distribution files so we're just going into this folder here called dist and you can see start all so just check to make sure everything's okay now i did leave an error on purpose here so we should have an error so you can see here we've got a warning line one title overline too short okay so you can see that that's uh the case so let's just go back into our um let's see readme file so just complaining that these are these little equal signs here um they just need to be over the actual title here for example so it's very specific this type of setup this syntax here so if you're not familiar with this this could cause you a little a few problems so go ahead and just copy and paste this to begin with and then kind of start to drill down into the more specifics when you've got time so let's just run that again um okay so notice now when i run that again what's happened i've got the same problem because what i need to do now is actually rebuild again so i need to just go through the process again the yes disk you can see that yep and then the the wheel we can just go through this process that was the process right so now we can go ahead and check again you can now see the error is gone uh so we can also this warning here the fact that we can also define our readme file uh for example utf-8 we could define it as utf-8 but the default is text x rst so these are just different form essentially styling formats i guess we're describing here and the styling here um that is required in order for this to be compatible uh with the tex x rst and that was a problem we were getting earlier basically it wasn't compatible so we can define that in our in our setup here so we can use we can define uh the the content type right so and now we've done that we yeah i think we're ready now so we've set up our files again we've re uh configured the the settings so now we're ready to upload so like i previously mentioned um the python package index has two two sites say okay so we've got the live index um so that's the pypi.org and then we've also got the test website so you're going to need to sign up individually for these packages or for these services so go ahead and sign up and i'll see you in a couple of seconds so i've headed over to the test.pipe.org this is where i'm going to upload and just check my package can be uploaded and then use the package from there so you can see once you've logged in you'll need to go to the account settings so go ahead and do that we need to create an api key so let's go down into you're going to need to verify your email in order to for this to work and then go down to your api token and create your scope so here you can just select the project for example you want to use the api for i'm just going to select initially entire account and then give it a name and then add token from there you'll be presented with a token i think it's a one time only token so you only see it once you have to record it and then that's your token that we're going to use to then authenticate to log in and upload your package so what we have available here is we can we can go ahead and do this and type this all in of course but we can also automate this if we want to now if i run for example pip uh pic config list so this is going to be a give me a list essentially um of where variables are essentially going to be found and utilized now what we can do here um is we can create a file that has the login details already created and obviously then every time we try to upload it's just going to use those login files now on linux and mac is fairly simple to do this but on windows is a little bit quirky where you need to actually put this file so if you're using windows go ahead to your c drive users and then your user and in this folder here this is where you need to actually put this file so we're creating a dot py pirc file now if you haven't made a file like this before what's generally going to happen is you're going to right click say and make a new folder right file so a text document right and then you're going to say dot whatever now what's going to happen now is that in actual fact what you've just built there is a dot filename dot text file so what you actually need to do is you need to kind of open it up here and then go to file save as and then you can select all files and then just put doubles around and then save and then that will actually then save it as the file type that you need here so this is now an asd ssd file which obviously is nothing at all so let's just get rid of that so you're building here a dot p y p irc file now inside of that here we can set up all the configuration for the pi pi pi pi site and also the test pi pi site so here what we're going to need is to use the username um as underscore underscore underscore token underscore underscore so the doubles and then the password is going to be using pypi dash the then the actual api key that you're provided so there we got the username and the password already set up so we don't need to actually type that in okay so now we're ready to upload so let's use twine to do that and we're going to upload our repository and then we're going to specify the test pi pi and then of course we then need to say what we want to upload so that's in the disk so just make sure you're in the right directory remember i'm in the package i created the folder sorry django underscore books and then dis slash star so you can see that um everything should automatically happen um so it already says here that the file already exists because i've already done it to check to see if it works so that's why i can't upload it so what i can do is obviously go ahead and uh change the names around or if i go into my projects here and you can see that it already exists so let me just show you how to kind of um delete this again so we've got this project so we can go to manage and then you can see here we've got settings and down here we've got delete project that was very quick i do apologize um so we need the project name which was called django books so django books delete project there we go so that's how to delete your project if you're wondering how to do that so that you can find the other deletion where you actually delete the files but the project actually still stays there the container project still stays there um so that's how to completely get rid of the project now it might take a couple of seconds just to upload before you need to then try and upload again sorry update so let's just try that again so go ahead and actually delete the uh dist folder because i just wanted to show you this so if you do have this type of problem you notice every time we run the command to package you can see that we're building all these new packages and we're trying to upload all of these packages which is not necessarily ideal so delete this folder and then run this setup again so that's the the s dist and then the the wheel and now try and upload there we go so now we've uploaded we now have this available on the test package index website so we can have a look okay so i think i've covered there all the basic problems you might have initially um so do remember every time you want to recreate your project delete the folder and then recreate it and you can now see i've got this i have my project here um i can manage it if i want to so it's a version three and so on so there's not too much i can do here that we need to talk about i can view it on the site so that's what it's going to look like it's got my setup instructions as it's set in the readme file so notice that on the readme file the format when there's two colons it means there's going to be a line break that's why there's a line break there and that's how to create a line break um if you're not too sure how to use this formatting syntax so yeah so let's just follow our instructions shall we and try and build a new application with this project or this package so here i'm just going to start from scratch again i won't take you through this but i'm going to build a new virtual environment install django and then i'm going to try and store my application so once my project's ready you can see that i've got the string here that i need to type in to actually install this project it's slightly different from what maybe you're used to installing from the link here um so i'll just go ahead and copy that let's go back into the project let's just bring that window down a bit okay so let's go ahead and run the command to install it okay so now we've installed so what we need to do now is just follow our instructions now the first thing is we need to go to the core and the settings so we just need to actually register our app of course so it was called it's just called books um we can double check because if we go into the event folder and lib you can see here it's called books so that's the package where it's installed nicely next to django which we've installed by the package manager when we created this project okay so you can see it's there it's been downloaded the book so i'm going to call it into my project and now i need to run the migrations i'm not too sure if i've migrated or not yet but you can see here at the end of this migration it says the books database has been created so let's just create a um a cpu user in case we're going to need that and just check that okay and we're going to finish off with this uh run server house yep ground server so we're going to need to set up the urls that's the last thing we're going to need to do so in core the urls now let's have a look at our instructions that we set out so we're going to need to include um sorry i'm doing this too quick include uh okay so we're going to need to include and let's just go back to our instructions um and we're just going to need to copy this in so path books migrate etc now you notice the format in here needs to be changed slightly so the singles are incorrectly formatted okay i can see that's gonna happen for the whole thing here um okay so once i format and formatted this path here uh correctly uh so now we should be able to go into books and access our project so the server's on so let's open a new tab and let's have a look so we've got the we've registered the url it looks like so slash books and there we go so we've got a book application so let's see if we can add a new offer new so we do have database integration and we can edit those books a b and c say and there we go so if you manage to do all that congratulations you've just built your first python django package um hopefully this has kind of inspired you now to think about programming and building your application in a way to create these reusable apps that you can then plug and play into other projects um that you're potentially going to develop so as per normal thank you very much for listening hopefully it was useful and hopefully i'll see you in the next tutorial
Info
Channel: Very Academy
Views: 3,504
Rating: undefined out of 5
Keywords: python, django, python django, pypi, testpypi, django reusable app, package django app, how to make package for pypi, django 2021, django tutorial, djangotutorial, django tut, django 3, django examples, learn django, django beginners, beginners django, django example
Id: S1XSmiIV3LI
Channel Id: undefined
Length: 36min 19sec (2179 seconds)
Published: Wed Feb 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.