Apache 无法导入/没有名为 Django 错误的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1656159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:20:57  来源:igfitidea点击:

Could not import/No module named Django Error with Apache

djangoapache

提问by Noah Clark

I had a small proof of concept set up on a development server on a local machine. I'm now trying to move it over to django on a production server, which I'm using webfaction for. However, now that I'm switched over to apache from the built in django server I get the following:

我在本地机器上的开发服务器上设置了一个小的概念证明。我现在正试图将它移到生产服务器上的 django,我正在使用 webfaction 进行生产。但是,现在我从内置的 django 服务器切换到 apache,我得到以下信息:

ViewDoesNotExist: Could not import orgDisplay.views. Error was: No module named orgDisplay.views

But when check my orgDisplay apps folder there is a view.py in it. What am I doing wrong? I've tried adding the following to my settings.py by suggestion of the django IRC room.

但是当检查我的 orgDisplay apps 文件夹时,里面有一个 view.py。我究竟做错了什么?根据 django IRC 房间的建议,我尝试将以下内容添加到我的 settings.py 中。

import sys
sys.path.append(r"/home/user/webapps/django_project/myproject/orgDisplay")

which is the path to my app.

这是我的应用程序的路径。

any ideas on how to even begin to trouble shoot this?

关于如何开始解决这个问题的任何想法?

Thanks in advance.

提前致谢。

回答by Alexander Lebedev

I assume you're using mod_wsgi(which is recommended by Django authors), not mod_python. This is the way you should use your sys.path:

我假设您正在使用mod_wsgi(由 Django 作者推荐),而不是mod_python. 这是您应该使用 sys.path 的方式:

django.wsgi:

django.wsgi:

import os, sys
sys.path.append(r"/home/user/webapps/django_project/myproject/")
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"

sys.stdout = sys.stderr # Prevent crashes upon print

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

urls.py:

网址.py:

from django.conf.urls.defaults import *
urlpatterns = (
   ("", include("orgDisplay.urls")),
   # ...
)

orgDisplay/urls.py:

orgDisplay/urls.py:

import views

urlpatterns = ( 
    (r'^some_view/$', views.some_view), # It is actually orgDisplay.views.some_view
    # many more records ...
)

It is a bad idea to add project dir itself to path since you're be getting name conflicts between multiple projects.

将项目目录本身添加到路径是一个坏主意,因为您会遇到多个项目之间的名称冲突。

回答by Nick Meharry

I think you're appending the wrong directory to sys.path. I think Python is looking in the .../myproject/orgDisplayfolder for the orgDisplaypackage. Try removing the orgDisplayfrom your string, like this:

我认为您将错误的目录附加到sys.path. 我认为 Python 正在寻找包的.../myproject/orgDisplay文件夹orgDisplay。尝试orgDisplay从字符串中删除,如下所示:

import sys
sys.path.append(r"/home/user/webapps/django_project/myproject")

The other option would be to simply add myproject(or whatever your project is actually called) in the import statement.

另一种选择myproject是在导入语句中简单地添加(或您的项目实际调用的任何内容)。

# instead of "from orgDisplay import views"
from myproject.orgDisplay import views

Also, make sure to restart Apache after every edit.

此外,请确保每次编辑后重新启动 Apache。

回答by user200590

looking at manage.py, it does it like so:

查看 manage.py,它是这样操作的:

import sys

from os.path import abspath, dirname, join
from django.core.management import setup_environ


# setup the environment before we start accessing things in the settings.
setup_environ(settings_mod)

sys.path.insert(0, join(PINAX_ROOT, "apps"))
sys.path.insert(0, join(PROJECT_ROOT, "apps"))

回答by D Coetzee

Provided your WSGI file is in your project directory, a slightly more flexible way is this:

假设您的 WSGI 文件在您的项目目录中,一个稍微更灵活的方法是:

import os, sys
sys.path.append(os.path.dirname(__file__))

This will enable you to change your project location later without having to modify your WSGI file.

这将使您能够稍后更改项目位置,而无需修改 WSGI 文件。