Testing Your Python Package
Before publishing your newly created Python package to PyPI it’s a good idea to test if everything is setup correctly. In this video you’ll double check whether everything is setup as expected and publish your package to the Test PyPI page.
00:00
Before you attempt an upload, you should make sure that everything you expect to be included in your package is actually there. If you’re on a Linux or Mac, you can run the following command to look inside the tar
file to double-check what’s present. So, change directory to that dist/
folder, and you should—if you list everything in there—see your tar
and your wheel file.
00:21
And you can go ahead and type in tar tzf
and then the name, which in this case, the tar
has the dash (-
), and run that. And if I bring this up, you’ll see—well, the font size is a little hard to read, but everything should be in here. You’ve got your manifest, your README
,
00:43
and then inside reader/
, your __init__.py
, __main__.py
, config.txt
. It looks like everything’s included. If you’re on Windows, you may need to download something like 7-zip to look in these types of files. But then I also have—in my text editor, if I bring this back down, I can actually just click on these and my text editor will open them up like it’s a file explorer.
01:07 So, this is a great quick way to make sure that everything’s laid out rather than looking at terminal output. A good quick check is also to use Twine, which is the tool that you’ll use to actually upload to PyPI.
01:20
If you don’t have it do a pip install
—
01:25
without hitting caps lock—twine
.
01:30
And once you have that, you may be able to run twine check
and then just do—or actually, before we run this, let’s get back out into that main directory so that you can see the actual dist/
folder. And try running check dist/*
, like that. Now, if you get an error after you’ve installed it saying command not found
, that just means that pip
decided to install twine
and not add it to your path variable. If you don’t want to add it to your path variable, you can also just say python -m twine
, and it’s just like the same thing.
02:12
So let’s just do check dist/*
. And of course, it’s not going to find that file because you need an asterisk (*
). Okay, cool. So, you should see that each of the files in there—it passed. This won’t catch everything, but it’ll make sure you’ve got all your formats correct, and that it should render properly on PyPI.
02:36 So now that everything looks good, it’s time to try and upload it to PyPI. But rather than clutter the main PyPI site with test files, you can upload to a test site located at test.pypi.org.
02:49 If you were to look at pypi.org, you’d see this kind of page here, where you can search for projects. But you go to test.pypi.org, you can see that it actually looks pretty much identical.
03:06 The only real difference is you have this banner up here that lets you know that you’re on the test site. So if you don’t have an account here, go ahead and click Register and create your account.
03:16 Make sure you remember your username and password because you’ll need that when you use Twine to upload to here. So let’s go back,
03:26 make sure we’re in the right directory. And actually, because of this, I’m going to open this up a little more.
03:33
You should be able to call, well, in my case, python -m twine
, and then just say upload
. You’re going to add in another argument called --repository-url
, and this is how you define that you want to go to the test site, so just https://test.pypi.org/legacy/
, and then you want to send everything in that dist/
folder, so dist/*
.
04:10 Now it’s going to ask you for your username, so add that, and add in your password.
04:22
And you should see, it’s Uploading distributions
. It looks like everything was completed, so let’s head over to the TestPyPI, go back to the homepage,
04:36
and look at that! Right down here in New releases, there’s the package I just uploaded. We can open this up, if you wanted to install it just like it was, you could go pip install
and use this separate URL.
04:50
So that’s pretty cool. Scrolling down here, this is all the information that was in that setup.py
. Here’s the README
.
05:01 It lists me as a maintainer. It’s got the License, the Author as Real Python, and the classifiers that were included. So, this is pretty cool! And everything looks right, so I think you’re ready in the next video to go ahead and upload your project to the real PyPI. It’s a pretty big step to take and it’s really exciting.
Denis Roy on April 16, 2020
@mounika - It is a field used to describe the license under which you release your code. In the video, they use “MIT”, which is a very permissive open source license using the same name as the university it originated from (MIT = Massachusetts Institute of Technology)
Mohan Kumar Paluru on April 25, 2020
What text editor is that ? Just curious :)
Dan Bader RP Team on April 26, 2020
It’s Visual Studio Code :)
William Halsted on Jan. 3, 2021
I attempted to upload my project to TestPyPi, entering the command:
G:\Coding\PyGame\PyGame Modules\easy_button>python -m twine upload --repository-url https://www.test.pypi.org/legacy dist/*
I entered my username and password, and all went well. However, I got a traceback error, underneath which were several during the handling of the above exception, another exception occurred
. The beginning lines of the errors, which mostly all had to do with connections, were linked to these files:
- “C:\Users\William\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connection.py”
- “C:\Users\William\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connectionpool.py”
- “C:\Users\William\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\adapters.py”
- “C:\Users\William\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\util\retry.py”
Can anybody explain what happened? I am using a Windows computer, and I believe it has an internet-firewall-type thing that blocks Python socket connections to a server, which may be the culprit. I would appreciate any assistance; perhaps I need to update my Python.
historicode97 on Jan. 21, 2022
For the upload command, there is also a way to specify only the repository name :
$ twine upload --repository testpypi dist/*
$ twine upload -r testpypi dist/*
info97 on Nov. 17, 2022
What is the name of the extension to see contents of wheel and tar? Found only a few like “Archive Browser” but they are not doing as yours do…
Geir Arne Hjelle RP Team on Nov. 17, 2022
@info97 the tools are typically included in Linux. On other systems, you may need to install similar tools. Which operating system are you using?
info97 on Nov. 18, 2022
Hi, I am on Mac OS. UNfortunately clicking on a tar in VSCode just brings the message that this is a binary file..
Become a Member to join the conversation.
mounika on March 28, 2020
what is licence in setup.py file?