Ubuntu16.04环境运行Django1.6.5的runtests.py测试脚本报错求助
Hey, let's get those runtests.py errors sorted out—they're actually super straightforward to fix once you know what's going on!
What's Causing the Problem
Your system is trying to run your Python script as a bash shell script instead of passing it to the Python interpreter. That's why:
- Bash throws a fit trying to execute
os.environ['DJANGO_SETTINGS_MODULE']like it's a shell command (hence the "command not found" error) - It doesn't recognize Python's syntax like
os.path.dirname(__file__)(the unexpected(token)
Two Easy Ways to Fix It
1. Run the Script Directly with Python
Since Django 1.6.5 works with Python 2.7 (the default on Ubuntu 16.04), just run it using the Python interpreter explicitly:
python runtests.py
This tells your system exactly which program to use to parse the script.
2. Make the Script Self-Executable
If you prefer using ./runtests.py to launch it, do these two steps:
- Add this line at the absolute top of your
runtests.pyfile (before any imports):
This is a "shebang" line that tells the system to use the Python interpreter to run the script.#!/usr/bin/env python - Give the script executable permissions with this bash command:
chmod +x runtests.py
Now you can run ./runtests.py without issues.
Quick Side Note
I noticed your script cuts off at from djang...—once you fix the execution problem, make sure that import line is complete (e.g., from django.test.utils import setup_test_environment) to avoid other errors down the line.
内容的提问来源于stack exchange,提问作者Jeel Prajapati

