IronLanguages / ironpython3

Implementation of Python 3.x for .NET Framework that is built on top of the Dynamic Language Runtime.
Apache License 2.0
2.48k stars 287 forks source link

Ipyc not including json in standalone executable #1748

Open JonahEMorgan opened 11 months ago

JonahEMorgan commented 11 months ago

Description

IronPython is not including the json module in a standalone executable when compiling one, resulting in an error message.

Steps to Reproduce

  1. Create a python script that imports json
  2. Compile using ipyc your-python-file.py /target:exe /standalone
  3. 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)]

slozier commented 11 months ago

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

JonahEMorgan commented 10 months ago

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))"

slozier commented 10 months ago

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"
JonahEMorgan commented 10 months ago

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.