How to Publish a Python Package to PyPI (pip)

pixegami · Beginner ·🛠️ AI Tools & Apps ·2y ago

Key Takeaways

This video demonstrates how to publish a Python package to PyPI using tools like pip, setup tools, wheel, and Twine, covering project setup, configuration, building, testing, and publishing.

Full Transcript

publishing a python package allows you to share your code with others but also make it easily installable you can use this to make your own python library or CLI application that you'll be able to install with a pip command in this tutorial I'm going to walk you through step bystep on how you can create a basic python project that you can publish to the piie repository once you've published it you and other people will be able to install this package using pip and then you can use it as a library or a CLI command so if that sounds good let's get started first you're going to need a couple of tools in your python environment so run this pip command to install setup tools wheel and Twine which we will be using as part of this project next you're going to need to create the project structure for your package so here's an example of that in this case I'm calling the project pixami hello so that's going to be the root folder and inside I'm going to have another folder with the same name which is going to be the package that I'll actually publish later on um but outside that folder in the root directory I've also got a setup py file and a readme file in the main.py file I'm just going to create a simple function which I'll name hello and it's just going to print this Hello message so it doesn't really do much at this point but I just want to see if I can create a package out of that and then use this function we're also going to need an init file as well in the same directory so go ahead and create that and put this content in there import this hello function we created earlier into this init.py file and just in case you're not familiar with this let me explain what it does the init py file in a python package serves two purposes first of all it tells python that the whole directory here should be treated as a package and second it controls what's actually available when that package is imported by actually importing this hello function to the inet piy file you can allow users to directly access that function straight from the package so this actually looks a lot cleaner and more straightforward when you actually use the package um so this is what it will look like once we've actually published it and we want to use it next we're going to need to configure our setup.py file which will contain the instructions for how to bundle and publish that package here is an example code of how you can structure your setup file it's going to have the name of your package which should match your folder name um and it's going to have a version number as well so you have to make sure that you increment this version number each time you want to publish a new version of this package and finally if you have any dependencies that you want your package to use uh you can put it in this array as well now there's actually a lot more parameters and options you can add to the setup function uh but we'll come back to that later for now if you have this file and this basic config we can move on to the next step which is actually building our package this command will generate two distributions for us a source distribution which is usually just the Python scripts and then a wheel of distribution so this usually also contains binaries uh which are plat form specific and these archives will have all the necessary files for installing and running the package now let's hop over to the code editor and actually give that a shot so here I've got my root project folder and inside I've got my actual package folder which I've called pixami hello uh and here there is my main.py file with my hello function which I am importing in this init.py file like this so this is going to be the package that I will create and distribute and publish now outside of that I've got my setup.py file and my readme file and also my requirements file but you don't need the readme and the requirements file so that's just up to you whether you want to include it or not um but the one that you need is the setup file so here's the code that we saw earlier it just has the name of the package version 0.1 and then no dependencies so it's just going to install on its own now let's go ahead and run that setup and after that's finished you could see it's created a couple of different directories for us there's a build folder and a distribution folder and the important file in here is this wheel file because this is the one that uh we can distribute and other people can install so once you see these files you're ready to move on to the next step now before you actually publish the files to the online repository you might want to try installing and testing it locally first just to make sure it works properly to do that you can run pip install directly on this wheel file that we just produced a moment ago just make sure to replace this command with the actual name and version of your package if you're trying to follow along now if like me you're running this and it's already installed or you just want to refresh the installation you can also use this Force reinstall flag with your pip command after that you should see a message saying that this was installed successfully and if you type pip list you should see this package show up in your python environment as well now with it installed in your environment you can create a new folder or a new file completely and then actually import and use the library that you've just installed so here I'm going to import that hello function that we published as part of the package and then I'm just going to use it here and here it doesn't actually know where the package is we've just installed it into our environment and that's where it lives right now and if you run this um you should be able to see this Hello message that we prepared earlier as part of this package now if this doesn't work for you for any reason Reon just make sure that you are using the python environment that actually has this package so if you type pip list you should see that package show up here and you can also click here as well in vs code to make sure that you really did select that correct environment okay so this is good if you wanted to make a library package but what if you actually wanted to use this in your terminal as a command on its own if you actually want to turn the package into a command line interface or a CLI application you can also do that go back to your setup POI file and specify entry points for your package and you can do it like in this code example here this will let you run a command and it will map it to any function you want inside your package so here I'm going to create a command called pixami Dello um and by the way this name can be anything you want it doesn't have to be related to your package name or your function name at all uh and I'm going to map it to the pixami hello package and the function called hello and again just to repeat myself this works because we've imported that hello function right into the package Nam space here so if you haven't done that you might have trouble finding that function let's go back to our editor and try that out so here I've just added the entry points you might also want to bump the version number so that you know that you've installed the version with this command so I'm going to do that I'm going to bump it to 0.2 and now let's go back to our terminal and build and install the package again now if you did bump the version number make sure you also change the wheel file because the version number is usually baked into the name here so make sure that you're installing the latest version uh that you just published with that installed successfully now let's see if we can actually run this command so here in my terminal I'm just going to type pixami hello and then hit run and now you can see that I've got that output message directly from my terminal so here if you want to create a package that you can run directly from your uh command line This is how you can do it now that your package is ready let's take a look at how we can publish it to pii pii is a python package repository and it's where most of the world's python packages are hosted so when you run pip install this is usually where it gets those packages from first you're going to need to go to p.org and create a pii account so go ahead and do that first if you haven't already once you have that you can run this command to upload anything in your distribution folder directly to pii uh and when you run this it's going to ask for your credentials which you can enter into the terminal directly or you can also provide it as an environment variable this is useful if you want to automate it fully for example run it as part of a cicd workflow you can set it like this um the environment variables are called twine username and Twine password and this works with both API tokens or with username password combinations once you've run the command to publish the package you can click on this link to see it and now you can see it's on the pii repository and here's the command we can use to install it and now anyone all over the world will be able to use this command to install your python package now this description section is a little bit light um so I'm going to show you a trick I like to use to turn the Project's read me document into this description content over here back in my project I actually already have a read me file uh that contains all the information about this project and if you already have that information it really doesn't make sense to copy all of that for the PPI project description when you can just reuse it so let's see how we can do that back in your setup file let's first import all the contents of that readme file I'm storing all the text content of this readme file in this description variable then inside your setup function we're going to set this long description parameter to whatever we read from the readme file we're also going to set the long description type to markdown so it knows how to render this properly with that change let's Now bump our version number up by one and now we can build and publish our package again and now if you go to our latest version you can see that the project description has been updated with the readme file and now that our package has been successfully published to pip let's see if we can actually install it from a fresh environment um using the PIP command directly without referencing our local distribution file so let's copy this pip install command over here and here I've opened up my terminal to a new python environment that doesn't currently have this module so I'm going to run this pip install command um and let's see if our Command actually works okay and that seems to work really well so now you can see how we create a python package publish it to piie and now us or anyone else can install it using pip and then use it directly in our terminal now I probably should mention that if you just want to test your uploads or experiment with this then pii actually has a test end point that you should use I actually recommend doing this instead of uploading to the official public repository because you don't want to start polluting the public repository with experimental projects to do that you'll have to follow these instructions to create a test user create a test API token and then make a couple of changes to your distribution command uh I'll put a link to this page in the project description so you can check it out if you want to try this that's going to be all for this tutorial today check out the description below for the code or for other useful links if you've enjoyed this video and you want to see more stuff like this um then let me know in the comments below what type of projects or tutorials you'd want to see covered next otherwise I hope you found this useful and thank you for watching

