microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.34k stars 1.19k forks source link

New Django test feature: can't find '__main__' module #24405

Closed khamaileon closed 2 weeks ago

khamaileon commented 3 weeks ago

Type: Bug

Hello team vscode-python, @eleanorjboyd ,

I finally had some time to try the new Django testing in VSCode. Unfortunately, I couldn't get it to work.

Here the log output:

2024-10-24 15:18:07.895 [info] 

2024-10-24 15:18:08.535 [info] Discover tests for workspace name: my-project - uri: /Users/me/workspace/company/my-project
2024-10-24 15:18:08.535 [info] Running discovery for unittest using the new test adapter.
2024-10-24 15:18:08.540 [info] Discovering unittest tests for workspace /Users/me/workspace/company/my-project with arguments: /Users/me/.vscode/extensions/ms-python.python-2024.16.1-darwin-arm64/python_files/unittestadapter/discovery.py,--udiscovery

2024-10-24 15:18:08.540 [info] > ~/.virtualenvs/my-project/bin/python ~/.vscode/extensions/ms-python.python-2024.16.1-darwin-arm64/python_files/unittestadapter/discovery.py --udiscovery
2024-10-24 15:18:08.541 [info] cwd: .
2024-10-24 15:18:08.599 [info] MANAGE_PY_PATH is set, running Django discovery with path to manage.py as: $/Users/me/workspace/company/my-project/src

2024-10-24 15:18:08.599 [info] Running Django tests with command: ['/Users/me/.virtualenvs/my-project/bin/python', '/Users/me/workspace/company/my-project/src', 'test', '--testrunner=django_test_runner.CustomDiscoveryTestRunner']

2024-10-24 15:18:08.621 [error] /Users/me/.virtualenvs/my-project/bin/python: can't find '__main__' module in '/Users/me/workspace/company/my-project/src'

2024-10-24 15:18:08.621 [info]

My settings.json:

{
  "editor.codeActionsOnSave": {
    "source.organizeImports": "explicit"
  },
  "editor.formatOnSave": true,
  "editor.rulers": [100],
  "isort.args": ["--profile", "black"],
  "notebook.codeActionsOnSave": {
    "source.organizeImports": "explicit"
  },
  "notebook.formatOnSave.enabled": true,
  "python.envFile": "${workspaceFolder}/.env.local",
  "python.experiments.optInto": ["pythonTestAdapter"],
  "python.testing.pytestEnabled": false,
  "python.testing.unittestArgs": [],
  "python.testing.unittestEnabled": true
}

My launch.json:

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python Debugger: Django",
      "type": "debugpy",
      "request": "launch",
      "args": ["runserver", "0.0.0.0:8000"],
      "django": true,
      "autoStartBrowser": true,
      "program": "${workspaceFolder}/src/manage.py"
    }
  ]
}

cd src && python manage.py test works well in the VS code terminal.

Since I wasn't sure that the problem wasn't due to the fact that I had a src subdirectory in my project, I redid the test with a new Django project, and I have the same issue.

Extension version: 2024.18.0 VS Code version: Code 1.95.2 (Universal) (e8653663e8840adaf45af01eab5c627a5af81807, 2024-11-07T11:07:22.054Z) OS version: Darwin arm64 23.5.0 Modes:

User Settings

``` languageServer: "Pylance" testing • unittestArgs: "" • unittestEnabled: true experiments • optInto: ["pythonTestAdapter"] ```

Installed Extensions |Extension Name|Extension Id|Version| |---|---|---| |Black Formatter|ms-python.black-formatter|2024.4.0| |CodeLLDB|vadimcn.vscode-lldb|1.11.1| |Dev Containers|ms-vscode-remote.remote-containers|0.388.0| |Docker|ms-azuretools.vscode-docker|1.29.3| |ESLint|dbaeumer.vscode-eslint|3.0.10| |Flake8|ms-python.flake8|2023.10.0| |gettext|mrorz.language-gettext|0.5.0| |GitHub Copilot|GitHub.copilot|1.243.0| |GitHub Copilot Chat|GitHub.copilot-chat|0.22.2| |isort|ms-python.isort|2023.10.1| |JavaScript Debugger|ms-vscode.js-debug|1.95.2| |JavaScript Debugger Companion Extension|ms-vscode.js-debug-companion|1.1.3| |Jupyter|ms-toolsai.jupyter|2024.10.0| |Jupyter Cell Tags|ms-toolsai.vscode-jupyter-cell-tags|0.1.9| |Jupyter Keymap|ms-toolsai.jupyter-keymap|1.1.2| |Jupyter Notebook Renderers|ms-toolsai.jupyter-renderers|1.0.21| |Jupyter Slide Show|ms-toolsai.vscode-jupyter-slideshow|0.1.6| |Prettier - Code formatter|esbenp.prettier-vscode|11.0.0| |Pylance|ms-python.vscode-pylance|2024.11.1| |Python|ms-python.python|2024.18.0| |Python Debugger|ms-python.debugpy|2024.12.0| |Table Visualizer for JavaScript Profiles|ms-vscode.vscode-js-profile-table|1.0.10| |Vue - Official|Vue.volar|2.1.10| |XML|redhat.vscode-xml|0.27.1|
System Info |Item|Value| |---|---| |CPUs|Apple M3 Pro (11 x 2400)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
webgl: enabled
webgl2: enabled
webgpu: enabled
webnn: disabled_off| |Load (avg)|2, 2, 2| |Memory (System)|18.00GB (0.50GB free)| |Process Argv|--crash-reporter-id 6a77dd1b-2544-406f-98a9-34d5a796c1c9| |Screen Reader|no| |VM|0%|
A/B Experiments ``` vsliv368cf:30146710 vspor879:30202332 vspor708:30202333 vspor363:30204092 vscod805cf:30301675 binariesv615:30325510 vsaa593cf:30376535 py29gd2263:31024239 vscaat:30438848 c4g48928:30535728 azure-dev_surveyonecf:30548226 962ge761:30959799 pythongtdpath:30769146 pythonnoceb:30805159 asynctok:30898717 pythonmypyd1:30879173 h48ei257:31000450 pythontbext0:30879054 cppperfnew:31000557 dsvsc020:30976470 pythonait:31006305 dsvsc021:30996838 724cj586:31013169 dvdeprecation:31068756 dwnewjupytercf:31046870 impr_priority:31102340 nativerepl2:31139839 refactort:31108082 pythonrstrctxt:31112756 cf971741:31144450 iacca1:31171482 notype1:31157159 5fd0e150:31155592 dwcopilot:31170013 ```
eleanorjboyd commented 2 weeks ago

Hi! From this MANAGE_PY_PATH is set, running Django discovery with path to manage.py as: $/Users/me/workspace/company/my-project/src it looks like you set the MANAGE_PY_PATH to a folder? If you could get it to point at your manage.py path for the django project you want to run then that should help.

khamaileon commented 2 weeks ago

Hi @eleanorjboyd,

Indeed, the issue was that the MANAGE_PY_PATH was set to a folder, instead of pointing directly to the manage.py file of the Django project I wanted to run.

Additionally, I had to add the following parameter to make it work in a subfolder: "python.testing.cwd": "${workspaceFolder}/src"

Thanks for the clarification!