Skip to content

Python module: replace config.py files by config.ini #16008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

tpetazzoni
Copy link

The OpenCV Python module currently installs and reads at runtime a config.py file and a config-X.Y.py file to retrieve some bits of configuration. This is done using the execfile() Python mechanism, which unfortunately only works with .py files, and not with .pyc files.

However, on some embedded systems, only .pyc files are installed: they are already byte-compiled, which optimizes the startup time, and installating both .pyc and .py files doubles the storage space, which is a limited resource on embedded systems.

In addition, loading .py file requires different code between Python 2 and Python 3.

To solve these issues, this pull request replaces the .py files by .ini files, which can trivially be parsed by the standard ConfigParser Python module.

…ATH_RELATIVE_CONFIGCMAKE when not needed

The variable OpenCV_PYTHON_INSTALL_PATH_RELATIVE_CONFIGCMAKE is set in
the if(IS_ABSOLUTE ...), but it is not used anywhere afterwards.

A variable of the same name is defined and used in the else() clause
of the same condition, but it is only used within that else() clause.

Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
The cv2 Python modules currently reads at runtime some bits of
configuration from config.py and config-X.Y.py files, by executing
them using execfile() or similar inside the Python interpreter.

Unfortunately, this requires having the .py files themselves
available, which may not be the case on embedded systems where only
the byte-compiled .pyc files are present (the .pyc files are already
byte-compiled, and in order to be space-efficient, .py files are not
installed on some embedded systems).

Due to the lack of .py files, importing the cv2 Python module
fails. Since this configuration mechanism is a bit unusual, and also
requires separate handling for Python 2 and Python 3, this commit
replaces it by using two .ini files, which are generated with the same
information as the .py files, but parsed using the standard
ConfigParser module of Python.

config.ini looks like this:

[python]
binpath = /usr/lib

while config-3.8.ini looks like this:

[python]
extensionpath = /usr/lib/python3.8/site-packages/cv2/python-3.8

If multiple paths are present, they are stored colon-separated, and
they are split again when the configuration files are parsed.

The only change relevant mentioning is that the expansion of
LOADER_DIR is now done when the configuration is parsed: if we have an
absolute path, we take it as-is, if there is a relative path, we
assume it is relative to LOADER_DIR.

Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
@@ -3,6 +3,7 @@
'''
import os
import sys
import configparser
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Python2 it is named ConfigParser.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, thank you, indeed.

@VadimLevin
Copy link
Contributor

Thanks for you contribution!
@tpetazzoni Do you have any chance to proceed work on this PR?

@asmorkalov
Copy link
Contributor

@tpetazzoni any progress on the patch?

@tpetazzoni
Copy link
Author

@asmorkalov No sorry, not yet, I'll try to get to it.

@alalek
Copy link
Member

alalek commented Jan 10, 2020

on some embedded systems, only .pyc files are installed

Consider disabling OpenCV Python bindings loader in your configuration: cmake -DOPENCV_SKIP_PYTHON_LOADER=ON ...

Perhaps .ini files will not work for us. In the future we want to detect Python distributions in runtime, like Anaconda Python, Intel Python distribution, etc. One package + auto detection/configuration through .py files config.

Instead of .ini approach you may take a look in direction of changing of importing approach (use some "import" code instead of "execfile")

@asmorkalov
Copy link
Contributor

OpenCV team decided to close the PR as expected behavior is reachable with -DOPENCV_SKIP_PYTHON_LOADER=ON option in CMake. See alalek's comment above.

@asmorkalov asmorkalov closed this Jan 31, 2020
@tpetazzoni
Copy link
Author

Well, OPENCV_SKIP_PYTHON_LOADER may not be an option: some people do want the Python bindings of OpenCV available on their embedded system.

@alalek
Copy link
Member

alalek commented Jan 31, 2020

OPENCV_SKIP_PYTHON_LOADER doesn't disable OpenCV Python bindings itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants