Django测试模块导入失败求助(Python3.8环境)
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, andpages_project/from the innerpagesfolder to your project's root directory (same level as the topPipfile). - Delete the inner
pages/pages/subfolder, then merge its contents (likeviews.py,urls.py) into the outerpages/app directory (don't overwrite existing files likeapps.pyunless you're sure they're incorrect). - Move the
templates/folder to either the project root or inside thepages/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

