Best Practices of Publishing a Python Module
If you're seeking to make a contribution to the Python package in the form of a module, make sure you have everything you need.
Join the DZone community and get the full member experience.Join For Free
In this article, I will go through the best practices that a Python aspirant should adhere to when making a contribution to the Python package index.
To begin with, if you want to publish a module in the Python community there is some standardization that you would need to follow:
- Your project should be packaged inside one single directory.
- There should be a setup.py in your project structure. setup.py allows you to specify the project configuration and to run packaging commands.
It is very important to know that there are following parameters that should be taken care of when setting up your module configuration
- The classifiers array that contains information like the "Intended Audience," "Programming Language," and "License" of your module.
install_requiresarray that contains all the modules required.
packagesarray contains all the packages that are present within the module.
download_urlwhich can be used to download the package.
console_scriptsif any, to run any particular script on the console for your module.
nameof your module.
authorof your module.
versionof your module.
P.S: Make sure the long description has been configured as well so that you have the module description in PyPi as well. For that, you need to have a README file in your repository. Follow the code block to upload the README to PyPi.
with open('README.md') as f: long_description = f.read()
Now, after that pass these two parameters inside the tuple
long_description = long_description, long_description_content_type = 'text/markdown'
You should be well versed with version control system and creating release tags once you think you are prepared to deploy your module.
Creating Release Tags
1. To create a release tag you should be having the latest code in your release branch. Run the following code to create a release tag run the following code.
git tag <release_version>
2. Once your release tag has been created run the following code to push the release tag on GitHub or any other source repository using this code block.
git push origin <release_version>
3. Be sure to document your release notes to give the user a clear view of what has been deployed in the latest module version.
1. First, create a source distribution. The kind of distribution also known as "Package" requires a build step when installed by pip.
python setup.py sdist
But we would want to install a wheel (a built package) which is faster to install than a source distribution.
python setup.py bdist_wheel
Uploading a Package to PyPi
To upload a module to PyPi follow these steps:
First of all, you need to have a module installed called twine. This helps you upload your binary distribution on PyPi with the latest release tag.
To upload your module to PyPi as a Beta version using twine please follow this code snippet:
# Release version to TestPyPi twine upload --repository-url https://test.pypi.org/legacy/ dist/*\r
To upload your module as a major release version to PyPi please follow this code snippet
# Release version to PyPi twine upload dist/*\r
Opinions expressed by DZone contributors are their own.