Skip to main content

Overview

This is a highly opininated guide on how to setup VScode for Python Development the way I personally like it, with a baseline Python project the way I also like it.

This doesn't mean I am correct, or someone with other preferences is incorrect, but this is what works for me.

Note this only works for pure Python projects. If your project requires C extensions or rust code, you will need to replace the flit builder with one that supports those things.

What is included in this skeleton

  1. My vscode setup for Python, including what plugins I use (in .vscode/settings.json)
  2. Examples for automated formatting with black and isort.
  3. Examples for automated testing with pytest.
  4. Examples for automatically testing the documentation examples with pytest.
  5. Examples for automated documentation building with mkdocs
  6. Examples for automatic linting with pylint
  7. Examples for automatic type checking with mypy
  8. Examples for automatic testing in multiple environments with tox
  9. Examples for Github Actions automation for running your tox test suite.

Software you need

Make sure the following applications are installed on your system globally. How you do this obviously varies depending on if you are using Windows, OSX or Linux.

  • Python3
  • Git
  • Visual Studio Code

Note on Debian/Ubuntu based distributions, you will need to apt-get install python3 python3-pip python3-venv git code.

How to setup

  • Copy the contents of this git repo to your new project directory.
  • Make a venv with python -m venv .venv.
  • Update the LICENSE file with your appropriate license information.
  • Replace this README.md file with an appropriate readme file.

You then need to install the development dependancies, and then the package in installable mode.

pip install -r requirements-dev.txt
flit install

My developer workflow

  1. Use vscode tools while developing, running tests out of vscode.
  2. View the "problems" tab.
  3. When ready to commit, I run tox from the command line.
  4. If tox returns no errors, I can commit and push.
  5. I can then verify github actions produces no errors on that push.

Use the package. It is fully installed and usable

$ python
>>> import python_vscode_template.arithmetic as ar
>>> ar.add_ints(2,3)
5

Format it with the black formatter

$ black .
All done! ✨ 🍰 ✨
6 files left unchanged.

Correct the import order with isort

$ isort .
Skipped 5 files

Run the tests (from the commmand line)

$ pytest
======================================= test session starts ========================================
platform linux -- Python 3.11.4, pytest-7.4.0, pluggy-1.2.0
rootdir: /home/dwg/CODE/Python-VSCode-Template
configfile: pyproject.toml
testpaths: tests
plugins: asyncio-0.21.1, anyio-3.7.1, cov-4.1.0
asyncio: mode=Mode.STRICT
collected 5 items                                                                                  

tests/test_arithmetic.py ....                                                                [ 80%]
tests/test_pokemon.py .                                                                      [100%]

======================================== 5 passed in 0.27s =========================================

Verify the documentation tests (from the command line)

$ pytest --doctest-modules src/
====================================== test session starts ======================================
platform linux -- Python 3.11.4, pytest-7.4.0, pluggy-1.2.0
rootdir: /home/dwg/CODE/Python-VSCode-Template
configfile: pyproject.toml
plugins: asyncio-0.21.1, anyio-3.7.1, cov-4.1.0
asyncio: mode=Mode.STRICT
collected 1 item                                                                                                                      

src/python_vscode_template/arithmetic.py .                                                [100%]                                          
====================================== 1 passed in 0.03s =======================================

Verify the type checking

$ mypy src/
Success: no issues found in 4 source files

Build the documentation and place the results in site/

mkdocs build

Start a documentation mini-webserver, that automatically updates

mkdocs serve

Run tests in multiple python environments

tox

From within vscode

You can verify the tests using the testing flask icon on the left.

  1. Brings up the testing dialog.
  2. Can run or debug individual tests, by selecting them and using the icons.
  3. Can rediscover, run all tests or debug all tests.

vscode testing example

On github

A small icon will have been added to github by github actions. It will be a ✅ if the tests are passing, a ❌ if they are failing and a 🟤 if the tests are still running.

You can click on it to view the logs and see what tests have passed, or what tests are still running.

github tests example

Stuff you will need to edit

  • tox.ini: Make sure envlist contains the versions of Python you want to test again. This example inclujdes
  • requirements-dev.txt: Make sure this includes any developer only dependancies.
  • requirements.txt: This should contain dependancies required to run the application or library. If you are making an application, you should pin each package to a specific version. If you are making a library, you should accept the minimum required version to allow users more choice. A library with a pinned version FORCES the user to install that version too, and is likely to clash with other libraries.
  • docs/: The entry document is index.md, but you should write the docs as you see fit.
  • src/: Update this with your package name.
  • tests/: This is where to write your tests. Files must start test_ to be picked up by tests/

Docstrings

See arithmetic.py for example docstrings. These are in Google Docstring Format.

Dist

flit build will put two files in the dist/ directory. A source file and a wheel. Both of these are directly usable by pip to install.

Publishing docs to Github pages

You can do this with mkdocs gh-pages. This will push the documentation in site/ to a github page. You will get the URL to it from the command.

Uploading to pypi

If you have never done this before, you need to make an account at Pypi. Then in Pypi - Manage Accounts, add 2FA and an API token.

Then repeat this for TestPypi and TestPypi - Manage Accounts, add 2FA and a API token.

Then edit .pypirc in your home directory, and make it similar to .pypirc-template in this repo, except with the appropriate real usernames and tokens.

My Global VSCode Settings

  • Press CTRL K then CTRL T, and select Dark+ as the theme (or your prefered theme).
  • Press CTRL , then make sure Auto Save is set to After Delay.
  • In the File menu, make sure Auto Save is ticked.

Metadata

Release files for python_vscode_template 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python_vscode_template 0.0.1
File Size Uploaded Provenance Actions
python_vscode_template-0.0.1.tar.gz 39.8 kB Without provenance Details for python_vscode_template-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python_vscode_template 0.0.1
File Interpreter ABI Platform Actions
python_vscode_template-0.0.1-py3-none-any.whl Python 3 none any Details for python_vscode_template-0.0.1-py3-none-any.whl

Total release size: 46.5 kB

Release files / python_vscode_template-0.0.1.tar.gz

Download URL python_vscode_template-0.0.1.tar.gz
Size 39.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f33ab4df0a37c07753adb7c3c21aed4fb8061cd34ef6712ac5b45a33bace5800
BLAKE2b-256 checksum
How to use checksums
4d74972e099f8f1b7e81815da670ccd04d73c49ddd092e1c5c9fe85423c14f88
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.31.0

Release files / python_vscode_template-0.0.1-py3-none-any.whl

Download URL python_vscode_template-0.0.1-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4fa707683078f61c840895a105561c9420c3c4e1403debc83efed49546fa8ba7
BLAKE2b-256 checksum
How to use checksums
af0a1177be644f4407d70f37e3388e2199a52e019e59c78898172b1525bf994a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.31.0

Security for python_vscode_template 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page