Skip to content

Extension doesn't use MANAGE_PY_PATH #24214

Closed as not planned
Closed as not planned

Description

@oaahmad

Type: Bug

Behaviour

Overview

Unittest discovery fails for Django tests when following these instructions. At least, they fail for me in projects that use a Python virtualenv inside a dev container where manage.py is not at the root of the project. I'm not sure which of those factors (if any) is relevant. I was able to reproduce the issue by creating this minimal example project: https://github.com/oaahmad/sample-django-project .

Steps to reproduce:

  1. Clone this repo: https://github.com/oaahmad/sample-django-project
  2. Build the dev container
  3. Open the "Testing" view from the activity bar

Expected Behaviour

  • The test_sample test should be visible in the Testing view
  • There should be no errors in the Testing view

Actual Behaviour

  • There are no tests listed in the Testing view
  • The Testing view shows the error "Error discovering unittest tests"

Screenshot:
discovery-error

Additional Information

The tests run without issue from the integrated terminal with ./manage.py test --noinput (from the /home/ubuntu/sample-django-project/src directory). Here is the output:

(sample-virtual-env) ~/sample-django-project/src$ ./manage.py test --noinput
Found 1 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
I ran
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
Destroying test database for alias 'default'...

This is part of the output in Output -> Python (I will paste the full output under "Diagnostic Data"):

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

This makes it seem like the Python extension is trying to run the tests as regular unit tests instead of using manage.py. However, I specify MANAGE_PY_PATH in a .env file, and I set "python.envFile" to that file's path (see the example project).

I tried rebuilding the dev container "without cache" multiple times. I tried different Python versions, and different Ubuntu versions for the container. I tried with both a custom user and with the ubuntu user that comes with Ubuntu 24. I tried with both Python's builtin venv package and the virtualenv package. I got the same issue every time.

Diagnostic data

Output from Python in the Output panel

2024-09-30 22:40:26.920 [info] Discover tests for workspace name: sample-django-project - uri: /home/ubuntu/sample-django-project
2024-09-30 22:40:26.922 [warning] could not find a pixi interpreter for the interpreter at /home/ubuntu/sample-virtual-env/bin/python3
2024-09-30 22:40:26.953 [info] > . ~/sample-virtual-env/bin/activate && echo 'e8b39361-0157-4923-80e1-22d70d46dee6' && python ~/.vscode-server/extensions/ms-python.python-2024.14.1-linux-arm64/python_files/printEnvVariables.py
2024-09-30 22:40:26.953 [info] shell: bash
2024-09-30 22:40:26.975 [warning] could not find a pixi interpreter for the interpreter at /home/ubuntu/sample-virtual-env/bin/python3
2024-09-30 22:40:26.979 [info] > ~/sample-virtual-env/bin/python3 ~/.vscode-server/extensions/ms-python.python-2024.14.1-linux-arm64/python_files/testing_tools/unittest_discovery.py . test*.py
2024-09-30 22:40:26.979 [info] cwd: ./src
2024-09-30 22:40:27.151 [error] Error discovering unittest tests:
 Failed to import test module: sampleapp.tests_sample
Traceback (most recent call last):
  File "/usr/lib/python3.12/unittest/loader.py", line 394, in _find_test_path
    module = self._get_module_from_name(name)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/unittest/loader.py", line 337, in _get_module_from_name
    __import__(name)
  File "/home/ubuntu/sample-django-project/src/sampleapp/tests_sample.py", line 2, in <module>
    from django.contrib.auth.models import User
  File "/home/ubuntu/sample-virtual-env/lib/python3.12/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/home/ubuntu/sample-virtual-env/lib/python3.12/site-packages/django/contrib/auth/base_user.py", line 57, in <module>
    class AbstractBaseUser(models.Model):
  File "/home/ubuntu/sample-virtual-env/lib/python3.12/site-packages/django/db/models/base.py", line 129, in __new__
    app_config = apps.get_containing_app_config(module)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/sample-virtual-env/lib/python3.12/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "/home/ubuntu/sample-virtual-env/lib/python3.12/site-packages/django/apps/registry.py", line 138, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Extension version: 2024.14.1
