6

I am trying to create an exe from python code. I can run the code just fine from the command line like this:

python myScript.py

I have installed py2exe from here: http://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/

And, have a setup.py that looks like this:

from distutils.core import setup
import py2exe

setup(console=['myScript.py'])

And, I run the setup.py like this:

python setup.py py2exe

I get the following in the output:

The following modules appear to be missing
['Carbon', 'Carbon.Files', '__pypy__', '_scproxy', 'http_parser.http', 'http_parser.reader', 'jinja2._debugsupport', 'jinja2._markupsafe._speedups',
'jinja2.debugrenderer', 'markupsafe', 'pretty', 'socketpool', 'socketpool.util']

And, sure enough, if I try to run the exe, I get errors:

$ ./myScript.exe
Traceback (most recent call last):
  File "restkit\__init__.pyc", line 9, in <module>
  File "restkit\conn.pyc", line 14, in <module>
ImportError: No module named socketpool
Traceback (most recent call last):
  File "myScript.py", line 12, in <module>
ImportError: cannot import name Resource

What do I need to do to get py2exe to find the dependencies?

Thanks

Doo Dah
  • 3,979
  • 13
  • 55
  • 74
  • 1
    py2exe is a bit dated (last change was in 2008 I think?). I think [PyInstaller](http://www.pyinstaller.org/) is an active project, and has much better support. Also, looking at your comments to the other answers, do you have the modules installed at a system level or are they local to your `myScript.py`? – ernie Mar 18 '13 at 18:40
  • Thanks. I have moved on from py2exe and have been trying cx-freeze. The modules are installed in the Python directory. I can import them fine from a python shell. I will give PyInstaller a shot. – Doo Dah Mar 18 '13 at 18:59
  • You actually have `Carbon` and `Carbon.Files` installed and importable on a Windows machine? – abarnert Mar 18 '13 at 19:10
  • No. I do not. I am not sure why py2exe thinks they are missing – Doo Dah Mar 18 '13 at 19:42
  • Ok, so, now I am trying with PyInstaller. Perhaps I should open a new thread and close this. AFAIK, there is no solution here with py2ex. fwiw, I tried with cx-freeze and packages = ["restkit", "jinja2" , "restkit.client" ]. I also tried with include = ["restkit", "jinja2" , "restkit.client" ] – Doo Dah Mar 18 '13 at 20:07
  • you can do the same thing that you did in cxfreeze there in py2exe with the "includes" option. – trevorKirkby Jan 05 '14 at 04:00

2 Answers2

1

Carbon is the name of two different Mac-specific things.

First, in 2.x, on both Mac OS X and Mac Classic builds, has a Carbon package in the standard library, used for calling Carbon/Toolbox APIs (and, in OS X, CoreFoundation and friends).

Second, in both 2.x and 3.x, on Mac OS X, with PyObjC, the PyObjC wrapper around Carbon.Framework is named Carbon. (PyObjC isn't part of the stdlib, but it does come with Apple builds of Python, and most third-party builds besides python.org's official installers.)

Neither of these will exist on Windows.

py2exe tries to be smart and only import things relevant to your platform. However, it's pretty easy to fool. For example, something like this:

try:
    import Carbon.Files
except:
    Carbon = collections.namedtuple('Carbon', 'Files')
    Carbon.Files = None

if Carbon.Files:
    Carbon.Files.whatever(…)

… might make py2exe think Carbon.Files is required.

Now, obviously this isn't your whole problem, but it is a very big red flag that py2exe's module dependency code is not working for your project. You probably have similar problems with all kinds of other modules, so it's both missing some things you need and demanding some things you don't have, and this is probably what's causing your actual problems.

As the FAQ explains, you can debug this by running the module-finder code to see where it's going wrong, like this:

python -m py2exe.mf -d path/to/my_file.py

You could use this information to guide the module-finder code, or to rewrite your code so you don't confuse py2exe.

Or, more simply, just explicitly include and exclude modules in your setup.py as a workaround, without worrying about why they're getting incorrectly detected.

abarnert
  • 354,177
  • 51
  • 601
  • 671
0

py2exe is python version dependent. Everything you're doing seems to be correct, I would guess you have the wrong version installed.

Chris Hawkes
  • 11,923
  • 6
  • 58
  • 68
  • 1
    Could be, but, I have Python 2.7.1 and installed py2exe-0.6.9.win32-py2.7.exe. Seems correct. – Doo Dah Mar 18 '13 at 17:13
  • yeah, sorry I use the same version and it's worked fine. You may want to try re installing it if you haven't already done so. – Chris Hawkes Mar 18 '13 at 18:23
  • sigh. I am having the same problem with cx-freeze, leading me to believe there is something about this that is preventing Python from finding the dependencies. – Doo Dah Mar 18 '13 at 18:32