Unable to import local modules to run the JupyterBook

Describe the bug
Unable to import local modules in the Book

To Reproduce
Steps to reproduce the behavior:

  1. Go to the repo , to ‘gh-pages’ and clone it : https://github.com/leilaicruz/SATAY-jupyter-book/tree/gh-pages
  2. cd SATAY-jupyter-book
  3. git checkout windows
  4. conda env create -f environment_win.yml
  5. conda activate wintest
  6. cd mini_book
  7. runjb docs
  8. See error [1;31mModuleNotFoundErrore[0m: No module named 'chromosome_and_gene_positions' ModuleNotFoundError: No module named 'chromosome_and_gene_positions'

Expected behavior
I expect that the file named : gene_reads.md is able to import the modules that require inside the python_modules folder, where I have this code, that works in jupyter notebook:

file_dirname = os.path.dirname(os.path.abspath('__file__'))
sys.path.insert(1,os.path.join(file_dirname,'python_modules'))
from chromosome_and_gene_positions import gene_position

Environment (please complete the following information):

  • Python Version [e.g. 3.7.7]

Thanks!

Can you print the path of __file__?

As far as I can tell, the problem occurs due to the working directory being different during execution than that of the file on disk.

As far as how this should be done, I have relayed your issue to the higher authority. :upside_down_face:

1 Like

waooo @anton-akhmerov Thanks so much for your proactivity! I will check the path.

Great :slight_smile:

Don’t mention it, I have a selfish interest in jupyter-book, so I’ve been opening issues there before :smiley:

I also submitted this issue to the higher authorities … but thanks to do it because somehow got more attention. Thanks for that.

1 Like

yes I have seen them :slight_smile: but still , your “selfishness” is really collaborative :wink:

When I print the path of __file__ is the path to python_modules folder where are all the functions I later call. In the normal nortebook it works , but not in the Jupyter Book workflow. Maybe it has to do with the .py files inside /docs ?? I put in the config.yml to exclude_patterns : ["*.py"] but still It does not work .

I have solved it! The point is that it seems that the local build and the web deployment function under different logic on how to interpret the relative path. For the web deployment , the Book takes the same folder structures you have in the project and the output of file_dirname = os.path.dirname('__file__') is exactly the path of the file you are in . For the local build I struggled a bit with this, and what is sometimes a bit annoying is that with the same structure that run in the web it does not build locally and viceversa.
For the modules , the way to call them is from folder_root.subfolder-with-modules.module-a import module-a and it seems also important to add a empty file called __init__.py to the subfolder-with-modules. You can see what I mean here : https://leilaicruz.github.io/SATAY-jupyter-book/gene_reads.html#loading-files

Thanks!

Yep, that’s a good idea.

A word of warning though: your approach is mostly a workaround due to the current limitations of jupyter-book. From what I understand, the latest version should make this unnecessary.

what exactly do you mean when saying?

to what you refer to?

This comment. The latest version of one of the packages used in jupyter-book ensures that the files are executed in the same folder to which they belong.

Yes indeed, I think that is the case for web deployment , but did not work for me for the local build. :thinking:
I installed the new version when I saw the issue reply .

I think this Jupyter Book is a really nice tool for Open science practices , to show , share, and test your code , only using markdown , python and version control with git. :slightly_smiling_face: