I have a simple IronPython script:
# Foo.py
import os
def main():
print( "Hello" )
if "__main__" == __name__:
main()
It runs fine and prints Hello if I run it with IronPython as:
ipy Foo.py
Following the instructions given in IronPython – how to compile exe, I compiled this IronPython script to a EXE using:
ipy pyc.py /main:Foo.py /target:exe
Executing Foo.exe gives this error:
Unhandled Exception: IronPython.Runtime.Exceptions.ImportException: No module named os
at Microsoft.Scripting.Runtime.LightExceptions.CheckAndThrow(Object value)
at DLRCachedCode.__main__$1(CodeContext $globalContext, FunctionCode $functionCode)
at IronPython.Compiler.OnDiskScriptCode.Run()
at IronPython.Compiler.OnDiskScriptCode.Run(Scope scope)
at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptC
ode, ModuleOptions options)
Why cannot module “os” be found? How do I fix this, so I can get a working EXE?
(Note that this is different from the question IronPython cannot import module os since the script works fine if I run with ipy.exe.)
Building an Ironpython EXE that you can distribute is a bit tricky – especially if you are using elements of the standard library. My typical solution is the following:
I copy all of the stdlib modules I need into a folder (usually all of them just for completeness) and use this script to build my exe. In this example I have two files FredMain.py and FredSOAP.py that get compiled into an EXE called Fred_Download_Tool
In my scripts I add the following when I am ready to compile. This allows the program to use the Standard Modules from my DLL instead of the Python Install. This is necessary if you want to distribute to people without Python installed. Just make sure you include the necessary DLL’s when you create your installer.