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