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.
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)
That's all you need.
--Folder to put the contents of the library
--Code that defines some function or class (.py
)
setup.py
--__init__.py
(There are two underscores before and after)This time, assuming that we will create a module called mylib
, we prepared as follows.
tree .
setup.py
mylib
|-- __init__.py
|-- mylib.py
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')
__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.
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.
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.
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.
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.
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