我是Python新手,我已经研究了如何从目录/子目录导入自定义模块。例如this和this。
这是我的结构
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
模块/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: No module named hello
知道我做错了什么吗?
编辑:
index.py
__init__.py
modules/
hello.py
HelloWorld.py
moduletest.py
User/
Users.py
您还可以在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。
您需要在.wsgi文件中设置应用程序的路径,在您的情况下,它似乎是
index.py
:您应该在
modules/
目录中有一个__init__.py
文件来告诉Pythonmodules
是一个package。它可以是空文件。如果您愿意,可以将其放入
__init__.py
中,以简化导入包的模块:来自Importing * From a Package
相关问题 更多 >
编程相关推荐