Ipyc not including json in standalone executable
Description
IronPython is not including the json module in a standalone executable when compiling one, resulting in an error message.
Steps to Reproduce
- Create a python script that imports json
- Compile using ipyc your-python-file.py /target:exe /standalone
- Run your-python-file.exe
Expected behavior:
Python script works
Actual behavior:
IronPython outputs this error message: Error occurred: No module named 'json'
Version Information
Microsoft Windows Version 22H2 (OS Build 22621.2283)
IronPython 3.4.1 (3.4.1.1000) [.NETFramework,Version=v4.6.2 on .NET Framework 4.8.9181.0 (64-bit)]
json is part of the standard library and would need to be included in the standalone assembly as well.
Similar issue for on ipy2: https://github.com/IronLanguages/ironpython2/issues/759
I copied the python standard library in the IronPython "Lib" directory to a new subdirectory of my project titled "Lib" and I added
import sys
sys.path.append("Lib")
to my python code. This fixed the python import errors, but now I am receiving a new error message from .NET: "Error occurred: Could not load file or assembly 'System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. Invalid pointer (Exception from HRESULT: 0x80004003 (E_POINTER))"
Hmm, I'm not able to reproduce the error with a simple program. Although it looks like ipyc is only embedding System.Memory and System.Runtime.CompilerServices.Unsafe so if a code path is trying to use System.Buffers then I guess it makes sense that it would fail... As a workaround I think you can explicitly include the assembly on build:
ipyc your-python-file.py /target:exe /standalone "C:\Program Files\IronPython 3.4\System.Buffers.dll"
Adding the System.Buffers library allows the code to run. However, when the UDPServer in my code receives a packet, it throws this error:
Exception happened during processing of request from ('127.0.0.1', 55451)
Traceback (most recent call last):
File "C:\Users\my-username\source\repos\python\thing\lib\socketserver.py", line 305, in _handle_request_noblock
Error occurred: Object reference not set to an instance of an object.
The weird thing is that this error does not happen when using the IronPython console to run my code.