# Importing Compiled C Extensions ^ Python Files from Folder Dynamically in Jupyter Notebook

**URL:** https://discourse.jupyter.org/t/importing-compiled-c-extensions-python-files-from-folder-dynamically-in-jupyter-notebook/20811
**Category:** JupyterHub
**Tags:** jupyterlab, jupyterhub, how-to, help-wanted, notebook
**Created:** [August 8, 2023, 8:35am UTC](https://discourse.jupyter.org/t/importing-compiled-c-extensions-python-files-from-folder-dynamically-in-jupyter-notebook/20811 "2023-08-08T08:35:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![eggyboy](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@eggyboy](https://discourse.jupyter.org/u/eggyboy)
#### Post date: [August 8, 2023, 8:35am UTC](https://discourse.jupyter.org/t/importing-compiled-c-extensions-python-files-from-folder-dynamically-in-jupyter-notebook/20811/1 "2023-08-08T08:35:47Z")

</div>

I have a Jupyter Notebook file in which I’m trying to import compiled C extensions and Python files from a `src` folder located within the same directory. I can successfully import the module using `import src.module_name`, but I want to achieve dynamic imports with just `import module_name`. I have also configured my `pyproject.toml` file to set the `python_paths` option but it doesn’t really work. Here’s my directory structure:

```auto
project_folder/
├── my_notebook.ipynb
├── src/
│ └── module_name.c
└── pyproject.toml

```

In my Jupyter Notebook

**Working Solution:**

```python
import src.module_name

# Calculate lengths and elements for the range from 1 to 15 using Cython
sample_range, lengths, elements = src.module_name.calculate_lengths_cython(1, 15)

```

**Not Working Solution:**

```python
import module_name

# Calculate lengths and elements for the range from 1 to 15 using Cython
sample_range, lengths, elements = module_name.calculate_lengths_cython(1, 15)

```

And here’s my `pyproject.toml` file:

```toml
# pyproject.toml
[tool.pytest.ini_options]
python_paths = ["src"]

```

Note: The `module_name.c` file contains C code that has been compiled into a C extension.

Is there a way to achieve dynamic imports without explicitly including the `src` folder in the import statement?

---

<div class="post-metadata">

### Author: ![eggyboy](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@eggyboy](https://discourse.jupyter.org/u/eggyboy)
#### Post date: [August 8, 2023, 6:11pm UTC](https://discourse.jupyter.org/t/importing-compiled-c-extensions-python-files-from-folder-dynamically-in-jupyter-notebook/20811/3 "2023-08-08T18:11:54Z")

</div>

I deleted the other one, I didn’t think posting on 2 different forums would be problematic as I wanted to reach as many informed people as possible. But I understand that it is best to have a single post addressing the question.

---

<div class="post-metadata">

### Author: ![bollwyvl](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/bollwyvl/32/904_2.png) [@bollwyvl](https://discourse.jupyter.org/u/bollwyvl)
#### Post date: [August 8, 2023, 6:17pm UTC](https://discourse.jupyter.org/t/importing-compiled-c-extensions-python-files-from-folder-dynamically-in-jupyter-notebook/20811/4 "2023-08-08T18:17:59Z")

</div>

If writing a `pyproject.toml`, it’s not really going to have any effect for python itself until it’s installed, e.g. with `pip install -e .`, and the compiling/cythonizing step actually occurs. Then `import module_name` should work.

---

<div class="post-metadata">

### Author: ![eggyboy](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@eggyboy](https://discourse.jupyter.org/u/eggyboy)
#### Post date: [August 8, 2023, 9:10pm UTC](https://discourse.jupyter.org/t/importing-compiled-c-extensions-python-files-from-folder-dynamically-in-jupyter-notebook/20811/6 "2023-08-08T21:10:58Z")

</div>

Thank you that works
