# Unable to import local modules to run the JupyterBook

**URL:** https://forum.kavli.tudelft.nl/t/unable-to-import-local-modules-to-run-the-jupyterbook/95
**Category:** Programming questions
**Created:** [10 June 2020 14:00 UTC](https://forum.kavli.tudelft.nl/t/unable-to-import-local-modules-to-run-the-jupyterbook/95 "2020-06-10T14:00:39Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Leila](https://forum.kavli.tudelft.nl/user_avatar/forum.kavli.tudelft.nl/leila/32/28_2.png) [@Leila](https://forum.kavli.tudelft.nl/u/Leila)
#### Post date: [10 June 2020 14:00 UTC](https://forum.kavli.tudelft.nl/t/unable-to-import-local-modules-to-run-the-jupyterbook/95/1 "2020-06-10T14:00:39Z")

</div>

**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](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:

```python
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!

---

<div class="post-metadata">

### Author: ![anton-akhmerov](https://forum.kavli.tudelft.nl/user_avatar/forum.kavli.tudelft.nl/anton-akhmerov/32/4_2.png) [@anton-akhmerov](https://forum.kavli.tudelft.nl/u/anton-akhmerov)
#### Post date: [10 June 2020 15:27 UTC](https://forum.kavli.tudelft.nl/t/unable-to-import-local-modules-to-run-the-jupyterbook/95/2 "2020-06-10T15:27:55Z")

</div>

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](https://github.com/executablebooks/jupyter-book/issues/705). 🙃

---

<div class="post-metadata">

### Author: ![Leila](https://forum.kavli.tudelft.nl/user_avatar/forum.kavli.tudelft.nl/leila/32/28_2.png) [@Leila](https://forum.kavli.tudelft.nl/u/Leila)
#### Post date: [10 June 2020 16:18 UTC](https://forum.kavli.tudelft.nl/t/unable-to-import-local-modules-to-run-the-jupyterbook/95/3 "2020-06-10T16:18:23Z")

</div>

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

Great 🙂

---

<div class="post-metadata">

### Author: ![anton-akhmerov](https://forum.kavli.tudelft.nl/user_avatar/forum.kavli.tudelft.nl/anton-akhmerov/32/4_2.png) [@anton-akhmerov](https://forum.kavli.tudelft.nl/u/anton-akhmerov)
#### Post date: [10 June 2020 16:38 UTC](https://forum.kavli.tudelft.nl/t/unable-to-import-local-modules-to-run-the-jupyterbook/95/4 "2020-06-10T16:38:18Z")

</div>

Don’t mention it, I have a selfish interest in jupyter-book, so I’ve been [opening issues](https://github.com/search?q=org%3Aexecutablebooks+is%3Aissue+author%3Aakhmerov&unscoped_q=is%3Aissue+author%3Aakhmerov) there before 😃

---

<div class="post-metadata">

### Author: ![Leila](https://forum.kavli.tudelft.nl/user_avatar/forum.kavli.tudelft.nl/leila/32/28_2.png) [@Leila](https://forum.kavli.tudelft.nl/u/Leila)
#### Post date: [10 June 2020 16:39 UTC](https://forum.kavli.tudelft.nl/t/unable-to-import-local-modules-to-run-the-jupyterbook/95/5 "2020-06-10T16:39:53Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Leila](https://forum.kavli.tudelft.nl/user_avatar/forum.kavli.tudelft.nl/leila/32/28_2.png) [@Leila](https://forum.kavli.tudelft.nl/u/Leila)
#### Post date: [10 June 2020 16:45 UTC](https://forum.kavli.tudelft.nl/t/unable-to-import-local-modules-to-run-the-jupyterbook/95/6 "2020-06-10T16:45:01Z")

</div>

yes I have seen them 🙂 but still , your “selfishness” is really collaborative 😉

---

<div class="post-metadata">

### Author: ![Leila](https://forum.kavli.tudelft.nl/user_avatar/forum.kavli.tudelft.nl/leila/32/28_2.png) [@Leila](https://forum.kavli.tudelft.nl/u/Leila)
#### Post date: [11 June 2020 08:02 UTC](https://forum.kavli.tudelft.nl/t/unable-to-import-local-modules-to-run-the-jupyterbook/95/7 "2020-06-11T08:02:48Z")

</div>

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 .

---

<div class="post-metadata">

### Author: ![Leila](https://forum.kavli.tudelft.nl/user_avatar/forum.kavli.tudelft.nl/leila/32/28_2.png) [@Leila](https://forum.kavli.tudelft.nl/u/Leila)
#### Post date: [13 June 2020 07:32 UTC](https://forum.kavli.tudelft.nl/t/unable-to-import-local-modules-to-run-the-jupyterbook/95/8 "2020-06-13T07:32:39Z")

</div>

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](https://leilaicruz.github.io/SATAY-jupyter-book/gene_reads.html#loading-files)

Thanks!

---

<div class="post-metadata">

### Author: ![anton-akhmerov](https://forum.kavli.tudelft.nl/user_avatar/forum.kavli.tudelft.nl/anton-akhmerov/32/4_2.png) [@anton-akhmerov](https://forum.kavli.tudelft.nl/u/anton-akhmerov)
#### Post date: [13 June 2020 13:36 UTC](https://forum.kavli.tudelft.nl/t/unable-to-import-local-modules-to-run-the-jupyterbook/95/9 "2020-06-13T13:36:51Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Leila](https://forum.kavli.tudelft.nl/user_avatar/forum.kavli.tudelft.nl/leila/32/28_2.png) [@Leila](https://forum.kavli.tudelft.nl/u/Leila)
#### Post date: [13 June 2020 13:58 UTC](https://forum.kavli.tudelft.nl/t/unable-to-import-local-modules-to-run-the-jupyterbook/95/10 "2020-06-13T13:58:43Z")

</div>

what exactly do you mean when saying?

> [@anton-akhmerov](#):
>
> the latest version should make this unnecessary

to what you refer to?

---

<div class="post-metadata">

### Author: ![anton-akhmerov](https://forum.kavli.tudelft.nl/user_avatar/forum.kavli.tudelft.nl/anton-akhmerov/32/4_2.png) [@anton-akhmerov](https://forum.kavli.tudelft.nl/u/anton-akhmerov)
#### Post date: [13 June 2020 14:08 UTC](https://forum.kavli.tudelft.nl/t/unable-to-import-local-modules-to-run-the-jupyterbook/95/11 "2020-06-13T14:08:16Z")

</div>

[This comment](https://github.com/executablebooks/jupyter-book/issues/704#issuecomment-642977831). 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.

---

<div class="post-metadata">

### Author: ![Leila](https://forum.kavli.tudelft.nl/user_avatar/forum.kavli.tudelft.nl/leila/32/28_2.png) [@Leila](https://forum.kavli.tudelft.nl/u/Leila)
#### Post date: [13 June 2020 14:12 UTC](https://forum.kavli.tudelft.nl/t/unable-to-import-local-modules-to-run-the-jupyterbook/95/12 "2020-06-13T14:12:51Z")

</div>

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. 🙂
