4

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?
neves
  • 33,186
  • 27
  • 159
  • 192

1 Answers1

1

Common dir structure:

  • app
    • __init__.py
    • yourappcode.py
  • tests (pytest looks for this)
    • __init__.py
    • test_yourunittests.py
  • server.py if you have one
  • .env
  • .coveragerc
  • README.md
  • Pipfile
  • .gitignore
  • pyproject.toml if you want
  • .vscode (helpful)
    • launch.json
    • settings.json

Or you could do one better. Ignore my structure and look at the some of famous python projects github page. Like fastAPI, Flask, asgi, aiohttp are some that I can think of right now

Also:

  • I think absolute imports are easier to work with compared to relative imports, I could be wrong though
  • vscode is able to use pytest. Make sure you have a testing extension. Vscode has a built in one im pretty sure. You can configure it to pytest and specify your test dir. You can also run your test from command line. If youre at the root, just running ‘pytest’ will recognise your tests dir if it’s named that by default. Also your actual test files need to start with prefix test_ i think.
  • The launch.json doesn’t need to be anything special. When you click on the settings button next to play button in the debug panel. Vscode will ask what kind of app is it. I.e If its a flask app, select python then select flask and it will auto generate a settings file which you can tweak however you want in order to get your app to run. I.e maybe you want to expose a different port or the commands to run your app are different
  • It sounds to me like you just need to spend a bit of time configuring vscode to your specific python needs. For example, you can use a virtualenv and linting in whichever way you want. You just need to have a settings.json file in the .vscode folder in your repo where you specify your settings. Configurations to specify python virtualenv and linting methods can be found online
md2perpe
  • 3,372
  • 2
  • 18
  • 22
HaffiM
  • 31
  • 1
  • 4