问题
Is there a way to ensure that jupyter notebook always starts with either:
1) Certain packages imported and/or 2 Certain options set
I don't want to have to type the same things everytime at the the top of each notebook I run - e.g. always using numpy or pandas.
Additionally, I always want to be able to see multiple output per cell. I use the following code to let this work just fine, but I want this saved as some sort of template, that doesn't require manual effort from me to type each time.
Thanks!
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
回答1:
First, you find where the startup folder is located.
# on Jupyter notebook
import IPython
IPython.paths.get_ipython_dir()
On Windows, the response is u'C:\\Users\\yourname\\.ipython', while on Linux ~/.ipython.
In that location, there are profile folders. At least, there is a profile_default on your computer. One startup folder exists in each profile folder.
You put a python script file in that folder (my case: C:/Users/myname/.ipython/profile_default/startup).
I name my script file 00-first.py, and put this code in it:
import numpy as np
import pandas as pd
When I start the Jupyter notebook server with the default profile, the startup script will be executed prior to opening a Jupyter notebook.
On a newly open Jupyter notebook, you can use numpy and pandas (as np, pd) without importing them first.
print(np.pi) #3.141592...
来源:https://stackoverflow.com/questions/46737950/always-have-jupyter-notebook-load-with-certain-options-packages