使用py2exe构建python时导入错误

时间:2012-06-30 13:53:33

标签: python py2exe

我正在尝试制作一个小脚本来远程管理Windows计算机(目前只关闭)。我使用的方法涉及webapp2服务器。我想将我的第一次尝试编译成.exe。我遇到的问题是,在成功编译之后,我去运行它并返回错误:

Traceback (most recent call last):
 File "web2.py", line 2, in <module>
 File "webapp2.pyc", line 25, in <module>
 File "webob\__init__.pyc", line 1, in <module>
 File "webob\datetime_utils.pyc", line 10, in <module>
ImportError: No module named email.utils

我也尝试过使用cx_Freeze,结果相似。我遵循import error while bundling using py2exe给出的建议无济于事。

如果有任何用处,我的代码是:

import cgi
import webapp2
import os
import socket


def ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(('google.com', 0))
    return s.getsockname()[0]

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.out.write("""
          <html>
            <body>
              <form action="/shutdown" method="link">
                <div><input type="submit" value="Shutdown"></div>
              </form>
            </body>
          </html>""")


class shutdown(webapp2.RequestHandler):
    def get(self):
        self.response.out.write('<html><body>Shutting down...<pre>')
        self.response.out.write('</pre></body></html>')
        os.system("shutdown -p -f")

app = webapp2.WSGIApplication([('/', MainPage),
                              ('/shutdown', shutdown)],
                              debug=True)
def main():
    from paste import httpserver
    httpserver.serve(app, host=ip(), port='80')

if __name__ == '__main__':
    main()

提前谢谢。

编辑:

我发现使用modulefinder有很多模块没有被导入。但是,我不知道这是在正常运行还是仅在导入或类似情况下发生的情况。

http://pastebin.com/s0U9WHJ6

2 个答案:

答案 0 :(得分:0)

我发现问题在于我假设py2exe会像解释器那样导入webob。实际上我需要将webob文件夹放在我正在构建的文件夹中。

答案 1 :(得分:0)

我不确定,但您可以尝试在setup.py中专门包含email.utils,方法是在导入py2exe的脚本中将以下参数添加到setup函数调用中:

options={"py2exe": {'includes': ["email.utils"]}}

那,或者您可以在导入webapp2之前尝试特定地导入它,就像在第1行上一样:

import email.utils
import cgi
import webapp2

如果这表示无法找到不同的模块,请尝试在包含列表中添加模块:

options={"py2exe": {'includes': ["email.utils", "othermodulename"]}}

或特别是再次导入它。 希望这可以帮助! : - )

相关问题