Make your own module quickly with setuptools (python)

Overview

You may be developing something yourself and modularizing your classes and functions.

By writing and building setup.py using setuptools,

test.py


import mylib

You will be able to do something like this, but where you want to modularize I would like to summarize the minimum necessary items as a memorandum. If you prepare this, you can modularize it very quickly.

I will write only the minimum necessary, so the details are Official Document Please refer to.

Commentary

The explanation is as follows.

--Prepare folders and files. --Write a little library contents. --Write a little __init__.py. --Write a little setup.py. --Command execution of python setup.py --Optional (when a library depends on another library)

What to prepare

That's all you need.

--Folder to put the contents of the library --Code that defines some function or class (.py)

This time, assuming that we will create a module called mylib, we prepared as follows.

tree .
setup.py
mylib
|-- __init__.py
|-- mylib.py

Write the function you want to make into a library.

Then, I wrote the function appropriately in mylib.py which is the contents of the library as follows.

mylib.py



class Test():
    def __init__(self):
        pass

    def pt():
        print("from class Test")


def test():
    print('hello world')

Prepare __init__.py.

――What file to import --Which function to import

Just write a few lines about.

__init__.py


from .mylib import (
    test,
    Test,
)

__version__ = '0.0.0'

in this case, From the file mylib.py, with the function test I'm just writing a class called Test in this library.

Write a little setup.py.

setup.py


from setuptools import setup, Extension,find_packages

setup(
    name = 'mylib',
    packages=find_packages(where='mylib'),
    package_dir={'': 'mylib'},
)

This is ok.

Run setup.py.

The python library is as ashared object file (so file) It is stored in the python environment folder. The location is https://qiita.com/kenmaro/items/d589d32115154dcd26b2 Read this article please refer to.

If you put it in this folder, no matter where you run python import mylib

Can be. When you want to do that

python setup.py install

Please execute.

When you want to create a folder of so files in the hierarchy where setup.py is located instead of storing it in this folder (At this time, you can ʻimport my lib` only in this hierarchy.) Is

python setup.py build_ext --inplace

Can be.

Optional (when relying on other libraries)

For example, your own library calls tensorflow internally, I think most of the time I use other libraries. At that time, the following work is required plus.

--Create requirements.txt. --Change setup.py a little.

requirements.txt In requirements.txt

requirements.txt


tensorflow==2.2

And so on, write the required library.

Add to setup.py

setup.py


from setuptools import setup, Extension,find_packages

def _requires_from_file(filename):
    return open(filename).read().splitlines()


setup(
    name = 'mylib',
    packages=find_packages(where='mylib'),
    package_dir={'': 'mylib'},
    install_requires=_requires_from_file('requirements.txt'),
)

Write like this. Now try python setup.py install etc. as before.

Summary

This time with the minimum steps possible Implementing your own python module by using setuptools I tried to explain.

This time around here.

end.

Recommended Posts

Make your own module quickly with setuptools (python)
[Python] Make your own LINE bot
To import your own module with jupyter
Publish your own Python library with Homebrew
Make your own music player with Bottle0.13 + jPlayer2.5!
Make your Python environment "easy" with VS Code
Flow of creating your own package with setup.py with python
Memo to create your own Box with Pepper's Python
Let's call your own C ++ library with Python (Preferences)
Call your own python module from the ROS package
Make Puyo Puyo AI with Python
Make a fortune with Python
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.
Reinforcement learning 23 Create and use your own module with Colaboratory
Make Echolalia LINEbot with Python + heroku
Make your own manual. [Linux] [man]
Make apache log csv with python
Let's make a GUI with python.
Solve your own maze with Q-learning
Make a recommender system with python
Let's make a graph with python! !!
Train UGATIT with your own dataset
Solve your own maze with DQN
Make your own VPC with a Single Public Subnet Only with boto
How to make your own domain site with heroku (free plan)
Make the Python console covered with UNKO
Your own Twitter client made with Django
Let's make a shiritori game with Python
Create wordcloud from your tweet with python3
Suddenly with Python PyInstaller No module named pyinstaller
[LLDB] Create your own command in Python
Easily use your own functions in Python
Create your own DNS server with Twisted
[Python] Package and distribute your own modules
Make a relation diagram of Python module
Create your own Composite Value with SQLAlchemy
Fractal to make and play with Python
Make your own NOAA / GOES familiar X-ray flux 3days plot in Python
Create your own virtual camera with Python + OpenCV and apply original effects
HTML document your Python program with Sphinx
[Python] Register your own library on PyPI
Let's make a voice slowly with Python
Until you install your own Python library
Make pypy submission easier with atcoder-cli (python)
Let's make an image recognition model with your own data and play!
Integrating with setuptools / python setup.py test / pytest-runner
[Python] Let's make matplotlib compatible with Japanese
[Python] Quickly create an API with Flask
Let's make a web framework with Python! (1)
[Python] How to deal with module errors
Make a desktop app with Python with Electron
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
How to access data with object ['key'] for your own Python class
Get your own IP address in Python
The road to updating Splunkbase with your own Splunk app for Python v2 / v3
Error when installing a module with Python pip
Make Python scripts into Windows-executable .exes with Pyinstaller
[Python] Implement your own list-like class using collections.UserList