Skip to content

launching_and_configuring_ipython

Launching and Configuring the IPython Shell

Section titled “Launching and Configuring the IPython Shell”

IPython (Interactive Python) is a powerful interactive shell that offers significant improvements over the standard Python REPL (Read-Eval-Print Loop). It provides features like tab completion, object introspection, syntax highlighting, system shell access, and integration with various toolkits, making it a highly productive environment for development, data exploration, and interactive computing.

The most common and recommended way to start an IPython session is by simply typing ipython in your terminal or command prompt after installing it (usually as part of the Jupyter ecosystem or via pip install ipython).

$ ipython
Python 3.10.4 (main, Mar 31 2022, 08:41:55) [GCC 7.5.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.4.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]:

This command launches the enhanced IPython shell directly, replacing the standard >>> prompt with IPython’s numbered In [ ]: prompt.

It’s important to note that IPython is the core execution engine (kernel) for Jupyter Notebook and JupyterLab. While this tutorial focuses on the terminal-based IPython shell, much interactive Python work today happens within Jupyter environments, which provide a web-based interface combining code, text, and visualizations.

Understanding PYTHONSTARTUP (Advanced/Optional)

Section titled “Understanding PYTHONSTARTUP (Advanced/Optional)”

Python itself has an environment variable called PYTHONSTARTUP. If this variable is set to the path of a readable Python file, that file will be executed before the first prompt is displayed in interactive sessions started with the standard python command. This is a general Python feature, not specific to IPython.

Common uses for PYTHONSTARTUP include importing frequently used modules (like os or sys) or defining utility functions you want available in every standard Python interactive session.

Here’s an example of a simple startup.py file:

~/.config/python/startup.py
# (Path can be anywhere, this is just an example location)
import os
import sys
print("--- Custom startup script executed ---")
print(f"Python version: {sys.version.split()[0]}")
print(f"Current directory: {os.getcwd()}")
print("Modules 'os' and 'sys' imported.")
print("-------------------------------------")

To use this, you would set the PYTHONSTARTUP environment variable:

On Linux/macOS (add this to your .bashrc, .zshrc, or .profile):

export PYTHONSTARTUP="/home/user/.config/python/startup.py"

On Windows (Command Prompt - temporary):

set PYTHONSTARTUP="C:\Users\User\config\python\startup.py"

On Windows (PowerShell - temporary):

$env:PYTHONSTARTUP = "C:\Users\User\config\python\startup.py"

For permanent setting on Windows, use the System Properties -> Environment Variables dialog.

Now, when you type python (not ipython), the startup.py script will run before you get the >>> prompt.

Why Not Use PYTHONSTARTUP to Launch IPython?

Section titled “Why Not Use PYTHONSTARTUP to Launch IPython?”

While technically possible in the past to use PYTHONSTARTUP to launch IPython when typing python, this approach is no longer recommended and generally discouraged for several reasons:

  • Clarity: Typing ipython explicitly shows the intent to start IPython.
  • Consistency: The python command is expected by many scripts and tools to launch the standard Python interpreter. Changing this behavior can lead to confusion or break compatibility.
  • IPython’s Own Configuration: IPython has its own robust configuration system for running startup code, which is the preferred method.
  • Simplicity: Simply typing ipython is easier than setting up environment variables and startup scripts for this purpose.
Section titled “Recommended IPython Customization: Startup Files”

If you want to run specific code every time IPython starts, the best practice is to use IPython’s built-in startup file mechanism. Create Python files (e.g., 00-imports.py) in your IPython profile’s startup directory (usually ~/.ipython/profile_default/startup/). Any .py or .ipy files in this directory will be executed automatically when ipython launches.

Example ~/.ipython/profile_default/startup/00-imports.py:

print("--- IPython startup: Importing common libraries ---")
import numpy as np
import pandas as pd
print("NumPy (np) and Pandas (pd) imported.")
print("---------------------------------------------")

This method keeps standard python behavior intact while providing powerful customization specifically for your IPython sessions. For more details, consult the official IPython documentation on configuration.