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

标签 python python-2.7 mod-wsgi wsgi

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

这是我的结构,

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

索引.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]

初始化.py,

from modules import moduletest
from modules import hello
from modules import HelloWorld

modules/hello.py,

def hello():
    return 'Hello World from hello.py!'

modules/HelloWorld.py,

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

    def sayHello(self):
        return self.message

modules/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] import hello [wsgi:error] [pid 5840:tid 828] [client 127.0.0.1:54621] ImportError: No module named hello

知道我做错了什么吗?

编辑:

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

最佳答案

你应该在 modules/ 目录中有一个 __init__.py 文件来告诉 Python modules 是一个 package .它可以是一个空文件。

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

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

来自 Importing * From a Package

Now what happens when the user writes from sound.effects import *? Ideally, one would hope that this somehow goes out to the filesystem, finds which submodules are present in the package, and imports them all. This could take a long time and importing sub-modules might have unwanted side-effects that should only happen when the sub-module is explicitly imported.

The only solution is for the package author to provide an explicit index of the package. The import statement uses the following convention: if a package’s __init__.py code defines a list named __all__, it is taken to be the list of module names that should be imported when from package import * is encountered. It is up to the package author to keep this list up-to-date when a new version of the package is released. Package authors may also decide not to support it, if they don’t see a use for importing * from their package.

关于Python + WSGI - 无法从目录导入我自己的模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32066051/

相关文章:

Python CA Rally API Pyral - 在子项目中找不到用户故事

python - 将代码放置在 Apache 和 Django 的文档根问题之外

python - 在一台 Xampp Windows Apache Mod_WSGI 服务器上部署两个 Django 项目

python - 如何在 django 中设置 "CSRF verification failed"的默认处理程序?

python - Django Haystack - 索引单个字段

python - Tkinter多处理不能泡菜

python - 循环一列并填充函数 Pandas Dataframe Python 中的行

python - 如何使用 PycURL 保持非事件连接打开?

python - 按特定列分组,列出其他列 Pandas

python - 装饰图案与 Python 中的猴子修补