我在windows xp上创建了一个项目示例和一个app sampapp当我尝试使用命令sqlall
时,我收到以下错误。
C:\ Python27 \ Django-1.3.1 \ django \ bin> django-admin.py sqlall sampapp
Traceback (most recent call last): File
"C:\Python27\Django-1.3.1\django\bin\django-admin.py", line 5, in
<module > management.execute_from_command_line() File
"C:\Python27\lib\site-packages\django\core\management\__init__.py",
line 429, in execute_from_command_line utility.execute() File
"C:\Python27\lib\site-packages\django\core\management\__init__.py",
line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv) File
"C:\Python27\lib\site-packages\django\core\management\__init__.py",
line 261, in fetch_command klass = load_command_class(app_name,
subcommand) File
"C:\Python27\lib\site-packages\django\core\management\__init__.py",
line 67, in load_command_class module =
import_module('%s.management.commands.%s' % (app_name, name)) File
"C:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in
im port_module __import__(name) File
"C:\Python27\lib\site-packages\django\core\management\commands\sqlall.py"
, line 4, in <module> from django.core.management.sql import sql_all
File "C:\Python27\lib\site-packages\django\core\management\sql.py",
line 6, in <module> from django.db import models File
"C:\Python27\lib\site-packages\django\db\__init__.py", line 14, in
<modul e> if not settings.DATABASES: File
"C:\Python27\lib\site-packages\django\utils\functional.py", line 276,
in __getattr__ self._setup() File
"C:\Python27\lib\site-packages\django\conf\__init__.py", line 40, in
_set up raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable
DJANGO_SE TTINGS_MODULE is undefined.
有人可以帮我解决这个问题吗?
答案 0 :(得分:2)
导航到您的项目目录并在那里运行本地manage.py。
C:\...\my_project_where_sampapp_lives\> python manage.py sqlall sampapp
当您使用'python manage.py'时,您正在使用'django-admin.py'
$ django-admin.py shell
Error: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
$ python manage.py shell
...
>>> print "now it works"
答案 1 :(得分:0)
应指定DJANGO_SETTINGS_MODULE, 我通常将此行添加到我的.bashrc文件中:
导出PYTHONPATH =。 DJANGO_SETTINGS_MODULE = yoursettingsfile
答案 2 :(得分:0)
Django-admin.py正在查找您无法找到的项目settings.py
文件,因此会引发ImportError。似乎在使用django-admin.py命令时没有导航到项目目录。
您需要做的就是导航到您的项目目录。类似的东西:
(假设您的项目名称是我的项目)
cd myproject
然后运行
django-admin.py sqlall sampapp