简体   繁体   English

urls.py在Heroku中引发ImportError但在本地不引发

[英]urls.py raises ImportError in Heroku but not locally

I'm relatively new to django and the Heroku environment and I suspect I'm having the following issue because of something small I've neglected in my project. 我在django和Heroku环境中相对较新,我怀疑我遇到了以下问题,因为我在项目中忽略了一些小问题。 Basically I'm able to manage.py runserver locally without any issues, and local http views are displayed as expected. 基本上,我可以在本地管理manage.py runserver ,而不会出现任何问题,并且可以按预期显示本地http视图。 I'm able to push changes to Heroku without error as well. 我也可以毫无错误地将更改推送到Heroku。

However, when I try to access my remote Heroku project via http, I get an ImportError from the app's urls.py . 但是,当我尝试通过http访问我的远程Heroku项目时,我从应用程序的urls.py收到ImportError I believe the pycup app is defined properly in the project's settings.py , since it is able to run locally. 我相信pycup应用程序已在项目的settings.py正确定义,因为它可以在本地运行。 Nevertheless I've tried importing 'apps.pycup' and simply 'apps' to settings without success. 不过,我尝试将'apps.pycup'导入并且仅将'apps'导入设置没有成功。

My original thought was that somehow Heroku wasn't reading the file structure properly, or maybe git wasn't pushing the __init__.py file inside the apps folder to Heroku but after doing a quick check with heroku run bash and ls apps/ it's clear that's not the case. 我最初的想法是Heroku无法以某种方式正确读取文件结构,或者git并未将apps文件夹中的__init__.py文件推送到Heroku,但是在对heroku run bash进行了快速检查之后, heroku run bashls apps/很明显事实并非如此。

The reason I'm using an apps/ folder to contain the pycup app was that it was suggested on a how-to blog and it seemed like a good suggestion for potential future scalability to neatly develop more apps in the project. 我之所以使用apps/文件夹包含pycup应用程序,是因为它是在how-to博客上建议的,对于在项目中巧妙地开发更多的应用程序来说,这似乎是一个潜在的未来可扩展性的好建议。

Here is the basic file structure I'm using: 这是我正在使用的基本文件结构:

manage.py
Procfile
requirements.txt
reunion
|___ static
|___ settings.py
|___ urls.py
|___ wsgi.py
|___ __init__.py
|___ __pycache__.py
apps
|___ pycup
| |___ admin.py
| |___ apps.py
| |___ migrations
| |___ models.py
| |___ static
| |___ templates
| |___ tests.py
| |___ urls.py
| |___ views.py
| |___ __init__.py
| |___ __pycache__.py
|___ __init__.py
|___ __pycache__.py

Below are code samples from both urls.py files in the project: 以下是项目中两个urls.py文件的代码示例:

from the main project folder reunion/urls.py : 从主项目文件夹reunion/urls.py

from django.conf.urls import include, url
from django.contrib import admin

from apps.pycup import urls as pycupurls

urlpatterns = [
    # Examples:
    # url(r'^$', 'reunion.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include(pycupurls)),
]

from the app folder apps/pycup/urls.py : 从应用程序文件夹apps/pycup/urls.py

from django.conf.urls import include, url
from django.contrib import admin

from apps.pycup import views # <- This is the line to which the error is traced in Heroku

urlpatterns = [
    # Examples:
    # url(r'^blog/', include('blog.urls')),
    # url(r'^admin/', include(admin.site.urls)),

    url(r'^$', views.index, name='index'),
]

And this is the error I get: 这是我得到的错误:

ImportError at /
No module named pycup
Request Method: GET
Request URL:    http://********.herokuapp.com/
Django Version: 1.9.2
Exception Type: ImportError
Exception Value: No module named pycup
Exception Location: /app/apps/pycup/urls.py in <module>, line 4
Python Executable:  /app/.heroku/python/bin/python
Python Version: 2.7.11

If you are in the same parent folder as the item you are trying to import, you do not put the root path to the item you are calling. 如果您与要导入的项目位于同一父文件夹中,则不要将根路径放在要调用的项目上。 So where you have "from apps.pycup import views" it only needs to be "from . import views". 因此,如果您具有“来自apps.pycup导入视图”,则只需“来自.import视图”即可。 What your code is actually doing with the statement you have is attempting to access the "apps.py" file under the parent pycup folder and it is not finding a function in that folder called pycup 您的代码实际上对您正在执行的语句执行的操作是试图访问父pycup文件夹下的“ apps.py”文件,但在该文件夹中找不到名为pycup的函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM