Make your own module quickly with setuptools (python)


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

By writing and building using setuptools,

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.


The explanation is as follows.

--Prepare folders and files. --Write a little library contents. --Write a little --Write a little --Command execution of python --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 .

Write the function you want to make into a library.

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

class Test():
    def __init__(self):

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

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


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

Just write a few lines about.

from .mylib import (

__version__ = '0.0.0'

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

Write a little

from setuptools import setup, Extension,find_packages

    name = 'mylib',
    package_dir={'': 'mylib'},

This is ok.


The python library is as ashared object file (so file) It is stored in the python environment folder. The location is 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 install

Please execute.

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

python 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 a little.

requirements.txt In requirements.txt



And so on, write the required library.

Add to

from setuptools import setup, Extension,find_packages

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

    name = 'mylib',
    package_dir={'': 'mylib'},

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


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

This time around here.


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 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 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