'How to run Python code before every jupyter notebook kernel

Suppose I have a code snippet that I'd like to run every time I open a jupyter notebook (in my case it's opening up a Spark connection). Let's say I save that code in a .py script:

-- startup.py --

sc = "This is a spark connection"

I want to be able to have that code snippet run every time I open a kernel. I've found some stuff about the Jupyter Configuration File, but it doesn't seem like variables defined there show up when I try to run

print(sc)

in a notebook. Is there a command-line option that I could use -- something like:

jupyter notebook --startup-script startup.py

or do I have to include something like

from startup import sc, sqlContext

in all of the notebooks where I want those variables to be defined?



Solution 1:[1]

A custom package or explicit loading is not needed (though might be preferred if you work with others): you can have auto-executed startup scripts ? https://stackoverflow.com/a/47051758/2611913

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tomas