Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Jupyter default imports

Tags:

How do I tell Jupyter (console and notebook) to import some Python packages by default? I would like to do this using only the .jupyter folder

like image 307
Neil G Avatar asked Feb 17 '16 01:02

Neil G


2 Answers

I personally couldn't find a way to do so only using the .jupyter folder. You have to specify the commands to be executed on startup in the .ipython folder anyway:

  1. Create ~/.ipython/profile_default/ipython_config.py if it not exists
  2. Add something like this:

    c = get_config() c.InteractiveShellApp.exec_lines = [     'import numpy as np\n'     'import scipy as sp\n'     'import matplotlib as plt\n' ] 

You can also specify any valid commands here, not only imports.

like image 82
Slippy Avatar answered Oct 04 '22 15:10

Slippy


A bit of background:

  • Jupyter provides the UI/environments such as the console and notebook. It defers to what it calls kernels for execution.
  • IPython provides the default (Python) kernel for Jupyter.
  • Jupyter configuration doesn't affect kernels directly, but each kernel may have its own configuration.

The IPython configuration resides in your .ipython directory. The quickest way to add code to run on startup of IPython (affects IPython sessions in the terminal and notebook) is to add startup files to your IPython profile.

  1. Create the default profile, if it doesn't exist already (it probably does):

    ipython profile create 
  2. Make a Python script ~/.ipython/profile_default/startup/whateveryouwant.py and add any imports or other commands in there that you would like to have ready whenever you start IPython. IPython will run this script and any others in that directory every time it starts up.

like image 37
minrk Avatar answered Oct 04 '22 15:10

minrk