title: How to run Python code before every jupyter notebook kernel
body: <p>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:</p>
  
  <p>-- startup.py --</p>
  
  <pre class="lang-py prettyprint-override"><code>sc = "This is a spark connection"
  </code></pre>
  
  <p>I want to be able to have that code snippet run every time I open a kernel. I've found some stuff about the <a href="http://jupyter.readthedocs.io/en/latest/migrating.html#id9" rel="nofollow noreferrer">Jupyter Configuration File</a>, but it doesn't seem like variables defined there show up when I try to run </p>
  
  <pre class="lang-py prettyprint-override"><code>print(sc)
  </code></pre>
  
  <p>in a notebook. Is there a command-line option that I could use -- something like:</p>
  
  <pre><code>jupyter notebook --startup-script startup.py
  </code></pre>
  
  <p>or do I have to include something like</p>
  
  <pre class="lang-py prettyprint-override"><code>from startup import sc, sqlContext
  </code></pre>
  
  <p>in all of the notebooks where I want those variables to be defined?</p>