This repository is meant as a template for Python projects with full virtual environment and Python versioning support on VS Code.
Below you find instructions for setting it up.
- Python3;
- Git;
- VS Code;
- VS Code Python Extension (should auto install Pylance, Python Debugger, and Python Environments).
git clonethis repository;- Open the cloned folder on VS Code;
- On the Powershell Terminal, run
uv init --no-pin-pythonto start the project with support to python version changes, and to write a first.tomlfile to the folder; - Then run
uv add module1 module2 moduleN;;uv syncwith the list of dependencies needed for your project.
The final outlook of your folder should be like this:
my_project/ <-- Root workspace
├── .vscode
└── launch.json <-- Debugger setup
└── settings.json <-- General configurations
├── .venv/
├── pyproject.toml <-- Configures the whole project
├── scprof.ps1 <-- Easy debug with Scalene
├── skprof.ps1 <-- Easy debug with Snakeviz
└── src/
└── my_project/ <-- Only importable code lives here
└── __init__.py
├── tests/ <-- Tests sit outside source code
├── uv.lock <-- Virtual environment lock file
Aside from the Run and Debug functionality on VS Code, pytest allows for a better debugging experience on a dedicated tab. Just add it as a dependency and take the following conventions:
- Add
pytestby runninguv add pyteston the VS Code Terminal; - Save your files with the
test_prefix, e.g.test_main.pyto use auto-recognition; - Save the functions you need tested with a
test_prefix also; - (Optional) Keep all test files within a
tests/folder for ease of maintainability.
Now you can use the dedicated tab to run your tests. To use it on terminal, do as follows:
- Run
uv run pytest --pdbon the VS Code Terminal to debg;
VS Code has native support to pytest, though:
- Go to the
Testingtab on the left interface side (looks like a Beaker). If you can't see it, go to the Python tab instead, after that it should show up on the left. - Click
Initialize Testing Environment; - Over the top search bar you'll be prompted to
Configure Tests. Selectpytest. - Select the
tests/folder or any other wanted as root for test files.
Flamegraph is a VS Code Extension that works with py-spy. It is the closest option to MATLAB'S Profiler tool and will open a unique tab with bar data that shows performance data on hover. It will also show time spent per line on file.
- Run
uv add --dev py-spyto add the dependency; - Install the
Flamegraphextension; - Open the file and the drop down to execute, select
Flamegraph: Profile with py-spy.
Note that scalene will only profile code that runs for over 1 second or that uses at least 10 MB.
Steps for manual run:
- Run
uv add scalene; - Run
uv run scalene run /path/to/test_file.py --profile-allon yourtest_file.
You can also just run the utilitary script .\scprof.ps1 \path\to\file on your Terminal for convenience
This will create a .html file that opens on your browser. The design isn't great but works fine and with more overall detail than Flamegraph.
If you're on Windows, go to the Terminal and run .\skprof.ps1 \path\to\file.
- Add it to your environment by running
uv add --dev snakevizon Terminal; - Generate the profile binary using
cProfile:uv run python -m cProfile -o profile.prof /path/to/file.py; - Launch the UI with
uv run snakeviz profile.prof.