是否可以在不同的文件夹级别中拥有 Procfile 和 manage.py 文件?

Posted

技术标签:

【中文标题】是否可以在不同的文件夹级别中拥有 Procfile 和 manage.py 文件?【英文标题】:Is it possible to have a Procfile and a manage.py file in a different folder level? 【发布时间】:2013-09-08 07:37:06 【问题描述】:

简介:

我正在关注Getting Started with Django on Heroku 快速入门指南。

我打算应用 Two Scoops of Django 一书关于使用 virtualenvs 和项目的哲学。 Audrey RoyDaniel Greenfeld(作者)喜欢将所有环境放在一个目录中,将所有项目放在另一个目录中。

我还打算应用 Two Scoops of Django 书籍哲学,将django-admin.py startproject 管理命令生成的内容放在另一个用作 git 存储库根目录的目录中(又名三层方法)。

最高层的布局:

repository_root/
    django_project_root/
        configuration_root/

本地:

OS X 10.8.4 点==1.4.1 虚拟环境==1.10.1 virtualenvwrapper==4.1.1 wsgiref==0.1.2 .bashrc 包含:

export WORKON_HOME=$HOME/Envs
export PROJECT_HOME=$HOME/Projects

~/环境 ~/项目

hellodjango_venv 内部:

Django==1.5.2 dj-database-url==0.2.2 dj-静态==0.0.5 django-toolbelt==0.0.1 gunicorn==18.0 psycopg2==2.5.1 静态==0.4

从终端:

mac-pol:~ oubiga$ mkdir -p Projects/hellodjango_rep/hellodjango
mac-pol:~ oubiga$ cd Projects/hellodjango_rep/hellodjango
mac-pol:hellodjango oubiga$ mkvirtualenv hellodjango_venv
New python executable in hellodjango_venv/bin/python2.7
Also creating executable in hellodjango_venv/bin/python
Installing Setuptools..................................

...

..................................................done.
(hellodjango_venv)mac-pol:hellodjango oubiga$ pip install django-toolbelt

...

Successfully installed django-toolbelt django psycopg2 gunicorn dj-database-url dj-static static
Cleaning up...
(hellodjango_venv)mac-pol:hellodjango oubiga$ django-admin.py startproject hellodjango .
(hellodjango_venv)mac-pol:hellodjango oubiga$ touch Procfile && open Procfile

编辑过程文件:

web: gunicorn hellodjango.wsgi

开始进程:

(hellodjango_venv)mac-pol:hellodjango oubiga$ foreman start
01:30:37 web.1  | started with pid 638
01:30:37 web.1  | 2013-09-04 01:30:37 [638] [INFO] Starting gunicorn 18.0
01:30:37 web.1  | 2013-09-04 01:30:37 [638] [INFO] Listening at: http://0.0.0.0:5000 (638)
01:30:37 web.1  | 2013-09-04 01:30:37 [638] [INFO] Using worker: sync
01:30:37 web.1  | 2013-09-04 01:30:37 [641] [INFO] Booting worker with pid: 641
CTRL+C

到目前为止,一切都很好。

目前我的项目树是:

~/Projects/    
    hellodjango_rep/
        hellodjango/ cwd
            manage.py
            Procfile
            hellodjango/
                __init__.py
                settings/
                urls.py
                wsgi.py

我的 Virtualenvs 树是:

~/Envs/
    get_env_details
    initialize
    postactivate
    postdeactivate
    postmkproject
    postmkvirtualenv
    postrmproject
    postrmvirtualenv
    preactivate
    predeactivate
    premkproject
    premkvirtualenv
    prermproject
    prermvirtualenv
    hellodjango_venv/
        bin/
        include/
        lib/

但是,您还记得三层方法吗?我怎样才能实现它?

我很确定 hellodjango_rep/ 稍后将至少包含:.git、.gitignore 和 requirements.txt

研究:

在#django IRC 频道上,Jacob Kaplan-Moss 回答:

...Procfile 需要位于您的 git 存储库的顶层...

Neil Middleton,Heroku 的工程师,在 SO 中回答了这个问题“Procfile 声明类型 -> (none) in Heroku”:

...您的 Procfile 需要位于您的 git 存储库的根目录中 被推送到 Heroku...

来自 Heroku 开发中心:

...进程类型是通过一个名为 Procfile 的文件声明的,该文件位于 您应用的根目录...

第一次尝试:

我将 Procfile 上移一级文件夹。

(hellodjango_venv)mac-pol:hellodjango oubiga$ cd ..

目前我的项目树是:

~/Projects/    
    hellodjango_rep/ cwd
        Procfile
        hellodjango/
            manage.py
            hellodjango/
                __init__.py
                settings/
                urls.py
                wsgi.py

我重新开始这个过程:

(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ foreman start

我会得到ImportError: No module named hellodjango.wsgi

调试:

ImportError 似乎来自/Users/oubiga/Envs/hellodjango_venv/lib/python2.7/site-packages/gunicorn/util.py

def import_app(module):
    parts = module.split(":", 1)
    if len(parts) == 1:
        module, obj = module, "application"
    else:
        module, obj = parts[0], parts[1]

    try:
        __import__(module)  # <-- line 354
    except ImportError:
        if module.endswith(".py") and os.path.exists(module):
            raise ImportError("Failed to find application, did "
                "you mean '%s:%s'?" % (module.rsplit(".", 1)[0], obj))
        else:
            raise

...

没有名为 hellodjango.wsgi 的模块

作为第二次尝试:

Procfile 返回上一级。 Procfile 和 manage.py 又属于同一个了。

(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ foreman start

我会收到ERROR: Procfile does not exist. 发生这种情况的原因很明显。

假设:

来自 Django 文档:

...manage.py 在每个 Django 项目中自动创建。 manage.py 是 django-admin.py 的一个瘦包装器,负责处理 在委派给 django-admin.py 之前给你两件事: - 它将您项目的包放在 sys.path 上。 - 它设置 DJANGO_SETTINGS_MODULE 环境变量,使其指向您项目的 settings.py 文件...

我不确定问题是否与此有关。

所以:

是否可以在不同的文件夹级别拥有 Procfile 和 manage.py 文件?这种方法有什么问题? p>

提前谢谢你。

【问题讨论】:

我知道为时已晚。你不需要改变任何东西。将procfile 放在顶层。并运行gunicorn --chdir hellodjango hellodjango.wsgi。它会起作用的。谢谢 【参考方案1】:

首先我不得不说,我没有做过如此广泛的研究,并且已经使用了 Heroku 大约 3 次。但是:

(你的第二次尝试):

Procfile 确实应该在***目录中。如果你把 Procfile 移得更深, gunicorn 就找不到了。

(你的第一次尝试):

并且将您的 Procfile 放在***目录中,您应该说明 wsgi 的路径。这是不可能的。所以你应该在第一个“hellodjango”目录中创建 __init__.py 文件,将其标记为“模块”。然后你应该将 Procfile 中的路径更改为:hellodjango.hellodjango.wsgi

希望这会有所帮助。

【讨论】:

【参考方案2】:

我使用 Two Scoops 布局遇到了这个问题,我的解决方案是保持标准布局(repository_root 中的Procfileproject_root 中的manage.py)并制作@ 987654325@ 在运行 gunicorn 之前将目录更改为 project_root 的脚本。

例如,Procfile:

web: sh -c 'cd ./mysite/ && exec gunicorn mysite.wsgi --log-file -'

您同样可以让Procfile 调用外部脚本:

web: bash scripts/heroku_run

我更喜欢添加额外的模块路径,因为它可能会弄乱 settings.pyurls.py 等中的导入。

【讨论】:

谢谢!这对我帮助很大,因为我喜欢将 django 根目录放在存储库根目录下一级:)

以上是关于是否可以在不同的文件夹级别中拥有 Procfile 和 manage.py 文件?的主要内容,如果未能解决你的问题,请参考以下文章

在开发和生产中使用不同的Procfile

1 个 heroku 应用程序中的 2 个不和谐机器人

Nuxt 中同一级别的多个动态路由可能吗?

是否可以在 GLib 新结构化日志记录中为每个域/日志级别设置不同的写入器函数?

Heroku Gunicorn Procfile

如何在云上拥有软件访问文件