Python+;WSGI-Can';是否从目录中导入我自己的模块?

Python+;WSGI-Can';是否从目录中导入我自己的模块?,python,python-2.7,mod-wsgi,wsgi,Python,Python 2.7,Mod Wsgi,Wsgi,我是Python新手,我已经了解了如何从目录/子目录导入自定义模块。如和 这是我的结构, index.py __init__.py modules/ hello.py HelloWorld.py moduletest.py index.py # IMPORTS MODULES import hello import HelloWorld import moduletest # This is our application object. It could have any na

我是Python新手,我已经了解了如何从目录/子目录导入自定义模块。如和

这是我的结构,

index.py
__init__.py
modules/
  hello.py
  HelloWorld.py
  moduletest.py
index.py

# IMPORTS MODULES
import hello
import HelloWorld
import moduletest

# This is our application object. It could have any name,
# except when using mod_wsgi where it must be "application"
def application(environ, start_response):

    # build the response body possibly using the environ dictionary
    response_body = 'The request method was %s' % environ['REQUEST_METHOD']

    # HTTP response code and message
    status = '200 OK'

    # These are HTTP headers expected by the client.
    # They must be wrapped as a list of tupled pairs:
    # [(Header name, Header value)].
    response_headers = [('Content-Type', 'text/plain'),
                       ('Content-Length', str(len(response_body)))]

    # Send them to the server using the supplied function
    start_response(status, response_headers)

    # Return the response body.
    # Notice it is wrapped in a list although it could be any iterable.
    return [response_body]
init.py

from modules import moduletest
from modules import hello
from modules import HelloWorld
模块/hello.py

def hello():
    return 'Hello World from hello.py!'
模块/HelloWorld.py

# define a class
class HelloWorld:
    def __init__(self):
        self.message = 'Hello World from HelloWorld.py!'

    def sayHello(self):
        return self.message
模块/moduletest.py

# Define some variables:
numberone = 1
ageofqueen = 78

# define some functions
def printhello():
    print "hello"

def timesfour(input):
    print input * 4

# define a class
class Piano:
    def __init__(self):
        self.type = raw_input("What type of piano? ")
        self.height = raw_input("What height (in feet)? ")
        self.price = raw_input("How much did it cost? ")
        self.age = raw_input("How old is it (in years)? ")

    def printdetails(self):
        print "This piano is a/an " + self.height + " foot",
        print self.type, "piano, " + self.age, "years old and costing\
        " + self.price + " dollars."
但是通过Apache WSGI,我得到了这个错误

[wsgi:error][pid 5840:tid 828][client 127.0.0.1:54621]导入 您好[wsgi:error][pid 5840:tid 828][client 127.0.0.1:54621] ImportError:没有名为hello的模块

知道我做错了什么吗

编辑:

index.py
__init__.py
modules/
  hello.py
  HelloWorld.py
  moduletest.py
  User/
    Users.py

在代码中,hello.py仅包含一个方法,如果您将它包装在一个类中,该类将被视为一个模块。

您应该在
模块/
目录中有一个
init\uuuuuuuuuupy
文件,告诉Python
模块
是一个模块。它可以是一个空文件

如果愿意,您可以将其放入
\uuuu init\uuuuu.py
中,以简化程序包模块的导入:

__all__ = ['hello', 'HelloWorld', 'moduletest']

现在,当用户从
sound.effects import*
写入时会发生什么? 理想情况下,人们会希望它以某种方式进入文件系统, 查找包中存在的子模块,并导入它们 全部的这可能需要很长时间,导入子模块可能会导致 不必要的副作用,仅当子模块处于运行状态时才会发生 显式导入

唯一的解决方案是由包作者提供一个明确的 包裹的索引。import语句使用以下语句 约定:如果包的
\uuuu init\uuuu.py
代码定义了一个名为
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。这取决于
当新版本的
包已发布。包作者也可能决定不支持
如果他们看不到从他们的包中导入
*
的用途,则可以使用它


您需要在.wsgi文件中设置应用程序的路径,在您的示例中,它似乎是
index.py

import sys

path = '/full/path/to/app'
if path not in sys.path:
   sys.path.insert(0, path)

您还可以在apache virtualhost配置中修复此问题:

wsgidaemonprocessexample home=/path/to/mysite.com python path=/path/to/mysite.com


有关更多信息,请参见。

index.py
中,将
导入hello
替换为
来自模块的导入hello
我因导入而收到此错误。错误:没有模块名为modules
请尝试将
\uu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuy
文件添加到
模块
目录中。另外,请查看我应该在该目录中添加什么内容
modules/\uuuuu init\uuuuu.py
?@teelou:你不需要放任何东西。正如我所说,它可以是一个空文件。但是你可以在里面放一些东西,这样可以更容易地导入你的包和模块。我会在几分钟内为我的答案添加更多信息。@teelou:FWIW,我去年写了一个稍微复杂一点的包结构。@teelou:我刚刚看到了你的编辑。在
modules/User/
中,您还需要一个
\uuuu init\uuu.py
。请看一下我在上一篇评论中链接到的代码,因为它的结构与您的项目的结构相似。@teelou:当然。包的顶部目录需要位于Python路径中。因此,如果它还不在您的常规Python路径中,那么您需要显式地添加它。但是您不必担心将包的子目录添加到Python路径中,
\uuu init\uuuuuuuuuuupy
东西会让Python知道如何找到它们,一旦它找到了顶级目录(从路径信息)并意识到它是一个包(从该顶级目录中的
\uuu init\uuuuuuuuupy
).这实际上解决了我自己的问题。但是,可能有更好的方法获取路径:
os.path.split(\uuu file\uu)[0]
也就是说,最好将python路径传递给wsgi
--python path/full/path/to/app
WSGIProcessGroup示例
(使用相同的组名)在我尝试此操作时是必需的。