繁体   English   中英

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

[英]Python + WSGI - Can't import my own modules from a directory?

我是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] 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

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

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

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

导入*从包中

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

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

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

import sys

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

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

WSGIDaemonProcess example home=/path/to/mysite.com python-path=/path/to/mysite.com

有关详细信息,请参阅http://blog.dscpl.com.au/2014/09/python-module-search-path-and-modwsgi.html

在你的代码中,hello.py只包含一个方法,如果你把它包装在一个可以看作模块的类中。

暂无
暂无

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

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