WSGI 下所需的项目*和*应用程序的 Django sys.path.append
有人可以告诉我为什么我需要将项目根路径添加到 python 路径以及 WSGI 文件中的应用程序本身吗?
项目库称为“djapp”,应用程序称为“myapp”。
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../djapp')
os.environ['DJANGO_SETTINGS_MODULE'] = 'djapp.settings'
如果我省略带有“/../djapp/”的行,日志会告诉我无法导入“myapp”,即使“djapp.settings”可以导入。 (验证“djapp”已导入)
它可以使用 ./manage.py 命令正常运行。项目文件夹中有一个__init__
。
为了进行测试,我使用 addsitedir
看到了同样的问题:
site.addsitedir('/home/user/web/project/')
site.addsitedir('/home/user/web/project/djapp')
Could somebody give me a pointer on why I need to add my project root path to the python path as well as the application itself in my WSGI file?
Project base is called 'djapp', the application is called 'myapp'.
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../djapp')
os.environ['DJANGO_SETTINGS_MODULE'] = 'djapp.settings'
If I omit the line with "/../djapp/" the log tells my that 'myapp' can not be imported, even though 'djapp.settings' is. (validating 'djapp' was imported)
It al runs properly with the ./manage.py command. there's a __init__
in the project folder.
For testings sake, I see the same issue using addsitedir
:
site.addsitedir('/home/user/web/project/')
site.addsitedir('/home/user/web/project/djapp')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于
djapp
(django 项目文件夹)位于也属于部署的父文件夹中,因此我将djapp
文件夹简单地重命名为project
。那么这段代码总是正确:
完整的文件夹布局是:
你有什么。这样项目就可以始终被称为项目:)
希望有帮助。
格茨G
Since
djapp
(the django project folder) is in a parent folder that also belongs to the deployment I renamed thedjapp
folder simply toproject
.Then this code is always correct:
The complete folder layout being:
And what have you. This way project can always be called project :)
Hope that helps.
GrtzG
想必您的项目中有执行
from myapp import foo
的代码。两个选项:
from djapp.myapp import foo
,不建议这样做,因为它会妨碍可移植性;djapp
,并将DJANGO_SETTINGS_MODULE
设置为'settings'
。Presumably you've got code within your project which is doing
from myapp import foo
.Two options:
from djapp.myapp import foo
, which is not recommended as it prevents portability;djapp
in your WSGI, and set theDJANGO_SETTINGS_MODULE
to just'settings'
.