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

标签 django heroku directory virtualenv procfile

简介:

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

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

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

最高层的布局:

repository_root/
    django_project_root/
        configuration_root/


本地:

OS X 10.8.4
点==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

~/Envs
~/项目


hellodjango_venv 内部:

Django==1.5.2
dj-database-url==0.2.2
DJ-静态==0.0.5
django-toolbelt==0.0.1
枪兽==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

编辑 Procfile:

网页: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 channel 上,Jacob Kaplan-Moss 回答:

...the Procfile needs to be at the top-level of your git repo...

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

...your Procfile needs to be in the root of your git repository that is pushed to Heroku...

来自 Heroku 开发中心:

...Process types are declared via a file named Procfile placed in the root of your app...


作为第一次尝试:

我将 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

我会得到错误:Procfile不存在。它发生的原因很明显。


假设:

来自 Django 文档:

...manage.py is automatically created in each Django project. manage.py is a thin wrapper around django-admin.py that takes care of two things for you before delegating to django-admin.py:
- It puts your project’s package on sys.path.
- It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file...

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


所以:

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


预先感谢您。

最佳答案

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

(您的第二次尝试):

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

(您的第一次尝试):

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

希望这有帮助。

关于django - 是否可以在不同的文件夹级别中拥有 Procfile 和 manage.py 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18619886/

相关文章:

django - 通过 SSL 将 Django Postgres 连接到 AWS RDS

python - 无法导入 form_for_model

ruby - 英雄数据库 :pull does not work?

heroku - 在 psycopg2 中设置事务\查询超时?

php - 带有 WordPress 的 Windows 上的目录分隔符不正确

c++ - 递归文件夹读取C++

django - docker tmpfs 似乎对 postgresql 没有影响

node.js - Heroku 上的 nodemon 启动错误

c - 访问目录和文件

python - django - 允许用户在数据库中设置某些内容