VS Code version: Code 1.93.1 (Universal) (38c31bc77e0dd6ae88a4e9cc93428cc27a56ba40, 2024-09-11T17:20:05.685Z)
OS version: Darwin arm64 24.0.0
Modes:
Remote OS version: Linux arm64 6.10.4-linuxkit

  • Python version (& distribution if applicable, e.g. Anaconda): 3.12.3
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Venv
  • Value of the python.languageServer setting: Default
User Settings


envFile: "<placeholder>"

languageServer: "Pylance"

testing
• cwd: "<placeholder>"
• unittestArgs: "<placeholder>"
• unittestEnabled: true

Installed Extensions
Extension Name Extension Id Version
Code Spell Checker streetsidesoftware.code-spell-checker 3.0.1
GitLens — Git supercharged eamodio.gitlens 15.5.1
JavaScript Debugger ms-vscode.js-debug 1.93.0
Pylance ms-python.vscode-pylance 2024.9.2
Python ms-python.python 2024.14.1
Python Debugger ms-python.debugpy 2024.10.0
REST Client humao.rest-client 0.25.1
Table Visualizer for JavaScript Profiles ms-vscode.vscode-js-profile-table 1.0.9
System Info
Item Value
CPUs Apple M1 Pro (8 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, 3, 3
Memory (System) 16.00GB (0.07GB free)
Process Argv
Screen Reader no
VM 0%
Item Value
Remote Dev Container: Sample Dev Container @ desktop-linux
OS Linux arm64 6.10.4-linuxkit
CPUs unknown (8 x 0)
Memory (System) 7.66GB (4.16GB free)
VM 0%

Activity

  1. karthiknadig commented on Oct 1, 2024

    @karthiknadig
    Member

    oaahmad The .env file needs to be in the root of the project. It is in src/sampleapp/.env, it should be sibling of src.

    sample-django-project
    |- .devcontainer
    |- .vscode
    |- src
    |- .env
    
  2. oaahmad commented on Oct 1, 2024

    @oaahmad
    Author

    Karthik Nadig (@karthiknadig) Thank you, but that wasn't the issue. I tried moving the .env file to the root of the project (so it is a sibling of src) like you suggested. I still get the error, even after rebuilding the dev container.

    Here is my directory structure now:
    directory-structure

    And here is the same error still happening, even with that directory structure:
    error

    Besides, the .env file does not have to be at the root of the project according to the documentation I linked to: https://code.visualstudio.com/docs/python/testing#_django-unit-tests

    Note: By default, the Python extension looks for and loads .env files at the project root. If your .env file is not at the project root or you are using VS Code variable substitution, add "python.envFile": "${workspaceFolder}/<path-to-.env>" to your settings.json file. This enables the Python extension to load the environment variables from this file when running and discovering tests. Get more info about Python environment variables.

  3. karthiknadig commented on Oct 1, 2024

    @karthiknadig
    Member

    I just loaded your project it seems to be working:
    image

    image

    Not really sure why this is not working for you. The above screen shot is from loading your project from main branch, all i did was click Open in dev container.

  4. oaahmad commented on Oct 1, 2024

    @oaahmad
    Author

    I'm not sure either because that's not what happens for me 😕. I get the error in other projects too. Maybe the Python extension is caching something in a weird way? I will try on my personal Macbook and let you know how that goes (so far I have been trying on my work Macbook).

  5. oaahmad commented on Oct 1, 2024

    @oaahmad
    Author

    Karthik Nadig (@karthiknadig) I get the error on my personal laptop as well. So it happens on both. They are both Macbooks with Apple Silicon (M-series chips). So they are both on ARM if that matters. They are both on the latest version of macOS. My Python extension is installed inside my dev container if that makes a difference. I only activated my Python extension for the workspace (not globally), but I don't think that matters.

    I don't know what else I can look for, but it's just not working for me no matter what I try. Let me know if you think of something I can check. Thanks.

  6. karthiknadig commented on Oct 1, 2024

    @karthiknadig
    Member

    Since you are in a dev container could you just remove the .env and add that line to your DockerFile?

    ENV MANAGE_PY_PATH=/home/ubuntu/sample-django-project/src/manage.py
    
    
  7. oaahmad commented on Oct 1, 2024

    @oaahmad
    Author

    Karthik Nadig (@karthiknadig) I figured something out actually. I commented out my user-level settings for VS Code and it works now. I will try to figure out which setting the issue is and let you know so we can figure out if it's a bug or a user error on my part

  8. oaahmad commented on Oct 1, 2024

    @oaahmad
    Author

    Karthik Nadig (@karthiknadig) I figured out which setting causes the issue. It's a weird one. When I comment out this setting:

    "telemetry.telemetryLevel": "off",
    

    It works fine. When I enable the setting, it breaks. I tried multiple times in both states. So that seems like a bug with the extension or with VS Code. But at least I have a short term solution.

    Thanks for your help. Let me know if I should create a separate issue for that or if we should keep this one open.

  9. oaahmad commented on Oct 1, 2024

    @oaahmad
    Author

    Tagging Eleanor Boyd (@eleanorjboyd) because I think she did most of the work enabling test discovery for Django. Maybe she'll have some ideas about what's going on.

  10. 11 remaining items

  11. eleanorjboyd commented on Dec 16, 2024

    @eleanorjboyd

    Karthik Nadig (@karthiknadig) is this right? Are both settings required?

  12. karthiknadig commented on Dec 17, 2024

    @karthiknadig
    Member

    Since this feature was behind experiment "python.experiments.enabled": true is needed, but not sure why this is failing with "telemetry.telemetryLevel": "off". That should be investigated.

  13. github-actions commented on Jan 16, 2025

    @github-actions

    Because we have not heard back with the information we requested, we are closing this issue for now. If you are able to provide the info later on, then we will be happy to re-open this issue to pick up where we left off.

    Happy Coding!

  14. oaahmad commented on Jan 17, 2025

    @oaahmad
    Author

    Eleanor Boyd (@eleanorjboyd) Is there any more info I can provide, or was the info-needed label just left on by mistake?

  15. added
    needs spikeLabel for issues that need investigation before they can be worked on.
    and removed
    triage-neededNeeds assignment to the proper sub-team
    on Jan 17, 2025
  16. wudaudau commented on Apr 24, 2025

    @wudaudau

    Hi all,

    I encountered a similar issue, not exactly the same, but possibly related.

    The key error message I received while using TESTING in VSCode was:

    [error]   File "/Users/user/.vscode/extensions/ms-python.python-2025.4.0-darwin-arm64/python_files/unittestadapter/discovery.py", line 122
        if manage_py_path := os.environ.get("MANAGE_PY_PATH"):
                           ^
    SyntaxError
    

    In my case, the issue turned out to be a compatibility problem between VSCode and the Python version in my virtual environment. The current VSCode Python extension requires Python ≥ 3.8, and I was using an older version.

    The issue was resolved after upgrading my Python version to 3.11.

    Hope this helps someone facing a similar situation!

  17. eleanorjboyd commented on Sep 20, 2025

    @eleanorjboyd

    Hi! We now have a setting "python.terminal.useEnvFile" try setting this as well as as setting the python.envFile setting and see if that makes a different

  18. github-actions commented on Oct 21, 2025

    @github-actions

    Because we have not heard back with the information we requested, we are closing this issue for now. If you are able to provide the info later on, then we will be happy to re-open this issue to pick up where we left off.

    Happy Coding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-testingbugIssue identified by VS Code Team member as probable buginfo-neededIssue requires more information from posterneeds spikeLabel for issues that need investigation before they can be worked on.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions