PythonAnywhere部署Django网站时出现ModuleNotFoundError: No module named 'AlertTraceWebsite'问题求助
Hey there, let's work through this ModuleNotFoundError you're hitting when deploying your Django app on PythonAnywhere. I've run into this exact issue a handful of times, so here's a step-by-step breakdown of what to check and fix:
1. Double-Check Your Project Directory Structure
First, make sure your project is organized correctly. The AlertTraceWebsite directory (with your settings.py, urls.py, and an empty __init__.py file) should live directly inside the atportal folder you're adding to sys.path.
Your directory should look like this:
~/kabiboy/atportal/ AlertTraceWebsite/ __init__.py settings.py urls.py wsgi.py manage.py
If AlertTraceWebsite is nested deeper or in a different folder, your current path setup won't let Python find it. You can verify this by running ls ~/kabiboy/atportal in PythonAnywhere's console to see what's inside.
2. Ensure Your Virtual Environment Is Properly Activated in WSGI
I notice you're using a virtual environment (venv), but your current WSGI config doesn't activate it. That means PythonAnywhere might be using the global Python interpreter instead of your venv, which could lead to missing modules or path issues.
Add this code at the top of your WSGI file to activate the venv:
activate_this = os.path.expanduser('~/kabiboy/.virtualenvs/venv/bin/activate_this.py') with open(activate_this) as file_: exec(file_.read(), dict(__file__=activate_this))
3. Verify the Settings Module Path is Correct
Your DJANGO_SETTINGS_MODULE is set to AlertTraceWebsite.settings, which is correct only if AlertTraceWebsite is a module Python can import (i.e., its parent directory is in sys.path).
To test this locally in the console:
- Activate your venv:
source ~/.virtualenvs/venv/bin/activate - Navigate to
~/kabiboy/atportal - Run
python -c "import AlertTraceWebsite.settings"
If this throws the same ModuleNotFoundError, fix your directory structure or path first before adjusting the WSGI config.
4. Reload Your Web App After Changes
Every time you modify your WSGI file or project structure, don't forget to go to PythonAnywhere's Web tab and click the Reload button at the top. Without reloading, the server won't pick up your changes.
Updated WSGI Config Example
Here's how your full WSGI file should look with the virtual environment activation added:
import os import sys # Activate the virtual environment activate_this = os.path.expanduser('~/kabiboy/.virtualenvs/venv/bin/activate_this.py') with open(activate_this) as file_: exec(file_.read(), dict(__file__=activate_this)) # Add the project directory to sys.path path = os.path.expanduser('~/kabiboy/atportal') if path not in sys.path: sys.path.insert(0, path) # Set Django settings module os.environ['DJANGO_SETTINGS_MODULE'] = 'AlertTraceWebsite.settings' # Initialize WSGI application from django.core.wsgi import get_wsgi_application from django.contrib.staticfiles.handlers import StaticFilesHandler application = StaticFilesHandler(get_wsgi_application())
Start with checking the directory structure and testing the import in the console—those are the most common culprits here. Let me know if any of these steps resolve your issue!
内容的提问来源于stack exchange,提问作者terry5546

