configuring_ipython_jupyter
Configuring IPython and Jupyter
Section titled “Configuring IPython and Jupyter”While the default settings for IPython and Jupyter are excellent for most use cases, you might want to customize the environment, such as automatically importing commonly used modules or setting up specific configurations every time you start a session.
IPython Startup Files
Section titled “IPython Startup Files”IPython provides a simple way to run custom Python code every time it starts (including when it starts as a kernel for Jupyter). You can place Python scripts (.py) or IPython scripts (.ipy) in the IPython startup directory.
First, locate your IPython profile directory. The default profile is usually named profile_default. You can find the IPython directory using the command:
ipython locateInside this directory, navigate to the profile_default/startup/ folder (create it if it doesn’t exist). Any .py or .ipy file placed here will be executed in order when IPython starts.
For example, create a file named ~/.ipython/profile_default/startup/00-imports.py with the following content to automatically import NumPy and Pandas:
import numpy as npimport pandas as pd
print("NumPy and Pandas imported automatically.")Now, whenever you start IPython or a Jupyter kernel using this profile, np and pd will be available.
IPython Configuration Files
Section titled “IPython Configuration Files”For more complex configurations, IPython uses configuration files (e.g., ipython_config.py). You can generate a default configuration file using:
ipython profile create # Creates default profile if it doesn't existipython profile locate default # Shows path to default profile# Now edit ipython_config.py in that directoryInside ipython_config.py, you can uncomment and modify various settings. For instance, to change the auto-indent setting:
c = get_config()
# Turn off autoindentc.TerminalInteractiveShell.autoindent = FalseJupyter Configuration Files
Section titled “Jupyter Configuration Files”Similarly, Jupyter components like the Notebook server have their own configuration files (e.g., jupyter_notebook_config.py or jupyter_server_config.py for newer versions). Generate and locate it using:
jupyter notebook --generate-config # Or jupyter server --generate-configYou can configure options like the default port, browser behavior, password protection, etc., in this file.
Using startup files is generally recommended for simple customizations like imports, while configuration files offer more fine-grained control over IPython and Jupyter behavior.
Note: While setting the PYTHONSTARTUP environment variable (as mentioned in older guides) can execute a script upon starting the standard Python interpreter, it’s less common for customizing IPython/Jupyter environments compared to using profile startup files or configuration files.