ImportError:没有名为xxx的模块

时间:2015-04-15 13:07:27

标签: python py2exe

from lib import mod_a 
print mod_a.MyClassName 
Result:<class 'lib.mod_a.MyClassName'>

print __import__("mod_a").MyClassName
Result:<class 'mod_a.MyClassName'>

exe (由py2exe创建)运行时,引发错误

print mod_a.MyClassName
Result:<class 'lib.mod_a.MyClassName'>


#Error
print __import__("mod_a").MyClassName            
  

回溯(最近一次调用最后一次):文件“test.py”,第28行,在中   打印导入(“mod_a”)。MyClassName
  ImportError:没有名为mod_a的模块


这是我的setup.py

from distutils.core import setup  
import py2exe
import sys

includes = ["encodings", "encodings.*"]
sys.argv.append("py2exe")
sys.argv.append("-p lxml,gzip")
options = {"py2exe": {
                  "compressed": 1,
                  "optimize": 2,
                  "ascii": 0,
                  "bundle_files": 1,
                  }
        }
setup(
        version = "",
        description = "",
        name = "",
        options = options,
        zipfile=None,
        console = [{"script":'test.py',
                    'icon_resources':[(1, 'update.ico')]}])

2 个答案:

答案 0 :(得分:0)

导入py2exe

后,尝试在设置中导入mod_a
from distutils.core import setup  
import py2exe
import sys
from lib import mod_a 

答案 1 :(得分:0)

print sys.modules["lib.mod_a"].MyClassName

在exe上运作良好!