How to make the Package in Python - Data Driven Investor - Medium 03.03.
2020, 12:09
How to make the Package in
Python
Ankit Kumar Rajpoot Follow
Mar 2 · 5 min read
In this article, we will see how to make packages and public it
on GitHub and PiPy. The packages are collections of modules
and modules are collections of functions and classes. Building
today’s software doesn’t happen without extensive use of
libraries. Libraries dramatically cut down the time and eCort
required for a team to bring work to production. By leveraging
https://medium.com/datadriveninvestor/how-to-make-the-package-in-python-a82292aeb775 Strona 1 z 13
How to make the Package in Python - Data Driven Investor - Medium 03.03.2020, 12:09
the open-source community engineers and scientists can move
their unique contribution towards a larger audience, and
eCectively improve the quality of their code. Companies of all
sizes use these libraries to sit their work on top of existing
functionality, making product development more productive
and focused.
Step 1:- Create Package Framework
Picking A Name
Python module/package names should generally follow the
following constraints:
All lowercase
Unique on PyPI, even if you don’t want to make your
package publicly available (you might want to specify it
privately as dependency later)
Underscore-separated or no word separators at all (don’t
use hyphens)
We’ve decided to turn our bit of code into a module called
ankitpackage .
The initial directory structure for ankitpackage should look like
this:
https://medium.com/datadriveninvestor/how-to-make-the-package-in-python-a82292aeb775 Strona 2 z 13
How to make the Package in Python - Data Driven Investor - Medium 03.03.2020, 12:09
ankitpackage
├── ankitpackage
└── __init__.py
├── setup.py
The top-level directory is the root of our repo, e.g.
ankitpackage.git . The subdirectory, also called, is the actual
Python module.
Step 2:- Fill Out Description Details
The main setup conOg Ole, setup.py , should contain a single
call to setuptools.setup() , like so:
from setuptools import setup
setup(name='ankitpackage',
version='0.1',
description='First python package',
url='http://github.com/storborg/funniest',
author='Ankit',
author_email='[email protected]',
license='MIT',
packages=['ankitpackage'],
zip_safe=False)
STEP 3: Adding Additional Files
Additional Oles should always be added inside the inner
ankitpackage directory.
For example, let’s move our one function to a new
utilities.py submodule, so our directory hierarchy looks like
https://medium.com/datadriveninvestor/how-to-make-the-package-in-python-a82292aeb775 Strona 3 z 13
How to make the Package in Python - Data Driven Investor - Medium 03.03.2020, 12:09
this:
ankitpackage
├── ankitpackage
└── __init__.py
└── utilities.py
├── setup.py
In utilities.py :
def joke():
return ('That is my first package!')
. . .
Import in local
Now we can install the package locally (for use on our system),
by running the following command:
$ pip3 install .
https://medium.com/datadriveninvestor/how-to-make-the-package-in-python-a82292aeb775 Strona 4 z 13
How to make the Package in Python - Data Driven Investor - Medium 03.03.2020, 12:09
Now after installing, we can use it anywhere in our system.
>>> from ankitpackage import utilities
>>> print (utilities.joke())
. . .
Publishing On GitHub
GitHub
GitHub, Inc. is a US-based global company that provides
hosting for software development version control using Git.
https://medium.com/datadriveninvestor/how-to-make-the-package-in-python-a82292aeb775 Strona 5 z 13
How to make the Package in Python - Data Driven Investor - Medium 03.03.2020, 12:09
Best Coding Languages to Learn in 2019 | Data
Driven Investor
During my years as an undergrad, I skipped many night-
outs to pick up Java hoping it would one day help me get…
ahead in…
www.datadriveninvestor.com
Ignoring Files (.gitignore, etc)
.gitignore 1le is a text 1le that tells Git which 1les or folders to
ignore in a project. A local .gitignore 1le is usually placed in
the root directory of a project. You can also create a global
.gitignore 1le and any entries in that 1le will be ignored in all
of your Git repositories. So our directory hierarchy looks like
this:
ankitpackage
├── ankitpackage
└── __init__.py
└── utilities.py
├── setup.py
├── .gitignore
In .gitignore :
# Compiled python modules.
*.pyc
# Setuptools distribution folder.
/dist/
https://medium.com/datadriveninvestor/how-to-make-the-package-in-python-a82292aeb775 Strona 6 z 13
How to make the Package in Python - Data Driven Investor - Medium 03.03.2020, 12:09
# Python egg metadata, regenerated from source files by
setuptools.
/*.egg-info
Now, push your package on GitHub running by the following
commands.
$ git init
$ git add .
$ git commit -m "first commit"
$ git remote add origin
https://github.com/rajputankit22/ankitpackage.git
git push -u origin master
Installing our Python Library from GitHub
With our library hosted on GitHub, we simply use pip3 install
git+ followed by the URL provided on our GitHub repo
(available by clicking the Clone or Download button on the
GitHub website):
https://medium.com/datadriveninvestor/how-to-make-the-package-in-python-a82292aeb775 Strona 7 z 13
How to make the Package in Python - Data Driven Investor - Medium 03.03.2020, 12:09
. . .
Publishing On PiPy
The setup.py script is also our main entrypoint to register the
package name on PyPI and upload source distributions.
To register the package (this will reserve the name, upload
package metadata, and create the pypi.python.org webpage):
https://medium.com/datadriveninvestor/how-to-make-the-package-in-python-a82292aeb775 Strona 8 z 13
How to make the Package in Python - Data Driven Investor - Medium 03.03.2020, 12:09
$ python3 setup.py register
If you haven’t published things on PyPI before, you’ll need to
create an account by following the steps provided at this point.
First create a source distribution with:
$ python3 setup.py sdist
https://medium.com/datadriveninvestor/how-to-make-the-package-in-python-a82292aeb775 Strona 9 z 13
How to make the Package in Python - Data Driven Investor - Medium 03.03.2020, 12:09
This will create dist/ankitpackage-0.1.tar.gz inside our top-
level directory. If you like, copy that Ole to another host and try
unpacking it and install it, just to verify that it works for you.
That Ole can then be uploaded to PyPI with:
$ python3 setup.py sdist upload
https://medium.com/datadriveninvestor/how-to-make-the-package-in-python-a82292aeb775 Strona 10 z 13
How to make the Package in Python - Data Driven Investor - Medium 03.03.2020, 12:09
Installing the Package from PiPy
At this point, other consumers of this package can install the
package with pip :
$ pip3 install ankitpackage
This article describes how to create packages in python and
how to pushes on GitHub and pipy for other developers and our
colleagues.
Feel free to ask any questions or queries in the comment
section or you can ping me on Facebook.
https://medium.com/datadriveninvestor/how-to-make-the-package-in-python-a82292aeb775 Strona 11 z 13
How to make the Package in Python - Data Driven Investor - Medium 03.03.2020, 12:09
Gain Access to Expert Views
Email
Email
Data Driven
Investor
from confusion
First to
Name
First Name
clarity, not insanity
Follow Give me access!
I agree to leave Medium.com and submit this information, which will be
collected and used according to Upscribe's privacy policy.
3537 signups
Python Package Modules Pip Github
Discover Medium Make Medium Explore your
Welcome to a place where
yours membership
words matter. On Medium, Follow all the topics you Thank you for being a
smart voices and original care about, and we’ll member of Medium. You
ideas take center stage - deliver the best stories for get unlimited access to
with no ads in sight. Watch you to your homepage and insightful stories from
inbox. Explore amazing thinkers and
storytellers. Browse
About Help Legal
https://medium.com/datadriveninvestor/how-to-make-the-package-in-python-a82292aeb775 Strona 12 z 13
How to make the Package in Python - Data Driven Investor - Medium 03.03.2020, 12:09
https://medium.com/datadriveninvestor/how-to-make-the-package-in-python-a82292aeb775 Strona 13 z 13