danielapai / bioverse

A simulation framework to assess the statistical power of future biosignature surveys
MIT License
7 stars 5 forks source link

ModuleNotFoundError when attempting to import functions #41

Closed astral-carrier closed 7 months ago

astral-carrier commented 8 months ago

When importing functions as part of instantiating a Generator, Python raises a ModuleNotFoundError. The issue seems to be relating to Bioverse not extracting the package name properly from the provided file path.

I have developed a potential fix on my fork of the repository. It is not yet pushed to GitHub, as I still need to confirm it works on platforms other than Windows.

Example of the error, encountered on Tutorial 1:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[2], line 2
      1 # Open the transit mode generator and display its properties
----> 2 generator = Generator('transit')
      3 generator

File c:\\Users\\pandi\\Documents\\GitHub\\bioverse-testing\\.venv\\Lib\\site-packages\\bioverse\\generator.py:33, in Generator.__init__(self, label)
     31 Object.__init__(self, label)
     32 # Reloads each step to catch new keyword arguments
---> 33 self.update_steps()

File c:\\Users\\pandi\\Documents\\GitHub\\bioverse-testing\\.venv\\Lib\\site-packages\\bioverse\\generator.py:71, in Generator.update_steps(self, reload)
     69 for i,step in enumerate(self.steps):
     70     try:
---> 71         step.load_function(reload=reload)
     72     except KeyError:
     73         warn(\"function not found for step '{:s}' - removing it from the sequence!\"\\
     74               .format(step.function_name))

File c:\\Users\\pandi\\Documents\\GitHub\\bioverse-testing\\.venv\\Lib\\site-packages\\bioverse\\generator.py:285, in Step.load_function(self, reload)
    283 self.filename = self.find_filename()
    284 try:
--> 285     func = util.import_function_from_file(self.function_name, ROOT_DIR+'/'+self.filename)
    286 except FileNotFoundError:
    287     # try absolute path
    288     func = util.import_function_from_file(self.function_name, self.filename)

File c:\\Users\\pandi\\Documents\\GitHub\\bioverse-testing\\.venv\\Lib\\site-packages\\bioverse\\util.py:90, in import_function_from_file(function_name, filename)
     88 spec = importlib.util.spec_from_file_location(package_name+'.'+module_name, filename)
     89 mod = importlib.util.module_from_spec(spec)
---> 90 spec.loader.exec_module(mod)
     92 # Return the function
     93 return mod.__dict__[function_name]

File <frozen importlib._bootstrap_external>:940, in exec_module(self, module)

File <frozen importlib._bootstrap>:241, in _call_with_frames_removed(f, *args, **kwds)

File ~\\Documents\\GitHub\\bioverse-testing\\.venv\\Lib\\site-packages\\bioverse/functions.py:18
     15 warnings.filterwarnings(\"ignore\")
     17 # Bioverse modules and constants
---> 18 from .classes import Table
     19 from . import util
     20 from .util import CATALOG, interpolate_df

ModuleNotFoundError: No module named 'C:\\\\Users\\\\pandi\\\\Documents\\\\GitHub\\\\bioverse-testing\\\\'
matiscke commented 8 months ago

Good catch! We recently tested these notebooks on Unix-based systems without issues. Maybe it’s something as simple as slash vs. backslash. Using os.path might fix this?