本文介绍了django manage.py如何将项目包放在sys.path上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了django 和一些SO帖子以了解他们都说:

所以我检查了这2个文件的源代码(最新版本,所以它的文档)。



然后我很困惑。 manage.py 做第二件事:设置DJANGO_SETTINGS_MODULE环境变量。除此之外,我真的找不到这两个脚本之间的区别。



[django-admin.py]

 #!/ usr / bin / env python 
从django.core导入管理

如果__name__ ==__main__:
management.execute_from_command_line()

[manage.py]

 #!/ usr / bin / env python 
import os
import sys

如果__name__ ==__main__
os.environ.setdefault(DJANGO_SETTINGS_MODULE,{{project_name}}。settings)

from django.core.management import execute_from_command_line

execute_from_command_line sys.argv)

为什么? django文件是否过期?还是我错过了这里的东西?那么将项目的包放在sys.path上的代码在哪里?

解决方案

sys.path 已更新使用 handle_default_options(options)语句位于。执行路径如下:




  • execute_from_command_line(argv)(您的 manage.py

  • utility.execute()

  • handle_default_options(options)



命令使用相同的方法类用作管理命令的基类。


I read the django doc and some SO posts to know the differences between manage.py and django-admin.py.

They all say:

So I checked the scource code of these 2 files(latest version, so it the doc).

Then I am confused. manage.py does the second thing: sets the DJANGO_SETTINGS_MODULE environment variable. Besides that, I really can not find any differences between these 2 scripts.

[django-admin.py]

#!/usr/bin/env python
from django.core import management

if __name__ == "__main__":
    management.execute_from_command_line()

[manage.py]

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

Why? Is the django documentation out of date? Or I missed something here? And where is the code that puts the project’s package on sys.path?

解决方案

The sys.path is updated here using handle_default_options(options) statement located here. The execution path is as follows:

  • execute_from_command_line(argv) (your manage.py)
  • utility.execute() here
  • handle_default_options(options) here

The same method is used by Command class used as base class for management commands.

这篇关于django manage.py如何将项目包放在sys.path上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 20:27