简体   繁体   中英

Import error when building python using py2exe

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

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.

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! :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM