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?