簡體   English   中英

在mod_python下執行模塊時ImportError

[英]ImportError when executing module under mod_python

我有一個像這樣的python項目:

py/
   main.py
   __init__.py
   myapp/
           __init__.py
           my/
                __init__.py
                exam.py
           sub/
                __init__.py
                index.py

我的文件包含:main.py:

from myapp.sub.index import *
cd = myc()
cd.doit()

audit.py->

class myclass():
  def hel(self):
     print 'Hello world'

index.py->

from myapp.my.exam import myclass
class myc():
      def doit(self):
          mycc = myclass()  
          mycc.hel() 

我使用fedora並在使用命令時:

python main.py

打印Hello world ,但是當我想用apache和mod_python在瀏覽器中啟動main.py ,出現此錯誤:

File "/var/www/html/py/main.py", line 1, in <module>
  from myapp.sub.index import *

ImportError: No module named myapp.sub.index

在mod_python中不是相對導入工作嗎? 有什么問題,我該怎么辦?

我似乎在mod_python下執行模塊時未正確設置PYTHONPATH環境變量。

使用問題中描述的目錄結構來解決此問題的方法是將其添加到main.py文件中:

import sys, os
sys.path.insert(0, os.path.dirname(__file__))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM