Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

codeA guest post by Anton I. Sipos, who has been programming computers since they were 8 bits old. He is a happy participant in the open source community, including contributions to IPython itself. His open source contributions can be found at, short quips at @aisipos, and longer musings at Email him at

Do you find yourself performing lots of “setup” in your interactive Python sessions? If you find yourself performing repeated tasks often, such as importing commonly used types from the standard library or your own code, IPython provides profiles as a mechanism to automate this for

IPython ships with some built-in profiles. You can see what profiles are available to you with this command:

To use any of the above profiles, start IPython with:

Running IPython without specifying a profile will automatically start the profile named default. As demonstrated above, you can run another profile by specifying --profile when starting IPython.

Customizing a profile

IPython profiles can be customized by placing plain Python scripts in the startup folder of a profile. The files here will be executed first before the IPython shell starts taking commands. You can perform anything as elaborate as you like within these startup scripts. If you want these startup files run by default, place them in the startup folder of the default profile, which can be found at the location:

In my default profile I use a file to perform my most commonly used imports so they are available every time I start IPython automatically:

I use these modules very frequently, so it is convenient to have them always available to me in an interactive session. Importing these modules adds only a negligible increase in startup time. Note if you want to use this startup script yourself you will need to install numpy and requests, or else delete them from the list.

To create a separate, new profile, execute from the command line:

Sharing code between profiles

What if you needed to share initialization code between separate profiles? Unfortunately there is no inheritance mechanism for profiles. However, there are ways to accomplish this. A technique I have used is to just use filesystem hardlinks to duplicate an initialization script across multiple profiles.

Differences from the PYTHONSTARTUP environment variable

The plain Python interpreter has a feature where it will execute a Python script specified in the PYTHONSTARTUP environment variable for each interactive session. The IPython interpreter will also execute this script as well. What then are the advantages of using IPython profiles? I’ll list a few here:

  • While there can be only one PYTHONSTARTUP script defined at a time, you can have multiple profiles available. I often create a separate IPython profile for individual projects that I am working on. In the startup scripts of a project profile, I will perform project specific things, like automatically connecting to my development database instance.
  • Profiles allow multiple startup scripts within a single profile. This can be handy if your startup code is very large. As mentioned earlier, you could also use filesystem links to place shared startups.
  • Profiles are also used for more than just startup customization. Separate profiles contain separate command histories as well. This can be a help when working on multiple projects, to keep the history of your commands relevant only to what you are working on.

Do note, however, that IPython will execute both the PYTHONSTARTUP file and the chosen profile startup files, so it can be useful to use both together. Putting simple commands into your PYTHONSTARTUP file is a good way to have some commands run no matter what profile is active.


IPython provides %who and %whos magic commands to show which variables are in the interactive namespace. If you rely on this, you need to note that variables loaded in the startup section of your profiles will not show up with these commands. This is an explicit design decision. However, this hiding will be made optional in future versions of IPython. You can still use the built-in function locals() to see what is available, but it has a more cluttered output and is thus a little less convenient.


IPython profiles are a very convenient way to make your interactive IPython sessions more useful, and even more enjoyable. I hope this overview has given you some ideas in how to make your IPython environment a more productive experience.

Look below for some great Python and IPython books from Safari Books Online.

Not a subscriber? Sign up for a free trial.

Safari Books Online has the content you need

Python for Data Analysis is packed with practical cases studies that show you how to effectively solve a broad set of data analysis problems, using several Python libraries.
Mining the Social Web, 2nd Edition combines popular and useful social web data with analysis techniques and visualization to help you find the needles in the social haystack that you’ve been looking for—as well as many you probably didn’t even know existed.
Think Python takes you through the Python language one step at a time, beginning with basic programming concepts before moving on to functions, recursion, data structures, and object-oriented design.
Python Cookbook, 3rd Edition is your ticket if you need help writing programs in Python 3, or want to update older Python 2 code. Packed with practical recipes written and tested with Python 3.3, this unique cookbook is for experienced Python programmers who want to focus on modern tools and idioms.

Tags: built-in profiles, IPython, IPython Profiles, profiles, PYTHONSTARTUP,

Comments are closed.