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

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:

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

In my Jupyter Notebook

Working Solution:

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:

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:

# 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?

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.

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.

Thank you that works

1 Like