Repository navigation
Description
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:
- Clone this repo: https://github.com/oaahmad/sample-django-project
- Build the dev container
- Open the "Testing" view from the activity bar
Expected Behaviour
- The
test_sampletest 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"
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.languageServersetting: 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
karthiknadig commentedon Oct 1, 2024
oaahmad The
.envfile needs to be in the root of the project. It is insrc/sampleapp/.env, it should be sibling ofsrc.oaahmad commentedon Oct 1, 2024
Karthik Nadig (@karthiknadig) Thank you, but that wasn't the issue. I tried moving the
.envfile to the root of the project (so it is a sibling ofsrc) like you suggested. I still get the error, even after rebuilding the dev container.Here is my directory structure now:

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

Besides, the
.envfile 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-testskarthiknadig commentedon Oct 1, 2024
I just loaded your project it seems to be working:

Not really sure why this is not working for you. The above screen shot is from loading your project from
mainbranch, all i did was click Open in dev container.oaahmad commentedon Oct 1, 2024
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).
oaahmad commentedon Oct 1, 2024
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.
karthiknadig commentedon Oct 1, 2024
Since you are in a dev container could you just remove the
.envand add that line to your DockerFile?oaahmad commentedon Oct 1, 2024
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
oaahmad commentedon Oct 1, 2024
Karthik Nadig (@karthiknadig) I figured out which setting causes the issue. It's a weird one. When I comment out this setting:
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.
oaahmad commentedon Oct 1, 2024
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.
11 remaining items
eleanorjboyd commentedon Dec 16, 2024
Karthik Nadig (@karthiknadig) is this right? Are both settings required?
karthiknadig commentedon Dec 17, 2024
Since this feature was behind experiment
"python.experiments.enabled": trueis needed, but not sure why this is failing with"telemetry.telemetryLevel": "off". That should be investigated.github-actions commentedon Jan 16, 2025
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!
oaahmad commentedon Jan 17, 2025
Eleanor Boyd (@eleanorjboyd) Is there any more info I can provide, or was the
info-neededlabel just left on by mistake?wudaudau commentedon Apr 24, 2025
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:
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!
eleanorjboyd commentedon Sep 20, 2025
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
github-actions commentedon Oct 21, 2025
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!