1

How can I import a jupyter notebook which has spaces in the name or starting with a number? I know that is not a good standard to have spaces or numbers in the file names, but it's low effort for files to be ordered in a folder or in GitHub. I already tried different ways and blog posts but I cannot find any real solution.

Example: file to import = "4 linear algebra.ipynb" or "4_linear_algebra.ipynb"

  • Does this answer your question? [How do you import a file in python with spaces in the name?](https://stackoverflow.com/questions/9123517/how-do-you-import-a-file-in-python-with-spaces-in-the-name) – Jacques Gaudin Feb 17 '22 at 14:35
  • unfortunately, it does not work with jupyter notebooks. – Federico Baldi Feb 17 '22 at 15:36
  • 2
    Are you aware that jupyter notebooks can't be imported as easily as regular `.py` files? https://jupyter-notebook.readthedocs.io/en/4.x/examples/Notebook/rstversions/Importing%20Notebooks.html – Jacques Gaudin Feb 17 '22 at 15:50
  • yes, I know but this is exactly my problem. – Federico Baldi Feb 17 '22 at 16:14
  • Can't in you operationally make a copy of the notebook that conforms and import that? You can add a setting to ignore it on GitHub if you aren't looking to mess with your organization. There's some help out there for importing when your names conform to Python's rules, see [here](https://discourse.jupyter.org/t/package-for-importing-jupyter-notebooks-as-modules/12923/2?u=fomightez). Alternatively, maybe more in line with with @Jacques Gaudin is suggesting, you can use juptext to convert your notebook to Python or IPython & try to import what you need that way? You'd want to ignore that too. – Wayne Feb 17 '22 at 21:36
  • You could also leave your current main repo branch intact and add Github actions to handle some of these conversion steps upon each commit. The 'expanded' set of files could even be in a different branch that could be you operational branch. This would still keep your main branch organization intact. Also I think @Jacques Gaudin's point is valid. If this is going to be super useful code, make it a Python file you can import into multiple notebooks, including the one you may have first developed it in. – Wayne Feb 17 '22 at 21:40
  • the importnb was almost the solution, but it's not properly working. When I try to do `with Notebook: import __4__linear_algebra` I get a warning from Pylance `Import "__4__linear_algebra" could not be resolved` – Federico Baldi Feb 18 '22 at 14:11

0 Answers0