Skip to content
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

add support for python importlib to locate MRtrix3 python libraries #2735

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 38 additions & 12 deletions bin/mrtrix3.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,48 @@
#
# For more details, see http://www.mrtrix.org/.

import imp, os, sys
import os, sys
from distutils.spawn import find_executable

def imported(lib_path):
success = False
fp = None
try:
# since importlib code below only works on Python 3.5+
# https://stackoverflow.com/a/50395128
if sys.version_info < (3,5):
raise ImportError

import importlib.util

def imported(lib_path):
try:
spec = importlib.util.spec_from_file_location('mrtrix3', os.path.join (lib_path, 'mrtrix3', '__init__.py'))
module = importlib.util.module_from_spec (spec)
sys.modules[spec.name] = module
spec.loader.exec_module (module)
return True
except ImportError:
return False

except ImportError:
try:
fp, pathname, description = imp.find_module('mrtrix3', [ lib_path ])
imp.load_module('mrtrix3', fp, pathname, description)
success = True
import imp
except ImportError:
pass
finally:
if fp:
fp.close()
return success
print ('failed to import either imp or importlib module!')
sys.exit(1)

def imported(lib_path):
success = False
fp = None
try:
fp, pathname, description = imp.find_module('mrtrix3', [ lib_path ])
imp.load_module('mrtrix3', fp, pathname, description)
success = True
except ImportError:
pass
finally:
if fp:
fp.close()
return success


# Can the MRtrix3 Python modules be found based on their relative location to this file?
# Note that this includes the case where this file is a softlink within an external module,
Expand Down