You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django测试模块导入失败求助(Python3.8环境)

Fixing ImportError for Django Tests in Nested Project Structure

Looks like your Django project structure is nested incorrectly—that's the root cause of your ImportError, not Python 3.8 (though 3.8's stricter import rules might be amplifying the issue). Let's break down what's wrong and fix it step by step:

1. Your Project Structure is Messed Up

Looking at your file tree, you've got a pages folder inside another pages folder, plus manage.py is stuck in the inner directory instead of the project root. Django relies on a strict directory hierarchy to find modules like tests, so this nesting is confusing the import system completely.

Correct Django Project Layout

First, rearrange your files to match the standard Django structure:

.
├── Pipfile
├── Pipfile.lock
├── pages_project/  # Project config root directory
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── pages/  # Your actual app directory
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations/
│   │   └── __init__.py
│   ├── models.py
│   ├── templates/
│   │   ├── about.html
│   │   ├── base.html
│   │   └── home.html
│   ├── tests.py  # Keep your test code right here
│   ├── urls.py
│   └── views.py
├── manage.py
└── db.sqlite3

How to Rearrange:

  • Move manage.py, db.sqlite3, and pages_project/ from the inner pages folder to your project's root directory (same level as the top Pipfile).
  • Delete the inner pages/pages/ subfolder, then merge its contents (like views.py, urls.py) into the outer pages/ app directory (don't overwrite existing files like apps.py unless you're sure they're incorrect).
  • Move the templates/ folder to either the project root or inside the pages/ app directory (both are valid—just pick one and stick with it).

2. Verify App Registration in Settings

Open pages_project/settings.py and double-check your pages app is listed in INSTALLED_APPS:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'pages',  # Confirm this line exists!
]

3. Keep Your Test File in the Right Spot

Your existing test code is perfectly correct—just make sure it lives at pages/tests.py (not in some nested location):

from django.test import SimpleTestCase

class PagesTests(SimpleTestCase):
    def test_home_page_status_code(self):
        response = self.client.get('/')
        self.assertEqual(response.status_code, 200)

    def test_about_page_status_code(self):
        response = self.client.get('/about/')
        self.assertEqual(response.status_code, 200)

4. Run Tests Correctly

Navigate to your project root (where manage.py is now located) and execute:

python3 manage.py test pages

Specifying pages tells Django to only run tests for that app, which avoids any leftover confusion from the old messed-up structure.

Bonus: Using a Separate Tests Directory (If You Want)

If you prefer splitting tests into multiple files later, create a tests/ directory inside pages/ with this structure:

pages/
├── tests/
│   ├── __init__.py
│   └── test_views.py  # Your test code goes here

Django will automatically detect this as a valid test module as long as the __init__.py file exists.

内容的提问来源于stack exchange,提问作者user3763074

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 11:32:48