Original Description

Learn how to publish your own custom Python package to PyPI. This tutorial covers setting up the project, configuring the setup.py file, building and testing the package, adding CLI functionality, publishing to PyPI, and installing using pip. Watch this video if you want to share your code and make it easily installable for others. 📚 Chapters 00:00 Introduction 00:34 Project Setup 02:06 Configure and Build Package 04:14 Local Testing 05:53 Adding a CLI Entry Point 07:36 Publish to PyPI 10:01 Wrapping Up #pixegami #python
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from pixegami · pixegami · 55 of 60

1 How to Build an AWS Lambda Function in Python in Just 7 Minutes!
How to Build an AWS Lambda Function in Python in Just 7 Minutes!
pixegami
2 AWS CDK Tutorial: Deploy a Python Lambda Function using AWS
AWS CDK Tutorial: Deploy a Python Lambda Function using AWS
pixegami
3 I used GPT-3 to Write Poetry • Is AI the Future of Creative Writing?
I used GPT-3 to Write Poetry • Is AI the Future of Creative Writing?
pixegami
4 Create NFT Generative Art with Python! (Full Tutorial)
Create NFT Generative Art with Python! (Full Tutorial)
pixegami
5 Build an AI-driven SaaS Application: FULLSTACK Tutorial with Python, React, and AWS
Build an AI-driven SaaS Application: FULLSTACK Tutorial with Python, React, and AWS
pixegami
6 NextJS and TailwindCSS: How to Build a Portfolio Site from Scratch
NextJS and TailwindCSS: How to Build a Portfolio Site from Scratch
pixegami
7 Python Web Scraping Tutorial • Step by Step Beginner's Guide
Python Web Scraping Tutorial • Step by Step Beginner's Guide
pixegami
8 Build Wordle in Python • Word Game Python Project for Beginners
Build Wordle in Python • Word Game Python Project for Beginners
pixegami
9 How to create 1000+ unique NFT-style images (like Cryptopunk) | Python Tutorial
How to create 1000+ unique NFT-style images (like Cryptopunk) | Python Tutorial
pixegami
10 Top 10 Python Modules 2022
Top 10 Python Modules 2022
pixegami
11 How to Send SMS Text Messages with Python & Twilio - Quick and Simple!
How to Send SMS Text Messages with Python & Twilio - Quick and Simple!
pixegami
12 How To Write Unit Tests in Python • Pytest Tutorial
How To Write Unit Tests in Python • Pytest Tutorial
pixegami
13 How to Style Your React Landing Page with Tailwind CSS
How to Style Your React Landing Page with Tailwind CSS
pixegami
14 FastAPI Python Tutorial - Learn How to Build a REST API
FastAPI Python Tutorial - Learn How to Build a REST API
pixegami
15 How to Deploy FastAPI on AWS EC2: Quick and Easy Steps!
How to Deploy FastAPI on AWS EC2: Quick and Easy Steps!
pixegami
16 PyScript • How to run Python in a browser
PyScript • How to run Python in a browser
pixegami
17 My Custom Ubuntu Linux Terminal with Themes and Plug-ins 💻
My Custom Ubuntu Linux Terminal with Themes and Plug-ins 💻
pixegami
18 Deploy FastAPI on AWS Lambda ⚡ Serverless hosting!
Deploy FastAPI on AWS Lambda ⚡ Serverless hosting!
pixegami
19 NextJS Firebase Auth Tutorial • How to Authenticate Users for Your App
NextJS Firebase Auth Tutorial • How to Authenticate Users for Your App
pixegami
20 AWS Lambda Python functions with a database (DynamoDB)
AWS Lambda Python functions with a database (DynamoDB)
pixegami
21 How To Build a CRUD (TO-DO) App on AWS using FastAPI and Python
How To Build a CRUD (TO-DO) App on AWS using FastAPI and Python
pixegami
22 How to Make a Discord Bot with Python
How to Make a Discord Bot with Python
pixegami
23 How To Use GitHub Copilot (with Python Examples)
How To Use GitHub Copilot (with Python Examples)
pixegami
24 PyTest • REST API Integration Testing with Python
PyTest • REST API Integration Testing with Python
pixegami
25 Python Beginner Project: Build a Caesar Cipher Encryption App
Python Beginner Project: Build a Caesar Cipher Encryption App
pixegami
26 Decorators in Python: How to Write Your Own Custom Decorators
Decorators in Python: How to Write Your Own Custom Decorators
pixegami
27 NextJS 13 Tutorial: Create a Static Blog from Markdown Files
NextJS 13 Tutorial: Create a Static Blog from Markdown Files
pixegami
28 Exploring ChatGPT for Coding and Business ✨ 8 Real Examples!
Exploring ChatGPT for Coding and Business ✨ 8 Real Examples!
pixegami
29 How I Would Learn Python (if I had to start over) • A Roadmap for 2023
How I Would Learn Python (if I had to start over) • A Roadmap for 2023
pixegami
30 Build an AI Pokemon Generator with Python and Midjourney
Build an AI Pokemon Generator with Python and Midjourney
pixegami
31 Why You Should Learn Python in 2023 (as your first programming language)
Why You Should Learn Python in 2023 (as your first programming language)
pixegami
32 ChatGPI API in Python ✨ How to Build a Custom AI Chat App
ChatGPI API in Python ✨ How to Build a Custom AI Chat App
pixegami
33 Learn Python • #1 Installation and Setup • Get Started With Python!
Learn Python • #1 Installation and Setup • Get Started With Python!
pixegami
34 Learn Python • #2 Variables and Data Types • Python's Building Blocks
Learn Python • #2 Variables and Data Types • Python's Building Blocks
pixegami
35 Learn Python • #3 Operators • Add, Subtract and More...
Learn Python • #3 Operators • Add, Subtract and More...
pixegami
36 Learn Python • #4 Conditions • If / Else Statements
Learn Python • #4 Conditions • If / Else Statements
pixegami
37 Learn Python • #5 Lists • Storing Collections of Data
Learn Python • #5 Lists • Storing Collections of Data
pixegami
38 Learn Python • #6 Loops • How to Repeat Code Execution
Learn Python • #6 Loops • How to Repeat Code Execution
pixegami
39 Learn Python • #7 Dictionaries • The Most Useful Data Structure?
Learn Python • #7 Dictionaries • The Most Useful Data Structure?
pixegami
40 Learn Python • #8 Tuples and Sets • More Ways To Store Data!
Learn Python • #8 Tuples and Sets • More Ways To Store Data!
pixegami
41 Learn Python • #9 Functions • Python's Most Important Concept?
Learn Python • #9 Functions • Python's Most Important Concept?
pixegami
42 Learn Python • #10 User Input • 4 Ways To Get Input From Your User
Learn Python • #10 User Input • 4 Ways To Get Input From Your User
pixegami
43 Learn Python • #11 Classes • Create and Use Classes in Python
Learn Python • #11 Classes • Create and Use Classes in Python
pixegami
44 Learn Python • #12 Final Project • Build an Expense Tracking App!
Learn Python • #12 Final Project • Build an Expense Tracking App!
pixegami
45 Stripe & Firebase Tutorial • Add Payments To Your NextJS App
Stripe & Firebase Tutorial • Add Payments To Your NextJS App
pixegami
46 How To Use GitHub Actions • Automate Your AWS Deployments
How To Use GitHub Actions • Automate Your AWS Deployments
pixegami
47 How to Run a Python Docker Image on AWS Lambda
How to Run a Python Docker Image on AWS Lambda
pixegami
48 My MacOS Terminal Setup for HIGH Productivity
My MacOS Terminal Setup for HIGH Productivity
pixegami
49 Host a Python Discord Bot on AWS Lambda (Free and Easy)
Host a Python Discord Bot on AWS Lambda (Free and Easy)
pixegami
50 Python FastAPI Tutorial: Build a REST API in 15 Minutes
Python FastAPI Tutorial: Build a REST API in 15 Minutes
pixegami
51 Pydantic Tutorial • Solving Python's Biggest Problem
Pydantic Tutorial • Solving Python's Biggest Problem
pixegami
52 How to Get Started with AWS • Crash Course
How to Get Started with AWS • Crash Course
pixegami
53 Python Requests Tutorial: HTTP Requests and Web Scraping
Python Requests Tutorial: HTTP Requests and Web Scraping
pixegami
54 Amazon Bedrock Tutorial: Generative AI on AWS
Amazon Bedrock Tutorial: Generative AI on AWS
pixegami
How to Publish a Python Package to PyPI (pip)
How to Publish a Python Package to PyPI (pip)
pixegami
56 Langchain: The BEST Library For Building AI Apps In Python?
Langchain: The BEST Library For Building AI Apps In Python?
pixegami
57 RAG + Langchain Python Project: Easy AI/Chat For Your Docs
RAG + Langchain Python Project: Easy AI/Chat For Your Docs
pixegami
58 Python Dataclasses: Here's 7 Ways It Will Improve Your Code
Python Dataclasses: Here's 7 Ways It Will Improve Your Code
pixegami
59 Build a Custom AI RPG Game with OpenAI GPTs
Build a Custom AI RPG Game with OpenAI GPTs
pixegami
60 Create a Custom AI Assistant + API in 10 Mins
Create a Custom AI Assistant + API in 10 Mins
pixegami

This video teaches how to publish a Python package to PyPI, covering project setup, configuration, building, testing, and publishing, and is essential for developers who want to share their code and make it easily installable. By following the steps outlined in the video, developers can create and publish their own custom Python packages. The video provides a comprehensive guide to package management and deployment.

Key Takeaways
  1. Run pip command to install setup tools, wheel, and Twine
  2. Create project structure with root folder, package folder, setup.py file, and init.py file
  3. Configure setup.py file with package name, version number, and dependencies
  4. Build package with setup.py file and distribute it as a source distribution and a wheel distribution
  5. Add entry points to the setup.py file for CLI applications
  6. Bump version number and rebuild package
  7. Publish package to PyPI using twine and pip
  8. Use read me file as project description on PyPI
💡 Using PyPI's test endpoint for experimental projects allows developers to test their packages before publishing them to the live repository.

Related AI Lessons

Chapters (7)

Introduction
0:34 Project Setup
2:06 Configure and Build Package
4:14 Local Testing
5:53 Adding a CLI Entry Point
7:36 Publish to PyPI
10:01 Wrapping Up
Up next
AI in Care - Katie Furey, Pairly.com
The Access Group
Watch →