Import error when building python using py2exe

落爺英雄遲暮 提交于 2019-12-12 16:24:27

问题


I am trying to make a small script to remotely manage windows computers (currently only shutdown). The method I am using involves a webapp2 server. i would like to compile my first attempt into a .exe. The problem I am having is that after successfully compiling it I go to run it and it returns the error:

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

I have also tried this with cx_Freeze which had similar results. I have followed the advice given at import error while bundling using py2exe to no avail.

In case it is any use here is my code:

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()

Thank you in advance.

EDIT:

I have found out using modulefinder that there are a lot of modules not being imported. I don't however know if this is happening when ran normally or only when imported or something like that.

http://pastebin.com/s0U9WHJ6


回答1:


I have discovered that the problem was that I was presuming that py2exe would import webob as the interpreter does. In fact I needed to put the webob folder in the folder that I was building in.




回答2:


I am not sure, but you could try specifically including email.utils in the setup.py by adding the following argument to the setup function call in the script that imports py2exe:

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

That, or you could try specificly importing it before you import webapp2, like on line 1:

import email.utils
import cgi
import webapp2

If this says it can't find a diferent module, try adding the module in the includes list:

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

or specificly importing it again. Hope this helps! :-)



来源:https://stackoverflow.com/questions/11274596/import-error-when-building-python-using-py2exe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!