[Python] Package and distribute your own modules

what's this

When developing as a team, there are cases where you want other people to use your own tools, or you want to incorporate your own modules into your project. In such a case, "packaging" is convenient.

I'll write later how to secretly host a package in a private repository.

(Addition) I'm sorry this may be

Preparation

Project structure

The following explanation will be written assuming the following structure. Please spit out the requirements relation with pip freeze. The project name is "uhouhoapp"

procedure

example of main.py


def main():
    print('HelloWorld')

Preparation of setup.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from setuptools import setup, find_packages


def _load_requires_from_file(filepath):
    return [pkg_name.rstrip('\r\n') for pkg_name in open(filepath).readlines()]


def _install_requires():
    requires = _load_requires_from_file('requirements.txt')
    return requires


def _test_requires():
    test_requires = _load_requires_from_file('test-requirements.txt')
    return test_requires


def _packages():
    return find_packages(
        exclude=[
            '*.tests',
            '*.tests.*',
            'tests.*',
            'tests',
            '*.yaml'
        ],
    )


if __name__ == '__main__':
    description = ''
    setup(
        name='uhouhoapp',
        version='1.0.0',
        description=description,
        author='UhoUho Inc.',
        author_email='[email protected]',
        classifiers=[
            'Programming Language :: Python :: 2',
            'Programming Language :: Python :: 2.7',
            'Development Status :: 3 - Alpha',
            'License :: Confidencial',
            'Intended Audience :: Developers',
            'Natural Language :: English',
            'Operating System :: POSIX'
        ],
        packages=_packages(),
        install_requires=_install_requires(),
        tests_require=_test_requires(),
        test_suite='nose.collector',
        include_package_data=True,
        zip_safe=False,
        entry_points="""
        [console_scripts]
        uhouhoapp = uhouhoapp.main:main
        """,
    )

When to use

If you hit the following command, my module will be registered.


$ pip install -e .
Obtaining file:///Users/hiroyuki/test22
Installing collected packages: uhouhoapp
  Running setup.py develop for uhouhoapp
Successfully installed uhouhoapp-1.0.0

Run as command

If you want to execute it, do the following. If you're a good commander, you can get rid of the console_scripts part of setup.py.

$ uhouhoapp
HelloWorld

Recommended Posts

[Python] Package and distribute your own modules
How to package and distribute Python scripts
Python packages and modules
Import your own modules in Grasshopper's Python development
Flow of creating your own package with setup.py with python
Understand Python packages and modules
Call your own python module from the ROS package
[Python] Make your own LINE bot
[Python] logging in your own module
Introductory Python Modules and conditional expressions
Python Basic Course (14 Modules and Packages)
[Python] When you want to import and use your own package in the upper directory
Create your own graph structure class and its drawing in python
Specify your own class in class argument and return type annotation in Python
Modules and packages in Python are "namespaces"
Create your own Linux commands in Python
[LLDB] Create your own command in Python
Python installation and package management with pip
[Python] Register your own library on PyPI
Until you install your own Python library
Publish your own Python library with Homebrew
Get your own IP address in Python
Create your own virtual camera with Python + OpenCV and apply original effects
[Python] Implement your own list-like class using collections.UserList
Extend and inflate your own Deep Learning dataset
Make your own module quickly with setuptools (python)
python: Use your own class for numpy ndarray
A script that combines your favorite python modules and binaries into one Lambda Layer
Memo to create your own Box with Pepper's Python
Create your own Big Data in Python for validation
Take your own peak memory usage on Linux & Python
Create your own Random Dot Stereogram (RDS) in Python.
Try to improve your own intro quiz in Python
Let's call your own C ++ library with Python (Preferences)
[Blender × Python] Create your own function & summary so far
Use the CASA Toolkit in your own Python environment
Get your current location and user agent in Python
[Road to intermediate Python] Define in in your own class
python package dependencies and virtual environment management tool Poetry
Automate Chrome with Python and Selenium on your Chromebook
Get an abstract understanding of Python modules and packages
Make Jupyter Notebook your own: Change background and icons
[python] Compress and decompress
List of python modules
Batch design and python
Python iterators and generators
Python package development notes
Python test package memo
Vue-Cli and Python integration
Ruby, Python and map
Python ipaddress package notes
python input and output
Python and Ruby split
Python package manager comparison
Python3, venv and Ansible
Python asyncio and ContextVar
python> Link> PyUserInput package |
Until you can install your own Python library with pip
Try sorting your own objects with priority queue in Python
Run the intellisense of your own python library with VScode.
Have Python act for you, then your eyes and brain ...