[英]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.