I am building an image processing module in Python where I use some Fortran90 compiled files to make the iterative methods faster, but these files depend strictly on the Python version.
Right now, I have this directory structure:
├── ImageExtras.egg-info
│ ├── PKG-INFO
│ ├── SOURCES.txt
│ ├── dependency_links.txt
│ ├── requires.txt
│ ├── top_level.txt
│ └── zip-safe
├── LICENSE
├── MANIFEST.in
├── README.md
├── __init__.py
├── imageextras
│ ├── __init__.py <--
│ ├── cmp_src
│ │ └── dithering.f90
│ ├── compiled
│ │ ├── __init__.py
│ │ ├── py310
│ │ │ ├── __init__.py
│ │ │ ├── linux
│ │ │ │ ├── __init__.py
│ │ │ │ └── dithering.cpython-310-x86_64-linux-gnu.so
│ │ │ └── win
│ │ │ ├── __init__.py
│ │ │ └── dithering.cp310-win_amd64.pyd
│ │ └── py39
│ │ ├── __init__.py
│ │ ├── linux
│ │ │ ├── __init__.py
│ │ │ └── dithering.cpython-39-x86_64-linux-gnu.so
│ │ └── win
│ │ ├── __init__.py
│ │ └── dithering.cp39-win_amd64.pyd
│ └── src
│ ├── __init__.py
│ └── utils.py
├── setup.cfg
└── setup.py
The problem I'm having occurs in the python init file that I have marked with an arrow. I want that, depending on the operating system and the Python version, a different file is imported, so I made this and added to the marked init file:
# Native imports
from imageextras.src.utils import *
# Compiled imports
import os
import platform
import sys
system = platform.system()
if (sys.version_info >= (3, 10)) and (sys.version_info < (3, 11)):
py_ver = "py310"
elif (sys.version_info >= (3, 9)) and (sys.version_info < (3, 10)):
py_ver = "py39"
else:
raise RuntimeError("Unsupported Python version")
if system == "Windows":
os_choice = "win"
elif system == "Linux":
os_choice = "linux"
else:
raise OSError(f"Unsupported platform: {system}")
module_names = ["dithering"] # modules to add
for module_name in module_names:
module_dir = os.path.join("imageextras", "compiled", py_ver, os_choice)
sys.path.append(module_dir)
__import__(module_name)
For some reason it is unable to import the files.
I built the package and whenever I tried to import the module, it is just unable to import the "dithering" module. The thing is that, using the package itself without installing it works fine, but as soon as I "pip install ." and try to use the package from another directory, it is unable to find the file. By the way, I made sure to include the needed files in the manifest and if I check the installation, all the files are where they are supposed to be, which makes me clueless. I also tried changing the relative paths to absolute paths, but it didn't work either.
As a desperate attempt, I tried changing the init file to:
# Native imports
from imageextras.src.utils import *
# Provisional workaround
try:
from imageextras.compiled.py310.win import dithering
except:
try:
from imageextras.compiled.py39.win import dithering
except:
try:
from imageextras.compiled.py310.linux import dithering
except:
from imageextras.compiled.py39.linux import dithering
And this worked just fine. I'm clueless. The error I'm getting is: "module dithering not found"
Update
I got it to work, but in a rather sketchy way:
module_names = ["dithering"] # modules to add
for module_name in module_names:
package_dir = pkg_resources.resource_filename(__name__, "")
module_dir = os.path.join(package_dir, "compiled", py_ver, os_choice)
sys.path.append(module_dir)
module = __import__(module_name)
setattr(sys.modules[__name__], module_name, module)
sys.path.remove(module_dir)
Im going to leave the question open in case someone is able to come up with a better solution. Thanks!