Flow of creating your own package with setup.py with python

Introduction

It is a flow to create your own package with setup.py with python. I had a hard time with the error module not found, so I'd like to leave a note here so that I don't forget it. This time, I will not give it to PyPi etc., but simply organize the procedure to pip install in the local environment.

flow

  1. Describe the contents of sample.py, __ init__.py, setup.py
  2. Install the package locally with pip install
  3. Write the contents of foo.py and run it

Directory structure

With the following structure

--Install the functions in sample.py with pip install --Suppose you want to run those functions with foo.py.

Documents
└ tasks_project
  ├ src
  │   └ tasks
  │       ├ __init__.py
  │       └ sample.py     # sample.I want to package a function in py
  ├ hoge
  │   └ foo.py            # foo.sample with py.Run py function
  └ setup.py

Since these two directories are cousins, the following error will occur if you import them relative to each other.

attempted relative import beyond top-level package.

So, install it with pip as follows so that you can import it from anywhere.

Describe the contents of sample.py, __ init__.py, setup.py

Contents of sample.py

In sample.py, write the function you want. I simply wrote something like the following. I'm importing various modules because I wanted to check if numpy and pandas can be used properly.

import numpy as np
import pandas as pd
import datetime

def hello():
    print("hello_world")


def get_array():
    return np.array([1, 2, 3])


def get_df():
    return pd.Series([1, 2, 3])


def get_date():
    print(datetime.datetime(2019, 12, 1, 1, 1, 1))

Contents of __init__.py

I didn't write this here, so I suspect I was getting a module not found error. It's like setting which function to install at the time of pip install.

from .sample import (
    hello,
    get_array,
    get_df,
    get_date,
)

__version__ = '0.1.0'

Contents of setup.py

Make the most important settings when doing pip install in setup.py. Below, I think it's okay if you write name, version, packages, and package_dir properly.

"""Minimal setup file for tasks project."""

from setuptools import setup, find_packages

setup(
    name='tasks',
    version='0.1.0',
    license='proprietary',
    description='Module Experiment',

    author='greenteabiscuit',
    author_email='[email protected]',
    url='None.com',

    packages=find_packages(where='src'),
    package_dir={'': 'src'},
)

pip install

Move to the directory one level above tasks_project ( Documents in this case), and then perform pip install.

$ cd Documents
$ pip install ./tasks_project/

Processing ./tasks_project
Building wheels for collected packages: tasks
  Building wheel for tasks (setup.py) ... done
..........Abbreviation..........
Successfully installed tasks-0.1.0

If you see Successfully installed, you are successful.

Describe the contents of foo.py and run

import tasks

tasks.hello()

arr = tasks.get_array()
print(arr)

df = tasks.get_df()
print(df)

tasks.get_date()

If you write this, you should run it below and get results without any errors.

$ cd Documents/hoge/
$ python foo.py

hello_world
[1 2 3]
0    1
1    2
2    3
dtype: int64
2019-12-01 01:01:01

Finally

This will make it a little easier to write test code etc. I wanted to know earlier, but for some reason I couldn't find many good articles. .. .. I refer to the contents of the following books.

Python Testing with pytest: Simple, Rapid, Effective, and Scalable (English Edition)

Recommended Posts

Flow of creating your own package with setup.py with python
Run the intellisense of your own python library with VScode.
[Python] Package and distribute your own modules
Publish your own Python library with Homebrew
Make your own module quickly with setuptools (python)
Flow of creating a virtual environment with Anaconda
Memo to create your own Box with Pepper's Python
[Introduction to Udemy Python 3 + Application] 66. Creating your own exceptions
Let's call your own C ++ library with Python (Preferences)
Define your own distance function with k-means of scikit-learn
Creating BINGO "Web Tools" with Python (Table of Contents)
Summary of the basic flow of machine learning with Python
Call your own python module from the ROS package
Creating an egg with python
Until you can install your own Python library with pip
Try sorting your own objects with priority queue in Python
A record of patching a python package
[Package cloud] Manage python packages with package cloud
[Python] Make your own LINE bot
[Python] logging in your own module
Solve your own maze with Q-learning
Getting Started with Python Basics of Python
Visualize python package dependencies with graphviz
Life game with Python! (Conway's Game of Life)
10 functions of "language with battery" python
[Introduction to StyleGAN] Unique learning of anime with your own machine ♬
Introduction of python drawing package pygal
Implementation of Dijkstra's algorithm with python
Train UGATIT with your own dataset
Coexistence of Python2 and 3 with CircleCI (1.0)
[Python] Creating multiple windows with Tkinter
Solve your own maze with DQN
Bookkeeping Learned with Python-The Flow of Bookkeeping-
Basic study of OpenCV with Python
Analyze the source code of your own simple search engine written in Python with the code visualization tool "SOURCE TRAIL"
Get a list of packages installed in your current environment with python
Create your own virtual camera with Python + OpenCV and apply original effects
Explanation of creating an application for displaying images and drawing with Python
How to access data with object ['key'] for your own Python class
Basics of binarized image processing with Python
[Examples of improving Python] Learning Python with Codecademy
Creating a simple PowerPoint file with Python
Your own Twitter client made with Django
Create your own Linux commands in Python
[Reinforcement learning] DQN with your own library
Create wordcloud from your tweet with python3
Two-dimensional saturated-unsaturated osmotic flow analysis with Python
[LLDB] Create your own command in Python
Execute Python script with cron of TS-220
Easily use your own functions in Python
Python installation and package management with pip
Create your own DNS server with Twisted
Note when creating an environment with python
Check the existence of the file with python
Algorithm learned with Python 8th: Evaluation of algorithm
Create your own Composite Value with SQLAlchemy
Clogged with python update of GCP console ①
Easy introduction of speech recognition with Python
HTML document your Python program with Sphinx
[Python] Register your own library on PyPI
Until you install your own Python library