Python Packages: How to Reuse your Code?

Reuse your Code with Python Package

Reusing the code is an amazing aspect of any Python development project. It allows you to reduce the development time for the next project. You can also save a huge amount of effort and cost during developing a web application. Reusing the code results in a maintainable system. Python developers can easily understand how the code functions and achieve stability in the whole code.

Reuse your Code with Python Package

You can follow these steps to reuse your code in Python by creating a package and redistributing it.

To create this project locally, create the following file structure:

Package Folder

You will now create a handful of files to package up this project and prepare it for distribution. Create the new files listed below and place them in the project’s root directory — you will add content to them in the following steps.

  • Package folder contains
  • LICENSE
  • README.md
  • setup.py
  • tests
  • mypackage
  • __init__.py
  • main.py
  • LICENSE

It’s important for every package uploaded to the Python Package Index to include a license. You can also create a handful LICENSE using github.

Code with Python Package
Code with Python Package
  • README.md

You can generate your README file similar to a normal text file. You may read more details here:

https://guides.github.com/features/mastering-markdown/

  • Setup.py

setup.py is the build script for setuptools. It is a collection of enhancements to the Python distutils that allow you to more easily build and distribute Python distributions, especially ones that have dependencies on other packages.

Open setup.py and enter the following content.

import setuptools

with open("README.md", "r", encoding="utf-8") as fh:

long_description = fh.read()

setuptools.setup(

name="YOUR PACKAGE NAME", # It should be unique name

version="0.0.1",

author="Name of Author",

author_email="author@email.com",

description="A small title of the package",

long_description=long_description,

long_description_content_type="text/markdown",

url="https://mysite.com",

packages=setuptools.find_packages(),

classifiers=[

"Programming Language :: Python :: 3",

"License :: OSI Approved :: MIT License",

"Operating System :: OS Independent",

],

install_requires=[

"numpy==1.18.1",

"pandas==1.1.5",

],

python_requires='>=3.6',

)

========================================

The next step is to generate distribution packages for the package

========================================

Install latest versions of setuptools and wheel installed

python3 -m pip install –user –upgrade setuptools wheel twine

Run below command from the same directory where setup.py is located

python3 setup.py sdist bdist_wheel

This command should output a lot of text and once completed should generate two files in the dist directory.

Uploading the distribution

  • Create/Register an account
  • TEST ENVIRONMENT: Register an account on https://test.pypi.org/account/register/ and complete the steps on that page. You will also need to verify your email address before you’re able to upload any packages.
  • LIVE ENVIRONMENT: Register an account on https://pypi.org.

Note: These are two separate servers and the login details from the test server are not shared with the main server.

Once registered on account, run Twine to upload all of the archives under distribution.

  • TEST ENVIRONMENT

python3 -m twine upload --repository testpypi dist/*

  • LIVE ENVIRONMENT

python3 -m twine upload dist/*

Checkout Expression Analysis with Python Software Development

Congratulations, you’ve Packaged and Distributed a Python Project!

Packaging and redistributing the code enables developers to efficiently develop web applications. In this article, we looked at how to properly structure a project for the Python package. After that, we looked at the crucial steps to generate distribution packages for the same. We focused on reducing the packaging time and enable faster web development.

At BoTree Technologies, we reduce the Python web development code by reusing code and providing a systematic structure to your entire project. You can get a quick consultation with our Python developers.

Originally Published at https://www.botreetechnologies.com/blog/how-to-reuse-your-code-with-python-package/

Leading software development company in the USA, Singapore & India. Hire developers to build web apps with expert software development services.