3

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

Community
  • 1
  • 1
user1492867
  • 129
  • 1
  • 9
  • Does it help if you add `import email.utils` to your code and regenerate the .exe with py2exe? – Luke Woodward Jun 30 '12 at 18:13
  • Thanks, I tried this but got exactly the same error. – user1492867 Jun 30 '12 at 21:03
  • Hmmm... odd. Do you get the same error if you run the script without using py2exe? Interestingly, the [datetime_utils module](http://code.google.com/p/webapp-improved/source/browse/lib/WebOb-1.0.8/webob/datetime_utils.py) uses the `rfc822` module which has been deprecated for some time now. I don't know how relevant this is, however. – Luke Woodward Jun 30 '12 at 21:28
  • No I don't get the error when not using py2exe. – user1492867 Jun 30 '12 at 22:12

2 Answers2

0

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.

user1492867
  • 129
  • 1
  • 9
0

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

Tom
  • 846
  • 5
  • 18
  • 30