Once you cover the necessary ground for packaging whether it be coding, licensing, testing or documentation, it’s finally the moment of truth: Uploading to PyPI!
From this point on, you should be executing commands inside the root folder of your library. This is where installation files such as: setup.py, requirements.txt and readme.md exist.
There are two ways to ensure you’re running Python in a specific directory that comes to mind:
- Before starting Python: Navigate to the folder from Command Prompt (Anaconda Command Prompt or Anaconda Shell), then launch Python there.
- To do this, use cd command. For example:
- if you’re in C:/ and you want to navigate to C:/New_Folder, all you have to do is execute cd New_Folder
- Then, if you execute cd .. this will take you to the directory one level up, if you are in C:/New_Folder and execute cd .. it will take you back to C:/
- From inside Python:
- import os
You can check your current working directory from inside Python with this code:
import os dirpath=os.getcwd() print(dirpath)
Provided by HolyPython.com
Creating Source Distribution
If you’ve prepared all the packaging files, did some local testing and installation you are ready for the real deal: uploading your package to PyPi Python Repository.
After your package is successfully up, your package will be available for installation globally.
python setup.py sdist
Checking your distribution
It’s always a good idea to check the source distribution before uploading it to a repository.
This way if there is an error you will catch it before hand and avoid potential failures.
twine already has a fantastic command that takes care of this:
twine check dist/*
twine check is usually pretty good at pointing out the exact error when there is one. So, if there is an error, it will be raised to your attention, otherwise it will say something like check successfully completed 100%. Then, you’re good to go.
It’s the moment of truth, your precious package is ready to be uploaded and take its place in the wild. All you have to do is execute the command below:
twine upload dist/*
Then, you will be asked for your pypi username and password. Just enter them in order. While entering password it will seem like you’re not typing, if you’re not familiar with this it might feel weird, just type as usual and it will actually be registering key presses. Then press enter and ta-da!.
Your package is up congratulations.
If something goes wrong don’t be discouraged. First times are always a bit messy. Just refer to the tutorials about each step and once you address the problem everything should work fine.
We tried to include all the pitfalls and common errors in each step in this tutorial series. So, you should be covered if you still get a really unexpected error then chances are high that it also happened to someone else and there is a thread about it on Stack Overflow. If there isn’t it’s a good idea to just start it which will help the next guy encountering the same problem after you.
If you’re certain that you’ll upload your project but you just need a little more time, you might want to register your project name with PyPI in the meanwhile.
you need an extra file to do this:
servers = pypi username:[Enter your PyPI username here] password:[Enter your PyPI password here]
python setup.py register
Now, you can take your time without worrying about the project name getting taken.
Don’t drag it forever though, some things are meant to be fixed and improved during the different development stages of the code when the program is in alpha version.
Congratulations, now you don’t only know Python and programming but you’re also figuring out how to create software and upload it to a repository, in this case PyPI which is a fantastic step for coding work in Python.
Wishing everyone a productive, creative, innovative and meaningful coding journey.
If you need some inspiration check out some of the topics we have on our blog.
Please share this Python packaging tutorial if you found it helpful.