[Django 新手问题求教] Django2.2 使用 django-admin runserver 出现 No module named 'Bill'错误,但是使用 Python manage runserver 没问题。

2020-06-28 17:05:06 +08:00
 littlecrab

问题

使用 python manage.py runserver 正常启动,使用 django-admin 出错。查看 manage.py 中也是用的'Bill.settings',参考了 overflow 加了命令的参数还是不行,请教大家到底啥原因,如何解决

错误信息

使用命令

django-admin runserver 8054 --settings="Bill.settings" --pythonpath=.

出现错误

(venv-django2.2) D:\PythonApp\Bill>django-admin runserver 8054 --settings="Bill.settings"  --pythonpath="D:\Program Files\Python\Python36"
Traceback (most recent call last):
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\core\management\commands\runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\core\management\commands\runserver.py", line 67, in handle
    if not settings.DEBUG and not settings.ALLOWED_HOSTS:
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\conf\__init__.py", line 79, in __getattr__
    self._setup(name)
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\conf\__init__.py", line 66, in _setup
    self._wrapped = Settings(settings_module)
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\conf\__init__.py", line 157, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "D:\Program Files\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'Bill'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Program Files\Python\Python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "D:\Program Files\Python\Python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\PythonApp\venvs\venv-django2.2\Scripts\django-admin.exe\__main__.py", line 9, in <module>
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\core\management\base.py", line 336, in run_from_argv
    connections.close_all()
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\db\utils.py", line 219, in close_all
    for alias in self:
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\db\utils.py", line 213, in __iter__
    return iter(self.databases)
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\utils\functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\db\utils.py", line 147, in databases
    self._databases = settings.DATABASES
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\conf\__init__.py", line 79, in __getattr__
    self._setup(name)
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\conf\__init__.py", line 66, in _setup
    self._wrapped = Settings(settings_module)
  File "d:\pythonapp\venvs\venv-django2.2\lib\site-packages\django\conf\__init__.py", line 157, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "D:\Program Files\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'Bill'

1362 次点击
所在节点    Python
2 条回复
OnlyO
2020-06-28 17:15:25 +08:00
pythonpath 是你项目根文件夹, 你是不是路径没有添对,注意绝对路径和相对路径.
littlecrab
2020-06-28 18:57:49 +08:00
@OnlyO 谢谢! 想当然地以为 pythonpath 是 python 路径,No module 的错误就是 pythonpath 配置错了造成的。pythonpath 设置成项目路径就可以了。

不过现在一直提示找不到 django-admin“d:\program files\python\python36\python.exe: can't open file 'D:\Program Files\Python\Python36\Scripts\django-admin': [Errno 2] No such file or directory” ,环境变量没问题,django-admin runserver 也能打印版本号,找到现在也没找到解决方法,请问这个问题您知道如何解决吗

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/685368

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX