django-admin命令和其它任何操作都报错

黄昊英
2023-12-01

事情是这样的:有一天突然发现,我的django环境突然出问题了。无论是创建项目还是执行命令都报错故障如下:

[root@192.168.42.1 ~/python_code/Django]# django-admin -h
Traceback (most recent call last):
  File "/usr/local/bin/django-admin", line 11, in <module>
    sys.exit(execute_from_command_line())
  File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 303, in execute
    settings.INSTALLED_APPS
  File "/usr/local/lib/python3.5/site-packages/django/conf/__init__.py", line 48, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.5/site-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python3.5/site-packages/django/conf/__init__.py", line 92, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/local/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'mysite'

百思不得其解,我的django-admin命令什么时候去导入mysite模块了?百度来谷歌去,也没搜到什么有效方法,搁置了一个月,打算学项目,结果创建的时候又报这个错误,果然百度还是不靠谱。转向谷歌,最后在这个帖子里找到了貌似问题跟我类似的:https://stackoverflow.com/questions/23854146/run-django-admin-py-raise-error-could-not-import-settings-mysite-settings
人家原话是这样的:

What you did was to add an environment variable DJANGO_SETTINGS_MODULE=mysite.settings and after ater that, either uninstall the module OR yesterday you used to only run the django-admin.py command from inside the specific directory where mysite was located.

To solve this:

Solution 1:Remove the DJANGO_SETTINGS_MODULE environment variable (i'd suggest this)

Solution 2: run django-admin.py command from inside the directory you were running it yesterday

Sollution 3:Explicitly run django-admin.py whatever --settings=some.good.settings.module

But really, just do 1 anyway. it's saner that way.

If you don't know how to remove environment variables, check out google, it's pretty simple, and you should remember it anyway, cuz you did it yesterday :P (or check out this link

unset DJANGO_SETTINGS_MODULE Removes DJANGO_SETTINGS_MODULE – user773093 May 26 '15 

还是国外的大神多,看到这里我开始怀疑起我的环境变量了,于是我用env命令看了下,一看不得了。。。:
PATH=/root/.pyenv/shims:/root/.pyenv/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/sbin:/usr/local/mysql/bin:/usr/local/pureftpd/bin:/usr/local/apache2/bin:/usr/local/php/bin:/usr/local/pureftpd/bin:/usr/local/nginx/sbin:/usr/local/php/sbin:/usr/local/redis/bin:/root/bin:/usr/local/mysql/bin
PWD=/root/python_code/Django
DJANGO_SETTINGS_MODULE=mysite.settings
LANG=zh_CN.UTF-8
PS1=[\e[0m][\u@[\e[32;1m]192.168.42.1[\e[0m] \w]#
HISTCONTROL=ignoredups
PYENV_SHELL=bash
SHLVL=1
HOME=/root
LOGNAME=root
QTLIB=/usr/lib64/qt-3.3/lib
CVS_RSH=ssh

原来不知什么时候DJANGO_SETTINGS_MODULE=mysite.settings被设为了环境变量,那么这个环境变量就会把我代码中的设置覆盖,所以我无论执行任何django的操作都会调用到这个环境变量,出现开头的报错也就不足为奇了。
大概是某次跟着某个教程操作的,,真的是十分坑爹,再也不敢轻易设置这种环境变量了。

 类似资料: