How to compile multiple scripts with py2exe?
Question:
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
Answers:
-
Download and install – py2exe (link here http://www.py2exe.org/)
-
Create a setup.py like this (in the source dir):
from distutils.core import setup
import py2exe
setup(console = ['main.py'])
-
And then run as:
python setup.py py2exe
-
exe will get creted here .distmain.exe
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:Python27python.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!
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.
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
-
Download and install – py2exe (link here http://www.py2exe.org/)
-
Create a setup.py like this (in the source dir):
from distutils.core import setup import py2exe setup(console = ['main.py'])
-
And then run as:
python setup.py py2exe
-
exe will get creted here .distmain.exe
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:Python27python.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!
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.