I'm grafi working part-time at ALBERT Co., Ltd. I haven't posted to Qiita before, but I'll post it for the first time.
When doing data analysis, I often read images with Python and perform simple processing, so I would like OpenCV. Most people're done with Anaconda, but I personally don't want to leave the Python ecosystem, and I'd love to specify which libraries to link with.
That's why you're building OpenCV. I think there are explanations about how to build it here and there, so I will omit it. This will create a file such as cv2. Somehow .so
as a Python module. To install it globally, just do make install
.
However, from the environment created with virtualenv
(or venv
), this cv2
module cannot be seen unless --system-site-packages
is added. If I somehow copy cv2.so
to site-packages
in the virtualenv
environment, it works, but it's just not cool.
So I wondered if I could make this a wheel.
What I really want to do is "I want to make one pre-built extension module into a wheel", but it seems that this is not supported.
Therefore, I made a lot of mistakes, but I managed to do it. I mainly referred to
There are three.
First, the directory structure is as follows.
--+-- cv2 --+-- _native --+-- cv2.somehow.so
| | |
| | +-- __init__.py
| |
| +-- __init__.py
|
+-- setup.py
|
+-- MANIFEST.in
In __init__.py
directly under the cv2
directory
import * from _native.cv2
Write.
The __init__.py
under the _native
directory can be empty.
setup.py
is as follows
from setuptools import setup
from setuptools.dist import Distribution
class BinaryDistribution(Distribution):
def has_ext_modules(foo):
return True
setup(
name='cv2',
version='1.0',
packages=['cv2'],
include_package_data=True,
distclass=BinaryDistribution,
)
Also, MANIFEST.in
is
include cv2/_native/cv2.somehow.so
Write.
Now when you run python setup.py bdist_wheel
, you will have a wheel with a file name like cv2-1.0-cp36-cp36m-linux_x86_64.whl
under dist
. I'm happy.
It's simple to do, just create a dummy package and export all the definitions in the original cv2
module. I'd like to hide the original cv2
module for the time being, but if I renamecv2. Somehow .so
to _cv2. Somehow .so
, it will not work, so I solved it by putting it in another package. did.
By forcibly telling you that the extension module is included by setting has_ext_modules = True
, you can make the wheel so that the installation will be rejected if you bring it to another OS.
numpy
and scipy
will not perform well unless you compile them yourself and link them to a high-performance matrix operation library, but it is also convenient to have the compiled result as a wheel. I will write this detail later.
Recommended Posts