Python + WSGI - 无法从目录导入我自己的模块?

lau*_*kok 8 python mod-wsgi wsgi python-2.7

我是Python的新手,我已经查看了如何从目录/子目录导入我的自定义模块.比如这个这个.

这是我的结构,

index.py
__init__.py
modules/
  hello.py
  HelloWorld.py
  moduletest.py
Run Code Online (Sandbox Code Playgroud)

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

init .py,

from modules import moduletest
from modules import hello
from modules import HelloWorld
Run Code Online (Sandbox Code Playgroud)

模块/ hello.py,

def hello():
    return 'Hello World from hello.py!'
Run Code Online (Sandbox Code Playgroud)

模块/ HelloWorld.py,

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

    def sayHello(self):
        return self.message
Run Code Online (Sandbox Code Playgroud)

模块/ 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."
Run Code Online (Sandbox Code Playgroud)

但是通过Apache WSGI,我收到了这个错误,

[wsgi:error] [pid 5840:tid 828] [client 127.0.0.1:54621] import hello [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
Run Code Online (Sandbox Code Playgroud)

PM *_*ing 6

您应该__init__.pymodules/目录中有一个文件来告诉Python这modules是一个.它可以是一个空文件.

如果您愿意,可以将其放入其中__init__.py以简化导入包的模块:

__all__ = ['hello', 'HelloWorld', 'moduletest']
Run Code Online (Sandbox Code Playgroud)

导入*从包中

现在当用户写入时会发生什么sound.effects import *?理想情况下,人们希望以某种方式传递给文件系统,找到包中存在哪些子模块,并将它们全部导入.这可能需要很长时间,导入子模块可能会产生不必要的副作用,这种副作用只有在显式导入子模块时才会发生.

唯一的解决方案是让包作者提供包的显式索引.import语句使用以下约定:如果包的__init__.py代码定义了一个名为的列表 __all__,则它将被视为from package import *遇到时应导入的模块名称列表.在发布新版本的软件包时,由软件包作者决定是否保持此列表的最新状态.如果包装作者没有看到*从包装中导入的用途,他们也可能决定不支持它.


小智 0

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