Skip to content

Instantly share code, notes, and snippets.

@anujonthemove
Created October 8, 2024 04:56
Show Gist options
  • Save anujonthemove/26410fb3db00cd952f216955154ae633 to your computer and use it in GitHub Desktop.
Save anujonthemove/26410fb3db00cd952f216955154ae633 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"source": [
"### Importing files from modules into Jupyter Notebooks\n",
"\n",
"* If you have a defined project structure like the one below \n",
"and want to import utility or source files into your notebooks located outside under `app` folder, \n",
"you can use the code snippet provided to make those files accessible in your path and import them seamlessly.\n",
"\n",
" ```\n",
" ├── app\n",
" │   ├── src\n",
" │   └── utils\n",
" └── notebooks\n",
" ```\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# To enable this, add the following code to your notebook:\n",
"\n",
"import os\n",
"import sys\n",
"\n",
"# Get the current working directory\n",
"current_working_directory = os.getcwd()\n",
"\n",
"# Append the parent directory to the system path\n",
"sys.path.append(os.path.join(current_working_directory, \"../\"))\n",
"\n",
"# Now you can import modules from your project structure, for example:\n",
"# from app.utils.<module_name> import <class_or_function_name>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment