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

oub*_*iga 6 directory django heroku virtualenv procfile

介绍:

  • 我正在关注Heroku快速入门指南中的Django入门.

  • 我打算应用关于使用virtualenvs和项目的Django两本书的哲学思想.Audrey RoyDaniel Greenfeld(作者)喜欢将所有环境放在一个目录中,将所有项目放在另一个目录中.

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

最高级别的布局:

repository_root/
    django_project_root/
        configuration_root/
Run Code Online (Sandbox Code Playgroud)


本地:

OS X 10.8.4
pip == 1.4.1
virtualenv == 1.10.1
virtualenvwrapper == 4.1.1
wsgiref == 0.1.2
.bashrc包含:

export WORKON_HOME=$HOME/Envs
export PROJECT_HOME=$HOME/Projects
Run Code Online (Sandbox Code Playgroud)

〜/ Envs
~/Projects


在hellodjango_venv里面:

Django == 1.5.2
dj-database-url == 0.2.2
dj-static == 0.0.5
django-toolbelt == 0.0.1
gunicorn == 18.0
psycopg2 == 2.5.1
static == 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
Run Code Online (Sandbox Code Playgroud)

编辑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
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.

目前我的Projects树是:

~/Projects/    
    hellodjango_rep/
        hellodjango/ cwd
            manage.py
            Procfile
            hellodjango/
                __init__.py
                settings/
                urls.py
                wsgi.py
Run Code Online (Sandbox Code Playgroud)

而我的Virtualenvs树是:

~/Envs/
    get_env_details
    initialize
    postactivate
    postdeactivate
    postmkproject
    postmkvirtualenv
    postrmproject
    postrmvirtualenv
    preactivate
    predeactivate
    premkproject
    premkvirtualenv
    prermproject
    prermvirtualenv
    hellodjango_venv/
        bin/
        include/
        lib/
Run Code Online (Sandbox Code Playgroud)

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

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


研究:

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

... Procfile需要位于你的git repo的顶层......

Heroku的工程师Neil Middleton在SO中回答了这个问题"Procfile在Heroku中声明了类型 - >(无)":

...你的Procfile需要在你推送到Heroku的git仓库的根目录中...

从Heroku开发中心:

...进程类型通过放置在应用程序根目录中的名为Procfile的文件声明...


作为第一次尝试:

我将Procfile移到一个文件夹级别.

(hellodjango_venv)mac-pol:hellodjango oubiga$ cd ..
Run Code Online (Sandbox Code Playgroud)

目前我的Projects树是:

~/Projects/    
    hellodjango_rep/ cwd
        Procfile
        hellodjango/
            manage.py
            hellodjango/
                __init__.py
                settings/
                urls.py
                wsgi.py
Run Code Online (Sandbox Code Playgroud)

我再次开始这个过程:

(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ foreman start
Run Code Online (Sandbox Code Playgroud)

我将得到ImportError:没有名为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

...
Run Code Online (Sandbox Code Playgroud)

没有名为hellodjango.wsgi的模块


作为第二次尝试:

Procfile返回上一级.Procfile和manage.py再次归属于一起.

(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ foreman start
Run Code Online (Sandbox Code Playgroud)

我会明白ERROR: Procfile does not exist.它为什么会发生.


假设:

来自Django文档:

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

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


所以:

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


先感谢您.

sHt*_*tev 6

我使用两勺布局遇到了这个问题,我的解决方案是保留标准布局(Procfilein repository_rootmanage.pyin project_root)并制作Procfile一个脚本,在运行 Gunicorn 之前将目录更改为project_root

例如,在Procfile

web: sh -c 'cd ./mysite/ && exec gunicorn mysite.wsgi --log-file -'
Run Code Online (Sandbox Code Playgroud)

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

web: bash scripts/heroku_run
Run Code Online (Sandbox Code Playgroud)

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


dar*_*ess 5

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

(您的第二次尝试):

Procfile 确实应该在顶级目录中。如果你移动 Procfile 更深,gunicorn 就找不到它了。

(你的第一次尝试):

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

希望这可以帮助。