IDSIA/sacred

gather_sources_and_dependencies fails in notebooks with ipykernel 6.21.0

champialex opened this issue · 2 comments

Jupyter released 6.21.0 on the 31st of Jan.
It includes ipython/ipykernel#1078 which adds a file variable. Sadly, that file doesn't actually exist....

Reproduce in a notebook with

pip install ipykernel==6.21.0
from sacred.dependencies import gather_sources_and_dependencies
gather_sources_and_dependencies(globals(), save_git_info=True)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
path/to/my/notebook/directory/3a203c6e-289c-4c4e-abf1-74450c51ac54 in <cell line: 1>()
----> 1 gather_sources_and_dependencies(globals(), save_git_info=True)

/opt/conda/envs/my_env/lib/python3.8/site-packages/sacred/dependencies.py in gather_sources_and_dependencies(globs, save_git_info, base_dir)
    726 def gather_sources_and_dependencies(globs, save_git_info, base_dir=None):
    727     """Scan the given globals for modules and return them as dependencies."""
--> 728     experiment_path, main = get_main_file(globs, save_git_info)
    729 
    730     base_dir = base_dir or experiment_path

/opt/conda/envs/my_env/lib/python3.8/site-packages/sacred/dependencies.py in get_main_file(globs, save_git_info)
    597         main = None
    598     else:
--> 599         main = Source.create(globs.get("__file__"), save_git_info)
    600         experiment_path = os.path.dirname(main.filename)
    601     return experiment_path, main

/opt/conda/envs/my_env/lib/python3.8/site-packages/sacred/dependencies.py in create(filename, save_git_info)
    455     def create(filename, save_git_info=True):
    456         if not filename or not os.path.exists(filename):
--> 457             raise ValueError('invalid filename or file not found "{}"'.format(filename))
    458 
    459         main_file = get_py_file_if_possible(os.path.abspath(filename))

ValueError: invalid filename or file not found "path/to/my/notebook/directory/3a203c6e-289c-4c4e-abf1-74450c51ac54"

It works for me:

!pip show ipykernel
Name: ipykernel
Version: 6.21.0
[...]

from sacred.dependencies import gather_sources_and_dependencies
gather_sources_and_dependencies(globals(), save_git_info=True)

(None,
 set(),
 {<PackageDependency: ipython=8.10.0>,
  <PackageDependency: numpy=1.21.5>,
  <PackageDependency: sacred=0.8.4>})

Are there other packages / version differences that could cause this on your side?

I'm guessing that the difference is that I'm running in jupyterhub.
Or do you have __file__ that is defined, and that also exists according to os.path.exists(__file__)?