IronPython 3 Documentation

repository·main·Indexed 25 days ago

https://github.com/ironlanguages/ironpython3

An open-source implementation of Python 3 tightly integrated with the .NET ecosystem. It allows developers to use Python code within .NET applications, access .NET libraries via the clr module, and compile Python files into .NET assemblies or executables using ipyc.exe. The implementation targets Python 3.4 and provides a standalone interpreter (ipy) and a separate IronPython.StdLib package for standard library functionality.

Tokens
6.8K
Snippets
10
Records
83
Agent score
83%

What's inside IronPython 3

  1. Overview of IronPython 3 Repository Structure

    main

    The repository is organized into the following functional areas:

    • IronPython: Contains the core compiler and runtime (IronPython.dll).
    • IronPython.Tests: Targeted compiler and runtime unit tests executed via a C# test harness.
    • Public: Contains miscellaneous documentation and tutorials.
    • Samples: Demonstrations of specific features, such as a comment checker and voice capabilities.
    • Scripts: Infrastructure scripts and helper utilities.
    • StdLib: Python standard library imports.
    • Tests: General test suites.
  2. Overview of IronPython 3

    main

    IronPython is an open-source implementation of the Python programming language designed for tight integration with the .NET Framework. It allows users to utilize both .NET Framework and Python libraries, and enables other .NET languages to consume Python code seamlessly.

    This package provides a standalone Python interpreter accessible via the ipy command. It includes a modified version of the Python Standard Library optimized for IronPython and .NET environments. The current target version is Python 3.4, though it may include features from later CPython versions.

  3. Handle Text (str) vs. Data (bytes)

    main

    Python 3 distinguishes between text (str) and binary data (bytes).

    • Text: All text is Unicode. Use str for text.
    • Data: Use bytes for binary data. bytes is immutable; use bytearray for mutable buffered binary data.
    • Conversion: You must explicitly convert between them. Use str.encode() to convert str to bytes, and bytes.decode() to convert bytes to str.
    • Literals: Use b'...' for bytes literals.
    • I/O: open() defaults to text mode. Use open(..., 'rb') for binary mode.
  4. Use the IronPython ipy shim in PowerShell

    main

    After installation, you can enter the IronPython environment using the Enter-IronPythonEnvironment.ps1 script, which provides the ipy command for executing Python commands.

    & "~/ipyenv/v3.4.2/Enter-IronPythonEnvironment.ps1"
    
    ipy -c "print('Hello from IronPython!')"
  5. Embed the IronPython engine in a .NET application

    main

    Use the IronPython engine to embed a Python interpreter within a .NET application. This allows interpreted Python code to call .NET code and vice versa.

    Note: This package contains only the engine. To run most Python packages and standard functionality, you must also include the IronPython.StdLib package, which is distributed separately.

    var eng = IronPython.Hosting.Python.CreateEngine();
    var scope = eng.CreateScope();
    eng.Execute(@"
    def greetings(name):
        return 'Hello ' + name.title() + '!'
    ", scope);
    dynamic greetings = scope.GetVariable("greetings");
    System.Console.WriteLine(greetings("world"));