This post will help you to use line_profiler for python (2.5.x) as well as python3 (python 3.x) line_profiler will help you to profile the time taken to execute each individual lines of code. The profiler is implemented in C via Cython, an optimizing static compiler for Python programming language as well as Cython programming language (based on Pyrex).

1. Install mercurial (Skip this step if using Official line_profiler from rkern)

sudo apt-get install mercurial

Note: rkern’s Official line_profiler has recently added Python3 support in 1.0 release

2. Clone the line_profiler repository


git clone

Note: The old repo was moved from BitBucket link to GitHub link

You can directly jump to Step 3 now, as Python3 support is provided by rkern’s line_profiler šŸ™‚

Note: If this doesn’t work for you properly (Seems that they have kept support for 3.x on hold for now). You can clone this repository which is a fork of the above with 3.x compatibility.

hg clone

or you can clone this one too:

hg clone

3. Navigate into the line_profiler clone folder

cd line_profiler

4. Now you need to Install Cython (for python) / Cython3 (for python 3.x)

sudo apt-get install cython
sudo apt-get install cython3

5. Install line_profiler by running:

a) line_profiler for python

python install

b) line_profiler for python3

python3 install

6. Go to the folder where your .py file is located and run the following command: -l; python3 -m line_profiler

“ -l” will create an instance of LineProfiler and it will be inserted in __builtins__ namespace with the name ‘profile’. It is written to be used as a decorator. Therefore you can decorate any function you want to profile using @profile Here’s an example:

    def add(a,b):

The default behavior of kernprof is to compile the .py into a binary file like ‘’. To display the result you need to immediately run the command “python3 -m line_profiler” (for python3) or “python -m line_profiler” (for python) for displaying the formatted results. Reference: 1.

Let the Windows be open, and feel the Freedom.


5 thoughts on “using line_profiler with python

  1. Did you follow the steps specific for python3 ? maybe you ran ‘python install’

    Please try ‘python3 install’ and also install cython3 before this.

    Hope it works fine for you then šŸ™‚


    1. maybe I should have mentioned that I’ve actually read your instructions. šŸ˜‰

      You forgot to describe how to patch the line_profiler code to work with python3. I’m just wondering that nobody else had these problems.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s