PyPi - How to Publish your own PIP package

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'm going to go over what pi pi is and how you can use it to publish your own python projects that means by the end of this video you will be able to publish a package so anyone in the world can run a pip install to download and use your code so very quickly to give some background for those that don't know pi pi is the python package index pi pi allows you to find install and publish python packages this means that every time you do a pip install package name pip is going out to pie pie to download and install the package let's hop into a terminal and i'll show you how easy it is to publish your own python package okay so to get started with your first pie pie project start off in an empty directory i have one here all i have here is a readme file which has some instructions that i put together for us as we go through this now i'm also going to be going through the official python documentation on how to publish a pie pie project so i'll be referencing this but in my readme i sort of shorten it down and made it a little more simple than this official documentation so let's hop back into here and i'm going to open up a terminal and the first thing we want to do is make sure we have an upgraded version of pip since this is a pi pi project and we're going to be using pip to install it as well as do some pushing of the package we want to make sure we have the latest version so i'm on windows so i'm going to take this windows command and then there's the linux version of the command as well i'll make sure to add this to my github in the description below if you want to follow along so i'll take this and paste it in and this is going to go out and upgrade pip for us after that i have some instructions on how to create a directory structure and this is what our directory structure is going to look like so you should always include a license file this is just going to be some licensing information we have a pi project dot toml file i'll be going through this basically this has all the information in regards to the package as well as setup.cfg after that we have readme so it's just good practice to always have a readme file so when you upload this to github as well as pi pi this is going to be the documentation supplied with your project after that we have a folder named source and then under there is going to be our package name so this subfolder name is actually important when you do your pip install and then you do your python import statement this is going to be the package name so let's make sure to name that properly and then with any package you have this init.pie for our example we're just going to keep this empty and then we have our python script there's also this test directory this is for including any tests that you want with your project so let's go ahead and create all these i have commands to do that here so i'm just gonna copy this in and paste it and you can see that this created all the directory structure that we required so we got our test directory our license and our pi project and setup config and then our actual python project so i have this package named as my package make sure to rename this to whatever you want your package name to be so now that we have our directory structure created we want to modify this pi project dot toml file so i'm going to take this and paste it into the file and basically what this is is just specifying the setup tools required for building your project for most of your projects you're just going to leave it as is and use setup tools and wheel this should be sufficient for most of your projects after that let's go back into the readme let's copy and paste this configuration into our readme and this is going to be a lot of the information that you're going to want to customize for your project so i'll paste this in here and let's just sort of go over these settings so this is going to be the name of your project so when you do your pip install this is going to be the project name so i'm just going to call this my project and the version number i'm going to leave at 0.0.1 and as i make revisions to my project i'll have to make sure to increment this number and then some of these fields are pretty obvious so i'll just fill them out right now that's fine for our name i don't have a github url but i can add that in in a later update and most of this i'm just going to keep the same now one thing that i'll mention here is you could actually instead of using a setup.cfg file you could use a setup.pi file and this would allow you to use python to dynamically set some of these settings but the current recommendation that the pi pi foundation gives is to just use setup.config if possible okay so after you have your directory structure created your pi project dot toml and your setup.config setup you'll want to run the build so before we do that let's open up another terminal here and we're going to make sure that our build tools are installed and upgraded so i'm going to take this command which basically does a pip install upgrade build so this is just upgrading our build tools which we're going to use in the next step i'll scroll down here and you can see that we're just going python dash m build and this is just going to build the project in the current directory so i'll go python dash m build and this should create another folder for us with all the files that are required for this project and this is what's going to get uploaded to pi pi okay so it looks like that ran properly you can see the naming structure that it gave so it says my project the version number and just some additional information there if we look in this folder here this is everything in regards to the pi pi package that will be uploaded to pi pi so now that we have the build completed let's go ahead and upload this to pi pi so i'm going to go back into our browser here and i'm going to start using this documentation and if i scroll down you can see that it suggests that we use twine so i'm going to take this and update my documentation here so the first thing we want to do is uh upgrade twine and then we can upload to pi pi but what they're recommending is we actually instead of doing it to the production pi pi it wants us to use test pi pi and i suggest you do this as well so before you do that go on over to test.pipepi.org so we'll open that up and you can see we're in test pipe and i'm logged in if you're not already logged in go ahead and do so or create your account once you have an account created with a password go back in here and make sure you've upgraded twine so i'll do that right here and once twine is finished upgrading go ahead and take this command and put it in and basically just to break down this command it's just taking twine and it's saying upload and the repository that it's uploading to is test pi pi if you were uploading this to just pipe.org not the test instance you could remove this switch and uh this repository name and then this here is basically referencing this folder so this is the project that's going to be uploaded to test pi pi so i'll go ahead and enter that and it should prompt me for a username so i'm going to put in my username for test.pipe.org so i put in the wrong username and password so let me try again okay so i'm getting an error here and it's saying 403 forbidden uh user brad morg isn't allowed to upload project my project so basically what this is saying is there's already a project named my project on pi pi so this is a name collision i wanted to show this to you guys just in case if you ran into this issue so what i'm going to do is i'm going to make this project name unique just to make sure that this project uploads properly so i'll go back into our setup.config and instead of being my project which is pretty generic and obviously someone's taken this name already i'm gonna go devops journey dash my project and uh i'm gonna have to redo the build so let's redo the build here and i'm actually noticing that my old files are there so i'm actually gonna delete this as i don't want that to cause problems so i'm going to delete that and then just redo the build and just make sure everything's fresh here and when i have a look now you can see that this distribution directory looks a little bit cleaner so i like that let's go ahead and see if we can upload that i'm going to go back to my commands for that and it was the twine upload command so let's do that put in that username and it should work this time and it looks like it did so if we go to this url we can see that we have our project here so it tells you everything about it it says pip install and then it gives this extra parameter that you guys probably aren't really familiar with basically this is just referencing testpi.org if we were doing this to the regular pi pi production site then you wouldn't need to include that and then it has our readme and everything so this is looking like a nice good professional project so let's go ahead and make a change to our project and then i'll show you how we can actually install this using pip so if i hop back in here i'm gonna go here into our source directory and then i have this my package and then i have something called main i'm just gonna rename this to uh channel and i got some code ready that i'm just going to paste in here and basically what this does is it opens a web browser and goes to my youtube channel so that's the change that i've made in my code was add this channel.pi file and i should be able to go into python and import this after doing a pip install so let's go ahead and upload the change to pi pi and then we'll pull it down using pip so let's go into setup.config and all we need to do is increment the version number and then let's uh redo the build so we'll go pi dash m build and if i expand this we should see a new version of our build come up and it's fine for us to leave our old build files in there as well so i'll just leave them and you can see the version two tar dot gun zip file is there and then it looks like the wheel file just generated okay so it looks like our build files are there let's go ahead and upload it to pi pi so we'll run twine upload put in the username and see that it uploads and there you go you can see the new version here and it gives us the installation command so i'll pull that into my clipboard and let's go ahead and open a new terminal and i'm going to go ahead and run this command which should install my project and there you can see that it installed it now if i want to load it up i go into python and having a look at what my project looks like i'm not going to import it as devops journey dash my project i'm actually going to import it based on what this folder structure looks like channel dot pi so let's do that so i'll go from my package import channel and you can see that it opens up my youtube channel so there you go that's how you can create a new project and package it up and upload it to pi pi so other people can go out and use pip to install your project so once again i put all reference material in the description below so make sure to check that out if you want to use it to follow along and create your own python project anyways i hope this video was helpful if you want to learn more about python go ahead and check out my other python videos i also have a couple playlists dedicated to python and if you're interested in learning anything else about it or devops in general go ahead and subscribe to the channel thanks again for watching and hope to see you all in the next video you
Info
Channel: DevOps Journey
Views: 21,456
Rating: undefined out of 5
Keywords: how to publish pip package, how to publish python module, how to use pypi, learn python, own pip package, packaging python projects, publish pip package, publish pypi, publish python module, publish python modules, publish python package, publish your module on pypi, publish your own python library, publish your own python package, publish your python library on pypi, publish your python package on pypi, pypi, pyproject.toml, python programming, python tutorial, python twine
Id: v4bkJef4W94
Channel Id: undefined
Length: 15min 0sec (900 seconds)
Published: Wed Oct 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.