Hi everyone👋 , In this article, we will discuss how to Publish a Python package at PyPi using Twine Module. You can refer to my YouTube video to see a working tutorial for better understanding and a step-by-step guide of the same.
Python is so flexible and easy to use because of its available packages that are hosted on pypi.org, Let’s see how to publish your own package on PyPi using the Twine module.
Requirements :
- You must have an account of pypi.org.
- The twine library is created to simplify uploading packages in PyPI. To install twine library.
pip install twine
- Get your package ready.
Steps to publish package :
Step 1 : Prepare and organize the package
- Create a folder name must be same as package name. (Ensure that the name of your package does not pre-exist in PyPi)
- Inside this create another folder with same name or same package name, inside that put all the .py files with a single compulsorily init.py file and all folders/programs must be imported in init.py .
- Now outside of programs folder and inside the root folder,Setup.py file, README.md file and Liscense.txt file must be created. Below is the flow diagram of the hierarchy :
Step 2 : Creating README.md file
The .md files are mark down files just as markup languages, it has it’s own syntax,it is used as a User-Friendly Readme file which will be displayed on home page.
You may use extensions for VS Code or any online editors like dillinger.io to create Readme.md
Step 3 : Creating License.txt file
Your package must have license, If you don’t have any concern to sharing it all over world you may use License.txt. If your package has policies you may use any online license maker website like choosealicense.com.
Step 4 : Create Setup.py file
Your package must have a Setup.py file as it’s one of binding component of package, It describes dependencies of the package and author version, etc.
Below is the code for general Setup.py . One needs to use their own credentials.
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
# Here is the module name.
name="Enter_Module_name_Here",
# version of the module
version="0.0.1",
# Name of Author
author="Your_Name",
# your Email address
author_email="Youremail@gmail.com",
# #Small Description about module
# description="adding number",
# long_description=long_description,
# Specifying that we are using markdown file for description
long_description=long_description,
long_description_content_type="text/markdown",
# Any link to reach this module, ***if*** you have any webpage or github profile
# url="https://github.com/username/",
packages=setuptools.find_packages(),
# if module has dependecies i.e. if your package rely on other package at pypi.org
# then you must add there, in order to download every requirement of package
# install_requires=[
# "package1",
# "package2",
# ],
license="MIT",
# classifiers like program is suitable for python3, just leave as it is.
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
)
Step 5 : Final Stage
Here you have to create account on pypi.org also we will be using twine library. The whole package is uploaded in the form of .dist file.
To create .dist file use the command:
# Windows
python setup.py bdist_wheel
or
# Linux/Mac
python3 setup.py bdist_wheel
You will observe this message on Powershell when the above commands are executed.
Now our binary .dist file is created, now we need to upload it using the below command:
twine upload dist/*
Enter the required credentials regarding the pypi.org account and the package will be uploaded, After successful upload, you will get a link to your project and all the details.
Alright, guys! I hope this article was helpful for you if it is the leave your comments below. I will meet you in another article until then KEEP CODING🤗.