Python is a cool language, we can do near to everything, whether to run a Hello World to developing a tool. Here we'll see how to create your own sample python package in easy steps.
1) Package Structure (Very Important )
your_directory/
sub_directory/
__init__.py
setup.py
e.g.,
sample/
sample/
__init__.py
setup.py
2) Create __init__.py file
We need __init__.py file, to initialize the python package and to differentiate from other files.
This file contains only the function to be executed. e.g,
def hello( ):
return (u'Hello Evil Wolrd')
3) Create setup.py file
This is very important file , it contains a global setup( ) function. It gives specific details of your package.
Create a setup.py file in the main directory and outside sub-directory and write the following below
from setuptools import setup
setup(name='Sample',
version='0.1',
description='Sample Demo',
url='http://maddog.com',
author='HocusPocus',
author_email='Hocus@example.com',
license='DEMO',
packages=['Sample'],
zip_safe=False)
4) pip install < . >
This will install your package on your local system. (This can only be used locally. )
# pip install .
Remember there is ( . ) at the end of the command.
5) Use twine, for your package.
# pip install twine
Now to make your package available globally, register your package into PYPI. Go to the directory containing setup.py file and sub-directory and run the following :
# python setup.py sdist
This will generate a zip file of your package as source distributor.
# twine upload dist/*
This will upload your package to PYIP.
Ihis command will ask for user name and passwd. Give a username and passwd, and it will upload your package to PYPI.
Check: https://pypi.org/project/<your_package_name>
Whether your package exists or not.
This will install your package from anywhere to any machine, as long it has python installed.
# pip install <your_pacakge_name>
There you have it, your python package.
No comments:
Post a Comment