Describe the bug
Unable to import local modules in the Book
Steps to reproduce the behavior:
- Go to the repo , to ‘gh-pages’ and clone it : https://github.com/leilaicruz/SATAY-jupyter-book/tree/gh-pages
git checkout windows
conda env create -f environment_win.yml
conda activate wintest
- See error
[1;31mModuleNotFoundErrore[0m: No module named 'chromosome_and_gene_positions' ModuleNotFoundError: No module named 'chromosome_and_gene_positions'
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__'))
from chromosome_and_gene_positions import gene_position
Environment (please complete the following information):
- Python Version [e.g. 3.7.7]
Can you print the path of
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.
waooo @anton-akhmerov Thanks so much for your proactivity! I will check the path.
Don’t mention it, I have a selfish interest in jupyter-book, so I’ve been opening issues there before
I also submitted this issue to the higher authorities … but thanks to do it because somehow got more attention. Thanks for that.
yes I have seen them but still , your “selfishness” is really collaborative
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
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
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.
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.