9

I have a Python script I developed within a virtualenv on Windows (Python 2.7).

I would now like to compile it into a single EXE using Py2exe.

I've read and read the docs and stackoverflow, and yet I can't find a simple answer: How do I do this? I tried just installing py2exe (via the downloadable installer), but of course that doesn't work because it uses the system-level python, which doesn't have the dependencies for my script installed. It needs to use the virtualenv - but there doesn't seem to be such an option.

I did manage to get bbfreeze to work, but it outputs a dist folder crammed with files, and I just want a simple EXE file (one file) for my simple script, and I understand Py2Exe can do this.

tl;dr: How do I run Py2Exe within the context of a virtualenv so it correctly imports dependencies?

ezuk
  • 3,096
  • 3
  • 30
  • 41
  • You probably need to install py2exe into your virtual environment from source the same way you'd install any other module from source. That way py2exe will exist in the same place as all of your other dependencies. – g.d.d.c Nov 20 '13 at 19:24
  • This thread http://stackoverflow.com/questions/5989364/how-can-i-build-py2exe-without-errors shows how hard building it on Windows is. Is that really the only option? – ezuk Nov 24 '13 at 16:09
  • It's not that hard, though. If you retrieve the source and have Visual Studio [Express] installed, you launch a command prompt and call `python.exe setup.py install`. The binaries should work as well, but I have little experience with virtual environments. – g.d.d.c Nov 24 '13 at 21:36
  • I've successfully installed both py2exe and virtualenv, but when I try using py2exe from within a virtualenv, running the executable produced yields the exception: Traceback (most recent call last): File "CellProfiler.py", line 67, in File "site.pyc", line 658, in File "site.pyc", line 625, in main File "site.pyc", line 538, in virtual_install_main_packages IOError: [Errno 2] No such file or directory: 'X:\\projects\\CellProfiler-javabr idge-2\\CellProfiler\\dist\\library.zip\\orig-prefix.txt' I think the two might not play well together. – Lee Kamentsky Aug 05 '14 at 15:01

2 Answers2

11

You can do that this way:

  1. Activate your virtualenv and then ...
  2. easy_install py2exe-0.6.9.win32-py2.7.exe
hexo
  • 111
  • 3
1

Installing py2exe into your virtual env should be straightforward. You'll need Visual Studio 2008, the express version should work. Launch a 2008 Command Prompt and Activate your virtual env. Change into the directory that contains the py2exe source and run python setup.py install. You can verify that py2exe is in the correct environment by attempting to import it from an interactive shell. I tested myself earlier today (had to install virtualenv). It works exactly as expected.

g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • 1
    Accepting this even though I won't use it myself, because it seems like the only way. Too bad it requires installing something as big as VS2008... – ezuk Nov 25 '13 at 19:02