In more complex data science projects, you may have a folder `notebooks/` to organize notebooks and another folder `scripts/` with code for the project. You may want to use that code in a [[Jupyter Notebook]] for quick debugging and development. How can you do this? If you have just one file with code you need to import, add the file's parent folder to your [[PATH]] variable. If you have multiple files to import (or plan to later), make your library a package.
## add to path
Let's say you have a script `functions.py` in a folder `scripts/` in the same level as a folder `notebooks/` where your current `.ipynb` file is. If you won't be making any changes to the library, you can add the directory to your [[PATH]] variable.
```python
import sys
import os
scripts_path = os.path.abspath("../scripts")
sys.path.append(scripts_path)
from functions import foo_bar
```
However, [[Python]] caches imported modules, so if you modify your external script and re-run this cell, the changes won't be reflected in your Jupyter Notebook, unless you restart the kernel. To avoid restarting the kernel, use `importlib.reload()` like so
```python
import sys
import os
import importlib
components_path = os.path.abspath("../components")
sys.path.append(components_path)
import functions
importlib.reload(functions)
from functions import foo_bar
```
If this isn't working for you, confirm that your project directory looks like this
```
.
+-- notebooks/
+-- my_notebook.ipynb
+-- scripts/
+-- functions.py
```
## make your library a package
You can also turn the folder `scripts/` into a Python package by creating an empty `__init__.py` file. This option is best if you will be importing code from this folder often or if you will be importing from multiple files in this folder.
```
.
+-- notebooks/
+-- my_notebook.ipynb
+-- scripts/
+-- __init__.py
+-- functions.py
```
Then simply use this at the top of your notebook.
```python
import sys
sys.path.append("..")
import scripts
```