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.
sample.py
, __ init__.py
, setup.py
pip install
foo.py
and run itWith 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.
sample.py
, __ init__.py
, setup.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))
__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'
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.
foo.py
and runimport 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
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