How to Create a Python Package or Library and Upload to PyPi with Twine in 5 Easy Steps #python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome back to the channel if you're wanting to know how to upload a python package to Pi Pi so that you can run pip install and have your library accessible anywhere in the world then stick around for this video because I'm going to walk you through the five main steps in this process I'll explain them very easily and expect no technical background so the very first thing that you're going to want to do is you're going to want to create a Pi Pi or the python package index account and to do that you're going to go to piepie.org I'll link that in the description down below and you're going to click register and create your account now I'm going to presume that you've paused the video right now and you're picking right back up just now with the account already made so once you've made your account you really need to follow on to step two which is Identify some kind of problem that you want to solve with some kind of python library now I'm going to use an example from a single function from another project I'm working on and we're going to create a separate Library around that the idea of this function is to be able to take in a map or a dictionary into a Spacey pipeline if you don't know anything about Spacey don't worry I'm not covering that in this video this is just to show you essentially what we're going to be doing the idea is to take different Nar labels and automatically change them into something that a user might want now there's a couple of reasons to do this one the different models for different languages have different labels for things like person for example in the Spanish pipeline we have per but in the English pipeline we have person in other instances you might want to standardize things that are different and types of entities but part of the same categories such as lock and geopolitical entity so we can see right here on the left hand side an example of this and progress we've got loc for Yellowstone Park and gpe for Wyoming a state a geopolitical entity the goal of this pipeline is to take in the exact same text but allow for a user to pass in a dictionary called a label map and change any instance where the key aligns with one label and assign it to something else so in our case this pipeline should go ahead and automatically change Yellowstone Park from Wyoming from loc from gpe into loc so once you've identified the problem that you want to solve in my case simply changing the entity label it's time to take that function that you've developed and place it into a python file again this is not a video on Spacey so I'm not going to explain any of this instead we're going to jump in to the main thing that you have to do which is step three and that's going to be creating your essentially package setup or your file direct or your package directory and also creating the essential files now not only do you have to have all of your stuff in a python file of your functions and classes you need to actually structure your uh your library in a very systematic way a way in which Pi Pi will expect it in a way in which setup.pi will actually expect it and we'll approach both of those in turn so the first thing that you want to do is you want to have a directory that is set up where the library name is going to be in one folder in this case I'm calling it label underscore mapper and inside of here we're going to have two mandatory files one is going to be the file in which your python classes sit these could be your classes your functions it's important here to not have any print statements use things like logging and stuff like that but for right now we just have one simple function the other thing that you have to have in this directory is something that's called your init file and this is your underscore underscore init underscore underscore dot Pi this is a special kind of python file that is going to be initialized as soon as your python library is actually called by a user so when they do something like import label mapper this is going to be the first file that it encounters when it is imported now the role of this file is to function as this first step when your library is imported it needs to know what to actually import for the user this is where we tell the user ex or tell essentially the the python what we want to provide to the user now our goal is to provide the user with the functions and the classes that are in our main python file and so in this edit file we can save from sorry import dot so from the same directory component the file that the classes are found in and we want to import specifically um sorry from component import and we want to import specifically the classes and functions that are found in this file now for me the name of my class is label fixer now again python is case sensitive so make sure that you have that once you have that set up and this particular format that's all you need to have for your init file automatically what's going to happen when your library is called this file will trigger this and it will go ahead and go into your main python file here in this case component dot pi and automatically provide the user all of this code and specifically give them access to the classes and functions that you specify once you've done this you can test to make sure that this works now one way in which you can do this is you can use something like a jupyter notebook for example so instead of importing this particular component file we can say from label mapper import label fixer and we can execute this exact same section of code just as we did before and it'll run the exact same way which tells us that our library is structured correctly and we notice that everything works as expected once we have our main direct reset up the way we needed to meaning we have our init.pi file set up correctly and our actual python file that contains all of our code it comes time to actually set up the setup file the setup file is going to be the main file that you load up onto Pi Pi it needs to be structured in a very systematic way now there are a lot of things that you can include in your setup.pi file what I'm providing for you here is just a very rough collection of not necessarily the essentials but the things that I would typically view as mandatory some of these are however optional the very first thing that we want to do is we want to import the necessary things so we can say from setup tools we're going to import setup we're also going to import find packages find packages is a very important function that essentially goes through and finds all of the init.pi files no matter which directory they sit in as long as we're in the subdirectory of your main project it'll find all of them and automatically bring them in and package everything for you automatically so that you don't have to do all these steps manually the other thing that we need to do is we need to call set up and what setup is going to take is a series of arguments now here I have some of the ones I would view as standard but some of these are in fact optional now the very first thing is going to be mandatory and that's going to be the name this is going to be the name that somebody uses to install something so if it's label underscore mapper somebody will install it from Pi Pi with Pip install label Dash mapper the next thing that's mandatory is the version now this is very important on Pi Pi every single package name can only ever have one version of the same type this means that there is only ever one instance of label mapper version zero zero one so it's important to start very low here and iterate up however you see fit you can start off with three or of these or just simply one always start off with version one here the next bit here is packages this is going to be set equal to fine packages which again will go through all of your subdirectories here and find all of your init files and load everything up automatically for you the next thing that you need is the install requires install under requires functions kind of like your requirements.txt file it's going to be where you list all of the different packages that your python Library depends upon in this case we only need to make sure that the user has Spacey version 3.0 or higher installed and so inside of this list we have a sequence of strings these strings will point to a very specific library and version if necessary the other thing that we have here are really things that give some indication about who you are what the the library is actually supposed to do and some guidance on how to use it so I've got four things here again I view these as something that you should probably have it helps give users a sense of what this library is supposed to do I've got my name so people know who I am I also have a brief small description here usually just a few sentences in this case I'm not using this Library this is a python function that'll be wrapped up in something else I'm working on and then I have a long description a long description here is going to take a rather unconventional argument it's not going to be a long string I like to use this because this opens up a readme file it means that I can ensure that both the GitHub account or GitHub repository where this data will sit and the pi pi.org will all align with the exact same readme what this bit of code does is it opens up the readme file and which is a markdown file that explains essentially how to use it how to install it essentially a lot about what this project actually is it goes ahead and it opens it up reads it and loads it up as a long description automatically and then finally I specify long description content type this clarifies how this content should be read in this case it's a markdown file which has its own particular syntax and this tells it to essentially read it as a markdown file so once we have all that we're able to then go ahead and move on to step four which is installing a library called twine so if we run pip install twine we can go ahead and install twine into our into our environment or into whatever we're actually working with here in this case this is my root python directory on conda once we have twine installed we can go ahead and actually upload it to pipi.org and we can do that by using the command two separate commands here first we need to package our library and then we can actually upload it on to Pi Pi so the first thing that we need to do is we need to run python setup dot pi and S dist this is going to create a distribution folder automatically a disk folder for you automatically right here which is where your tar.gz file actually sit this is a compressed file that contains all of the data all of the package that you want to upload on to Pi pi.org it'll handle the creation of this for you automatically once this is created it comes time to actually upload it we can do this with twine by saying twine space upload and we can specify that we want to upload everything that sits in the dist folder by saying dis slash asterisk and this will upload everything automatically for us once we do that we have two things that we can enter in the first is our username and the second is going to be our password which I'm typing in right now and if everything works correctly it'll upload automatically it's already done it's a very small package and we see that it's uploaded right here and if we click on this link we'll notice everything is now uploaded correctly and this is when it comes the fun part we can actually test to make sure that we've done everything as we would expect we can run pip install um pip install in this case label Dash mapper so normally what I would do is I would create a fresh environment but if we don't want to we just want to run pip install label mapper I think my base environment is actually a little corrupted at the moment but it's downloading everything it's loading everything up correctly maybe it'll work actually just fine on this on this computer we'll see and everything's installed correctly so at this point I can run PIP show label Dash mapper and I can just make sure that it's been installed correctly within a few seconds we'll see that it actually has and we can see the summary right here we can see my name right here we can see where what version it's on we can see its name and that's how you go ahead and create a python library from scratch and upload it onto pipi.org if you follow these five steps you'll be able to do the same thing with your own project again this is just a simple toy example of something that you can do but if you have an idea for something that you would like to contribute go ahead and follow these steps with their own package and upload it and once you have link it down below so others can see and follow along as always thank you so much to everyone who supports this channel you get a lot out of it I do all this for free feel free to support it either via patreon or via YouTube membership both of which are linked in the description down below
Info
Channel: Python Tutorials for Digital Humanities
Views: 2,420
Rating: undefined out of 5
Keywords: python, digital humanities, python for DH, dh, python tutorial, tutorial, python and the humanities, python for the digital humanities, digital history, Python and libraries, python tutorials, python tutorials for digital humanities, Python, PyPI, Packaging, Programming, Code Sharing, Open Source, Tutorial, Python Tutorial, Python Package, Software Development, Coding, Development, Package Management, Python Development, Python3, setuptools, twine, how to upload to pypi, how to use twine
Id: OI6M9BRwfJM
Channel Id: undefined
Length: 12min 35sec (755 seconds)
Published: Mon Aug 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.