0

I have been recently working on a text game (executes in the console or CMD). I already tested it and it works perfectly. But now I want to make it a SINGLE EXE. I have already done that with other scripts. The proble is that this game is actually made of 3 scripts:main.py ,maps.py, UInterface.py. This are all imported in the main file like this

   import maps;
   import UInterface as UI;

What can I do to have everything into a single exe and make it work? Repeat, my code already worked, just need compiling

Meowtwo 117
  • 72
  • 1
  • 5
  • 1
    You should be use py2exe the same way you did with single file scripts. Normally it's able to figure out what modules your script imports and include them along with everything else. – Ross Ridge Aug 07 '14 at 17:05
  • Do you mean I should only compile main.py and the rest will compile automatically? – Meowtwo 117 Aug 07 '14 at 21:33
  • Py2exe isn't a actually a compiler, it just bundles your Python code with the Python interpreter. But, yes, if you tell it make `main.py` into an executable it should find `maps.py` and `UInterface.py` and include them as well. – Ross Ridge Aug 07 '14 at 23:51

3 Answers3

0
  1. Download and install - py2exe (link here http://www.py2exe.org/)

  2. Create a setup.py like this (in the source dir):

    from distutils.core import setup
    import py2exe
    
    setup(console = ['main.py'])
    
  3. And then run as:

    python setup.py py2exe
    
  4. exe will get creted here .\dist\main.exe

K246
  • 1,077
  • 1
  • 8
  • 14
0

SITUATION:

  • You have a "main script" that calls "other scripts" (in the same folder or in packages) created by yourself.

SHORT ANSWER:

  • First: You need to compile with py2exe ONLY the main script.
  • Py2exe can't find your "other scripts". To fix it, copy that code at the beginning of setup.py (changing main_script_dir of course)

    import sys
    main_script_dir = "folder1\\folder2\\main.py"
    main_folder = main_script_dir.rsplit("\\",1)[0]
    sys.path.append(main_folder)
    

It removes the name of the script, and adds the folder to the PYTHONPATH.


LARGE ANSWER: (The explanation about how to arrive to the conclusion)

How can you test that py2exe can't find your "other scripts"?

  • Run py2exe with a simple .bat file that contents cd commands to find setup.py, the run command, and a wait time command that allows you to read the output information in the console after.
cd..
cd..
cd..
cd..
cd..
cd Folder1
cd Folder2

"C:\Python27\python.exe" setup.py py2exe

>nul ping -n 30 localhost
  • You can find now a warning like "The following modules appear to be missing" (use right click > find if you want)

Why did it happen?

  • Py2exe looks for "other scripts" similar to import function (in the modules already imported, the main script folder, pythonpath's directories, etc). However, py2exe omits the main script folder (maybe it's a bug). That's why you won't have problems if you copy your "other scripts" in site-packages folder, for example.

How can you solve this?

You have to add the main script directory to the pythonpath:

  • (not recommended) ‎If you modify it with control panel the changes will remain after the program closes.

(like this: How to add to the pythonpath in windows 7?)

  • (recommended) If you modify it with code (following the steps detailed above in the short answer), the changes will remain only until the program closes.

Good luck!

0

I come across this post trying to create a set of executables from some independent scripts. Eventually, I solved this by putting all the script names assigned to console. From the documentation, console is the keyword for a list of scripts to convert into console exes.

Example:

setup(console=[
    'script1.py',
    'script2.py',
    'script3.py'
    ]
)

Hope this helps somebody trying to do the same thing.

Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
pky
  • 11
  • 2