python-line-profiler
is a vscode extension to configure and run line_profiler, then visualize the results in the editor.
This is a very buggy beta. If you run into a problem, look at the known issues first. If your issue still isn't resolved, or you have an outstanding question, submit an issue
If you find this useful and would like to see continued development, buy me a beverage by hitting the donate button at the top.
To use python-line-profiler
:
- Highlight a function in the editor, right click and choose
Python Line Profiler: Register Function
- In the file explorer, select a script to profile the registered function, right click and choose
Python Line Profiler: Run Script
- Once the script is done running, results will be displayed as text decorations showing the number of times each line was called along with total run times. Each line is highlighted in red, with brighter red indicating longer run times relative to other lines.
In the above example, line 92 is the brightest and therefore the most time consuming line of code.
A test script can be used to profile the code if it uses unittest
.
This extension requires that Python 3.7+ is installed. It is recommended that the vscode Python extension (ms-python) is installed.
One important thing to note is taht this extension installs libraries into the Python environment configured for the workspace. This is required since it runs scripts under the hood for profiling. It is expected that all the required packages for running a script have been installed into the configured environment. However, to run the code a few packages are needed and will be installed into the environment:
- fastapi
- line-profiler
To ensure dependency conflicts are not injected into the environment, specific versions are not pinned.
python.profile.lineColorOn: true
- Turn the line colors on (true
, default) or off (false
).
python.profile.lineColor: "100,0,0"
- A 3-tuple RGB value. Default is "100,0,0"
.
This is a very buggy beta. There are probably a lot of issues. Known issues are:
- Registering multiple functions with the same name with only show visualizations on one of the functions
- Visualization seem to disappear when multiple scripts are shown side by side when switching tabs.
pytest
files cannot be used as test scripts (probably).- No other testing packages can be used either.
- Running a script that also contains a profiling function, then switching to using a different script, returns no results.
Possible issues based on the current code. If you run into these issues, please open an issue:
- Import aliasing (
import numpy as np
) could cause issues. - Relative imports with more than one level of namespace packages will likely cause problems.
This is a buggy beta. If you're trying it out, give us some feedback in the issues!
Donations are appreciated to support the continued development of this project and future projects like it. A recurring donation can be set up using Libra Pay by clicking on the button below.