1

I'm trying use py2exe on a program that imports urlparse from six.moves.urllib_parse. Here is the program:

# hello.py
from six.moves.urllib_parse import urlparse
print('hello world')

And here is my setup.py:

from distutils.core import setup
import py2exe
setup(console=['hello.py'])

Running hello.py works fine. When I compile hello.py into an exe using python setup.py py2exe, a hello.exe file is produced. However, when I run hello.exe I get an error saying:

ImportError: No module named urlparse

I'm using Python 2.7.

With Python 3.4, I get an error saying KeyError: 'six.moves' when running python setup.py py2exe.

How can I stop these errors from occurring?

priomsrb
  • 2,602
  • 3
  • 26
  • 34
  • I would try with adding 'six' to the py2exe 'packages' option – Werner Oct 20 '14 at 12:49
  • @Werner That gives the error: "TypeError: six is not a package" – Nick Humrich Oct 21 '14 at 22:50
  • Since py2exe creates an exe for a specific python version, I decided to replace all my instances of six. Doing so fixed this error for me. (However I then ran into other import errors) – Nick Humrich Oct 22 '14 at 23:40
  • Oops, realized that later, and I tried 'includes' for 'six' but that didn't work either. – Werner Oct 23 '14 at 12:26
  • @Humdinger, what do you mean replacing all your 'six' instances? Did you upgrade to a newer version and that fixed that issue with py2exe? – Werner Oct 23 '14 at 12:27
  • @Werner I mean, in my code, whenever I am using a library from six, I instead replace the `import six` to import the python 3 version of the same module. – Nick Humrich Oct 23 '14 at 21:46

2 Answers2

1

The problem is just that py2exe doesn't detect the modules that are proxied through six, so they're not bundled.

All you have to do is add the module in question (urlparse) to your includes in your setup.py:

  options={
      "py2exe": {
      ...
      "includes": ["urlparse"],
      ...

That way the module will be packaged, and when six tries to import it, it will work.

Jaykul
  • 15,370
  • 8
  • 61
  • 70
0

py2exe released a new version recently that fixes this problem:

Changes in version 0.9.2.2:
- Added support for six, cffi, pycparser, openssl.

Using this version I was able to create an .exe and run it successfully.

priomsrb
  • 2,602
  • 3
  • 26
  • 34