遵循Django 2.2官方教程时出现ModuleNotFoundError: No module named 'polls'错误
Hey, let's get that polls app up and running! That error means Django can't locate your polls module, so let's walk through the most common fixes:
1. Double-check your project structure
First off, make sure your polls app is in the right place and has all the required files. Your project should follow this structure (the key detail is that polls/ sits at the same level as manage.py):
mysite/ manage.py mysite/ __init__.py settings.py urls.py # other core Django system files polls/ __init__.py # This empty file is critical—Python needs it to recognize the folder as a module admin.py apps.py migrations/ __init__.py models.py views.py urls.py # Your custom URL config here
If you manually created the polls folder, you might have forgotten these __init__.py files—add empty versions of them if they're missing.
2. Confirm your INSTALLED_APPS setup
You already added 'polls' to INSTALLED_APPS in settings.py, which is correct as long as the folder structure above is right. If polls were nested inside another subfolder, you'd need to use a path like 'mysite.polls', but that's not necessary here.
3. Check where you're running the server command
You used py mysite\manage.py runserver, which means you're executing the command from the parent folder of mysite/. That works fine, but only if polls/ is inside mysite/ (next to manage.py). If polls lives outside that folder, Django won't be able to find it.
A simpler approach is to move into the mysite/ folder first, then run the server:
cd mysite py manage.py runserver
This ensures Django looks for apps in the correct directory by default.
4. Recreate the app with Django's startapp command (if needed)
If you built the polls folder manually, you might be missing some auto-generated files that Django expects. Save your existing views.py and urls.py code, delete the current polls folder, then run this command from the same directory as manage.py:
py manage.py startapp polls
Then paste your saved code back into the new polls folder—this guarantees the structure is 100% Django-friendly.
Quick side note: Fix that HTML typo
While it's not causing the module error, your views.py has a tiny HTML mistake: <h1><this is a test page</h1> has an extra < before "this". Change it to <h1>This is a test page</h1> so your page renders correctly once the server runs.
内容的提问来源于stack exchange,提问作者BlockeeR

