↓ Skip to main content
  1. Tech & Lab/

Automatically Load Code in Jupyter Notebooks Using the IPython Startup Folder

Author
Yauheni Yakauleu
Short, practical articles on quant methods, alternative data, and applied AI. Code Insights, Touch IT LLC.

Automatically Load Code in Jupyter Notebooks Using the IPython Startup Folder
#

If you frequently work with Jupyter notebooks, you’ve probably found yourself writing the same import statements, utility functions, or configuration settings at the beginning of each notebook. This repetitive task can be eliminated by taking advantage of IPython’s startup folder functionality, which allows you to automatically load code every time you start a new notebook.

In this guide, we’ll explore how to set up and use the IPython startup folder in both Windows and Unix-based systems (macOS/Linux).

What Is the IPython Startup Folder?
#

The IPython startup folder is a directory where you can place Python scripts that will be executed automatically when you start an IPython kernel (which is what Jupyter notebooks use). This means any code, imports, or functions defined in these scripts will be available in all your notebooks without having to re-import or redefine them.

The startup folder is typically located at:

  • Unix (Linux/macOS): ~/.ipython/profile_default/startup/
  • Windows: C:\Users\YourUsername\.ipython\profile_default\startup\

Benefits of Using the Startup Folder
#

  • Save time: No need to repeatedly write common imports and functions
  • Standardize environments: Ensure all your notebooks have the same basic setup
  • Maintain consistency: Keep your plotting styles, pandas display options, and other configurations consistent
  • Reduce clutter: Keep your notebooks clean and focused on the actual analysis

Example Startup File
#

Here’s a simple example of what you might put in your startup file:

# 00-imports.py
print("Loading common imports...")

# Data manipulation
import numpy as np
import pandas as pd

# Visualization
import matplotlib.pyplot as plt
import seaborn as sns

# Set matplotlib to inline mode for Jupyter
%matplotlib inline

print("Imports loaded!")

This script would automatically load these libraries every time you start a new Jupyter notebook, saving you from having to write these import statements each time.

For more detailed information, check out the IPython documentation.

Related

Interactive Data Tables in Python Using itables

Interactive Data Tables in Python Using itables # When working with data in Python, displaying your dataframes in an interactive, user-friendly way can dramatically improve your analysis workflow. While pandas provides great functionality for data manipulation, its default display options are static and limited. This is where the itables library shines - it transforms your pandas DataFrames into fully interactive tables with sorting, filtering, and searching capabilities. In this guide, we’ll explore how to use itables in Python, from basic installation to advanced customization.