Packaging and Publishing Python code?

PythonServer Side ProgrammingProgramming

Python provides a very simple way of creating or publishing packages.

Package management in Python is available through different tools−

  • Pip- It remains one of the preferred choices because it virtually eliminates any manual installs and updates of software packages to operating systems. It manages full lists of packages and their corresponding version numbers, which fosters precise duplication of entire package groups in a distinct, separate environment.

  • Python Package Index (PPI) is a public package repository of user-submitted packages that can be installed using pip .i.e. pip install package_name.

Below is a step-by-step procedure on how to upload the package.

Step 1: Have a package to upload

I’m assuming you have the package ready to be published. If you don’t have, please follow below procedure to create python package or module, good thing is its very easy−

  • Create a python file with your code, call it myfirstPackage.py or myPackageName.py This is a module. a file (myfirstPackage.py) with data in it. We can import it or do whatever we want.

  • Make it a package:
Just add an empty __init__.py file to it.

echo >> __init__.py

or use touch command

touch __init_.py


$dir
Volume in drive C has no label.
Volume Serial Number is 8CD6-8D39

Directory of c:\Python\Python361\firstPackage

08-04-2019 05.44 PM <DIR> .
08-04-2019 05.44 PM <DIR> ..
08-04-2019 02.25 PM 47 myFirstPackage.py
08-04-2019 05.44 PM 13 __init__.py

You can see fro above two files are there inside the firstPackage directory.

That’s it, having a directory containing the two files (__init__.py and myfirstPackage.py) is called a package(myHelloModule).

Packaging your project

First, clone the sample project and give it the name of your module−

git clone https://github.com/pypa/sampleproject firstPackage

The important files are−

  • Setup.py – It allows us to specify our project configuration, and to run packaging commands: for example, try this command: python setup.py --help

  • Setup.cfg is an INI file containing option defaults for setup.py commands

  • README.rst describes the goal of the project, using reStructuredText.

Copy your module(s) inside this new folder and remove the existing “sample” module−

└───firstPackage
│ LICENSE.txt
│ MANIFEST.in
│ myFirstPackage.py
│ README.md
│ setup.cfg
│ setup.py
│ tox.ini
│ __init__.py

Configure the name, version, description

Edit setup.py to contain basic information about your Python package−

setup.py

import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
   name="firstPackage",
   version="0.0.1",
   author="Rajesh Joshi",
   author_email="callraj.joshi@gmail.com",
   description="my First Package",
   long_description=long_description,
   long_description_content_type="text/markdown",
   url="https://github.com/pypa/sampleproject",
   packages=setuptools.find_packages(),
   classifiers=[
      "Programming Language :: Python :: 3",
      "License :: OSI Approved :: MIT License",
      "Operating System :: OS Independent",
   ],
)

And your license file will be something like−

MIT License

Copyright (c) [2019] [firstPackage]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

.For README

## firstPackage
This is a sample package to learn the steps of creating and publishing package.

The actual packaging steps

Install or Update the setuptools and wheel packages in the environment.

>pip install wheel twine setuptools –upgrade

First, create a source distribution. This kind of “distribution” (.i.e. package) requires a build step when installed by pip.

>python setup.py sdist

Now we want to install a “wheel” (a built package) which is fater to install than a source distribution.

>python setup.py bdist_wheel

Hopefully, the package should be built and you can see package’s compressed file in dist directory in your firstpackage folder beside setup.py file.

Uploading your package

Now create a new virtual environment at some other location of your choice and activate it, just like below−

c:\Users\rajesh>virtualenv myPackage
Using base prefix 'c:\\python\\python361'
New python executable in c:\Users\rajesh\myPackage\Scripts\python.exe
Installing setuptools, pip, wheel...done.

c:\Users\rajesh\myPackage>.\Scripts\activate

(myPackage) c:\Users\rajesh\myPackage>

Copy the zip file created above in your new environment.

>pip install firstPackage-0.0.1.tar.gz

To verify your package is installed in your activated environment, just run pip list to show list of all packages in your current environment.

>pip list
Package      Version
------------ -------
firstPackage  0.0.1
pip           19.0.3
setuptools    41.0.0
wheel         0.33.1

Now its time to publish the package to PyPI, so that it is publically available.

First go to the path where setup.py exists and then install or update the twin package.

>pip install --upgrade twine

Finally, publish your package to PyPI through twin,

>twine upload dist/*
Enter your username: callraj.joshi
Enter your password:
Uploading distributions to https://upload.pypi.org/legacy/
Uploading firstPackage-0.0.1-py2.py3-none-any.whl
…

Above we just need to enter username and password, then it will start uploading our package.

raja
Published on 02-May-2019 13:27:02
Advertisements