首页 文章

如何修改Procfile以在Heroku上的非标准文件夹中运行Gunicorn进程?

提问于
浏览
40

我是heroku和gunicorn的新手,所以我不确定这是如何工作的 . 但我已经做了一些搜索,我认为我接近部署我的Django应用程序(1.5.1) . 所以我知道我需要一个Procfile

web: gunicorn app.wsgi

因为我的目录有点不同 . 我无法在根目录中运行gunicorn

app_project
    requirements/
    contributors/
    app/
        app/
            settings/
            wsgi.py
        # Normally Procfile goes here
    Procfile

通常app /将是根目录,但我决定以这种方式构建我的文件夹以将我的django应用程序与其他一些东西分开 . 由于我必须将Procfile放在根目录中以便heroku识别它,我应该在Procfile中放入什么和/或我应该在gunicorn命令中放置什么参数?

注意:

web: gunicorn app.wsgi # won't work because Procfile is in a directory above
                       # I also want to keep the directories as is
                       # I also don't want to create a secondary git inside the app folder just for heroku
web: gunicorn app.app.wsgi # won't work because I don't want to convert the folder into a python module

4 回答

  • 51

    尝试:

    web: gunicorn --pythonpath app app.wsgi
    
  • 40

    正如@Graham Dumpleton在他的回答中所说,OP的问题可以通过将他的Procfile修改为以下内容来解决:

    web: gunicorn --pythonpath app app.wsgi

    Why this works:

    • 请记住,Heroku只是使用Procfile来启动进程 . 在这种情况下,gunicorn进程 .

    • Gunicorn的 --pythonpath 参数允许您将目录动态附加到Python运行时在进行模块查找时搜索的目录列表 .

    • 通过将 --pythonpath app 添加到gunicorn命令,解释器基本上被告知'在应用程序目录中查找一个名为app的包(也称为app),其中包含一个名为wsgi的模块 .

    OP问题中文件夹的通用名称可能会模糊命令的语法,如下所示: gunicorn --pythonpath <directory_containing_package> <package>.<module>

    More Info:
    Gunicorn Documentation

  • 0

    我做了一个丑陋的黑客工作 . 所以我要发布我的答案,但我希望你们能提出更好的解决方案

    Procfile

    web: sh ./app/run.sh
    

    app_project /应用程序/ run.sh

    #!/bin/bash
    
    cd app
    gunicorn app.wsgi
    
  • 4

    我喜欢eikonomega的答案,但我想补充一下我是如何解决类似问题的:

    由于我的文件位于更多文件夹中,因此我有点困难

    而不是将路径添加到PYTHONPATH环境变量,而是像引用包中的模块一样引用它:

    在我的例子中,app对象位于answer1.py中,位于answer8-web-financial-graph文件夹中的answers文件夹中 .

    web: gunicorn app8-web-financial-graph.answers.script1:app
    

相关问题