Modern Python Projects Transcripts
Chapter: Let's build a package
Lecture: Publishing on PyPI

Login or purchase this course to watch this video and the rest of the course contents.
0:01 I think we have most of the stuff ready to publish our package on pypi. Let's quickly check the file.
0:10 I will leave the version 0.1 because this project is not yet polished. Some parts of the documentation are still missing, but it's good enough to use.
0:19 And we have the quick start guide explaining how to use it. So, we should be good. What else do we have here? We have the entry points set up.
0:29 We have the classifiers, If your project is actually more mature, you might want to change the status.
0:36 You can search for pypi classifiers and you will see a list of available different statuses. We also specify which versions of Python we support.
0:46 We could drop 3.5 and replace it with 3.9, but that's not really necessary. So let's keep that, we have the requirements here.
0:57 Yeah, I think we're ready to go. So let's search for the instructions on how to publish this project. If you Google for Python package guide,
1:06 you will find this page and We are actually interested in this website and here is the whole guide on how to
1:13 publish packages on pypi. So we are quickly going through all the steps here
1:17 If you more or less know how publishing packages already works and you don't want to go through all the steps, there is a much faster way.
1:27 If you go to the make file, you will see that there is a release command that also calls dist.
1:32 So those two commands will basically prepare your project for distribution and publish it on pypi
1:38 So we could basically call make release in the terminal and we would have everything done for us with one single command. But since this is a tutorial,
1:48 let me guide you through this process. But we have a simple project. We have the package files. We have setup the file
1:57 Or you can go here to find the list of classifiers. We have to read me. We have the license. We probably already have setup tools and will installed.
2:13 But just to be sure, let's rerun those commands. Yeah, we're good. So, now we have to run this command not pytest
2:30 stupid memory, around folder. So what just happened is that Python has created a distribution files for our project. This has created two files.
2:48 whl, .tar.gz, whl is more or less a binary file. So whenever Python can install your package from a whl is going to use it.
2:57 That's why your package is packed together with all the dependencies. If you don't provide a whl or for some reason Python can't use the whl,
3:06 it will try to build your project from sources from the .tar.gz file. But here Python will actually try to compile your project, so you might need
3:16 to have some dependencies installed and in the next step will use a package called twine to upload our project to pypi, Once again,
3:28 I think we have twine installed, but just to be sure, but not like that. Yeah, we're good. So, now we run this command oops,not like that,
3:52 Python -m cause we want to call a module. So one thing to notice is that we are using a test pypi server here not the real pypi.
4:02 This is perfect. If you want to test publishing Python packages. That way you won't be cluttering the real pypi with some dummy package, later on
4:11 you can simply remove this repository parameter, and you will publish to the real pypi and voila! We can see our package here, and now
4:27 I am a proud parent of a little uptimer package on a testpypi Server. I can copy this instruction to install it, so let's see if this will work.
4:37 Let's go back to the terminal and let's create a new virtual environment to make sure that we're not using the local uptimer package.
4:46 So let's go up one folder. Let's deactivate the previous environment, and lets create a new one.As you can see,
5:13 I'm inside a new virtual environment and I don't have a single package installed. If I run uptimer, I should get an error,
5:21 and I do. And when we run this, we get an error pip complaints that it can't find the request matching version 2.25
5:33 which is kind of weird, because when we're testing this package locally, there was no problem with requests.
5:38 Well, the problem here is that we told pip to use the test pypi server and not all packages are published on testpypi Server.
5:48 As you can see, colorama was there. But if we try to search for requests,
5:54 you can see that the last version that was published here was 2.5 and that was in 2015. So to fix that,
6:02 we have to tell pip to also look for dependencies on the standard pypi server So instead of telling pip to, install our up timer from the test pypi,
6:22 we will tell it to use the standard by pypi server and if it can't find a package there to use this extra index URL of testpypi.
6:33 So we've got all our dependencies should be installed from the standard pypi and our
6:37 uptimer can't be found on pypi because we haven't published it yet there. So for that we will instead of use test.pypi and now everything is fine
6:50 Here we have our uptimer in version 0.1 installed, so let's test it, without parameters. We got the error message saying that we have to
7:05 specify some URL's, which is good. So it means something is working. Perfect. So we have just successfully installed uptimer from the testpypi server
7:19 Since everything works fine, let's now publish it on the standard by pypi Server Once we test our package with the test pypi Server and we know that
7:30 everything is fine and we can install it, we can now go and upload it to the realpypi Server.
7:37 So I'm back to the folder with our uptimer and instead of running twine, upload to the test pypi repository. I'm going to remove this parameter,
7:48 and this will upload our package to the normal pypi server, again I have to provide the user name and password, and if I goto pypi,
8:03 you can see that our package is there. Let's try to install it and it's working.
8:44 Great. So we have successfully published our simple package on pypi and now everyone can use it. If you're following this tutorial,
8:53 you can actually run pip install uptimer in your terminal and then you can give it a try.

Talk Python's Mastodon Michael Kennedy's Mastodon