Search code examples
visual-studio-codepytestpylintvscode-debuggervscode-python

VSCode: how to structure a simple python package with few modules and tests, debugging and linting?


I'm having more trouble than I'd like to admit to structure a simple project in Python to develop using Visual Studio Code.

How should I structure in my file system a project that is a simple Python package with a few modules? Just a bunch of *.py files together. My requisites are:

  1. I must be able to step debug it in vscode.
  2. It has a bunch of unit tests using pytest.
  3. I can select to debug a specific test from vscode tab and it must stop in breakpoints.
  4. pylint must not show any false positives.
  5. The test files must be in a different directory of the main module files.
  6. I must be able to run all the tests from the console.
  7. The module is executed inside a virtual environment using python standard lib module venv
  8. The code will use type hints

I may use another linter, even another test framework.

Nothing fancy, but I'm really having trouble to get it right. I want to know:

  • How should I organize my subdirectory: a folder with the main files and a sibling folder with the tests? Or a subfolder with the code and a subsubfolder with the tests?
  • Which dirs must have a init.py file?
  • How the tests should import the files from the module? Should I use relative imports?
  • Should I create a pytest.ini file?
  • Should I create a .env file?
  • What's the content of my launch.json the debugger file config in vscode?

Solution

  • Some years later, I now think that the best option is to use Poetry package and environment manager. You must start your projects just using poetry new <my-package> that it will create the structure for you.

    Now everything just works.