Reputation: 987
I am working in a Jupyter directory that contains different notebooks, in different languages. When I open a Python notebook, I would like the Python kernel to automatically import a specific module, if found in the same folder.
For example, let say I have the file tree below:
root_folder
|- subfolder_1
| |- script1.ipynb
| |- script2.ipynb
| `- startup.py
`- subfolder_2
`- script3.ipynb
I would like the Python kernel to automatically import the startup.py
module on script1.ipynb
or script2.ipynb
opening. For script3.ipynb
, I would like not to raise any error (but try and import a startup.py
in case it exists).
The goal is to provide some folder specific functions without having to manually import this module.
I know it is not "expensive" to write from startup import *
, but I would like to do without it.
Upvotes: 4
Views: 1548
Reputation: 4765
You can utilize startup files.
You should be able to find them in <profile>/startup
directory.
Copied from docs:
If you want some code to be run at the beginning of every IPython session with a particular profile, the easiest way is to add Python (.py) or IPython (.ipy) scripts to your /startup directory. Files in this directory will always be executed as soon as the IPython shell is constructed, and before any other code or scripts you have specified. If you have multiple files in the startup directory, they will be run in lexicographical order, so you can control the ordering by adding a ‘00-‘ prefix.
Upvotes: 2