Giter VIP home page Giter VIP logo

spyder_line_profiler's Introduction

spyder_line_profiler

Description

This is a plugin to run the python line profiler and memory_profiler from within the spyder editor.

The code is an adaptation of the profiler plugin integrated in spyder.

Note: This plugin is compatible with spyder version 2.3.X. Here is the version for spyder 3.0.

Install instructions

Put the files p_lineprofiler.py, p_memoryprofiler.py, widgets/lineprofilergui.py, and widgets/memoryprofilergui.py in the directory spyderplugins/ from the spyder installation.

For example on Linux this should be /usr/lib/python2.7/dist-packages/spyderplugins/ or equivalent.

Line profiler requirements: The line_profiler module and the kernprof script have to be installed and accessible on the system. See the official line_profiler website for instructions.

Memory profiler requirements: The memory_profiler module has to be installed. See the official memory_profiler website for instructions.

Usage

Add a @profile decorator to the functions that you wish to profile then press Shift+F10 (line profiler default) or Ctrl+Shift+F10 (memory profiler default) to run the profiler on the current script, or go to Run > Profile line by line or Run > Profile memory line by line.

The results will be shown in a dockwidget, grouped by function. Lines with a stronger color take more time to run (line profiler) or have the largest increments in memory usage (memory profiler).

Screenshot

Line profiler:

image

spyder_line_profiler's People

Contributors

blink1073 avatar cmeeren avatar divenex avatar nodd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

spyder_line_profiler's Issues

Line color relative to function ?

Should the line color be relative to each function or to the max global time ?

If one function calls another, it is harder to see the bad line in the callee as the caller will always take more time.
Maybe the colors can be relative to the function but with a different color than red (computed from a hash of the function name rather than random so that the color don't change between calls)

filter py3 & finding anaconda python

Hi,
this really looks like it should be a standard plugin to spyder.
it's an awesome functionality to have in spyder!!
Very easy to use once it's up and running.

One possible further step could be non-intrusive marking of the @Profile.
Maybe it would be possible to make a mark similar to "breakpoint" in debugging?

Here is what I had to do to get it running on my computer.
I had 2 issues (both with memory_profiler, the line_profiler worked out of the box):

First of all I run anaconda 2.3.0 py34 64-bit on win8.1
I installed line_profiler & psutil with conda. The memory_profiler was installed with pip, since conda did not have a version in the repository (also the ones found on binstar seemed to be old).

As for getting into spyder I just copied the 4 relevant files in this repository to the spyder plug-in location as per the instructions.
#1 (this library). in memoryprofilergui changed line:

from: stuff = filter(None, l.split(' '))
to: stuff = list(filter(None, l.split(' ')))
I guess this is a py2 vs py3 thing, and I took the easy way out with making a list to go back to py2 behaviour, I did not test tuple but I guess that works as well.
#2 (more spyder itself) - it did not find the right python.exe.

The line_profiler worked out of the box.
After a while I tracked it down to that for the line_profiler it started the program "kernprof",
this one I only have one version of, and that's in the anaconda installation.
The memory_profiler started the program "python", which it finds with
spyderlib.utils.programs.find_program('python').

I know that I have a pretty scattered computer with regards to python installs, but it normally works out OK.
I have 3 different base pythons installed where anaconda and a regular python are 2 of them.
There are also a couple of virtual envs from the "small python" base.

Anyways when spyderlib finds the "python" progam it finds the path 'C:\Python34\python.exe'
If I put in IPython I get 'C:\Data\Anaconda3\Scripts\ipython.exe',
in other words - although spyder is installed via anaconda, it still does not recognize this...

When I look into my os.environ['PATH'] the non-anaconda paths are before the anaconda paths in a print-out.
So I guess it finds the non-anaconda first and is satisfied with that answer.

Maybe one can use the 'SPYDER_PARENT_DIR' environ_var to select the right python install.
For myself I just put in the hard path, i.e. line no 264 executable = "c:..\Anaconda3\python.exe" to make it work on my computer.

Once more, thanks for a great integration into spyder!!

bootstrap.py crash

The plugin works while launched as a script from within spyder, but crashes spyder at launch when integrated as a plugin.

(Solution inside) Fails on Windows because kernprof.exe is run with python

If run in Windows, fails with the following output:

  File "C:/Anaconda/Scripts/kernprof.exe", line 1
SyntaxError: Non-ASCII character '\x90' in file C:/Anaconda/Scripts/kernprof.exe on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

This is because kernprof.exe is run using python: Line 264 of lineprofilergui.py is

executable = '{0} {1}'.format(sys.executable, script_path)

which means executable is set to (in my case) C:\Anaconda\pythonw.exe C:\Anaconda\Scripts\kernprof.exe, whereas the correct would be only C:\Anaconda\Scripts\kernprof.exe.

Solution: Replace this line with

executable = script_path

Tested and works perfectly.

line_profiler GUI does not expand all functions

Hi,

When I'm profiling a function that it is inside of an imported class or library, sometimes the line_profiler GUI does not allow me to expand the function, even the output of line_profiler is OK. Other times it works fine.

This happens in both Windows and Ubuntu, so the problem should be in the code, but I don't see what could be the problem (I don't use ASCII characters for example).

I attach a figure to clarify my problem.

line_profiler

Thank you!

Line Profiling not showing up

Hi

I've got a working version of line_profiler. I follower the instructions and copied pline_profiler.py to spyderpluginsand lineprofilergui to the widget folders.
I cannot, however, see any option for line profiling once I'm in Spyder.

Coloration of lines by percent

Code lines with a high time should have their background set in red, like in Matlab.

The line with the highest time should be in red, and the alpha decreases when the time gets lower.

This should be an option since it may render strangely if the background is not white.

Fails if path to file contains spaces (Windows)

Line profiler fails to find the file and displays an error message when the path to the file being profiled contains a blank space...i.e., "C:\Python\test prog\test.py" will fail. If I move test.py to the path: "C:\Python\test\test.py" it works fine.

Seems like it should be a simple fix due to incorrect handling/joining of path strings for Windows systems?

-b flag for kernprof.py

What is the use of the -b flag for kernprof.py ? profile seems to be added to the namespace even without it.

Blank line before decorator

There's a blank line before the @profile decorator in the results, but it seems that it is not present on all systems.

Group by file or by function ?

Is it really necessary to group by file before grouping by function ? The original line_profiler groups by function only.
The @profile decorator is rather function-oriented. I thought there was a way to enable/disable the profiler globally but I can't find it any more. In this case grouping by files may be better.

If grouping by function, a solution may be to prepend the function with the file name and line number, the sorting by function would also sort by name. The problem being that the path can be very long.

No option 'use_colors' in section: 'lineprofiler'

At first launch, if the preference window is not opened first, the following exception is raised:

Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/spyderplugins/p_lineprofiler.py", line 134, in run_lineprofiler
    self.analyze(self.main.editor.get_current_filename())
  File "/usr/lib/python2.7/dist-packages/spyderplugins/p_lineprofiler.py", line 151, in analyze
    use_colors = self.get_option('use_colors')
  File "/usr/lib/python2.7/dist-packages/spyderlib/plugins/__init__.py", line 239, in get_option
    return CONF.get(self.CONF_SECTION, option, default)
  File "/usr/lib/python2.7/dist-packages/spyderlib/userconfig.py", line 307, in get
    raise cp.NoOptionError(option, section)
ConfigParser.NoOptionError: No option 'use_colors' in section: 'lineprofiler'

It seems that the option is not created when the plugin is loaded.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.