Neil G
Neil G

Reputation: 33252

Configuring Jupyter default imports

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

Upvotes: 23

Views: 12006

Answers (2)

Slippy
Slippy

Reputation: 495

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.

Upvotes: 23

minrk
minrk

Reputation: 38638

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.

Upvotes: 23

Related Questions