You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It can be desirable to distribute Python applications or libraries as standalone binaries or JAR files without any external dependencies.
10
-
The Truffle framework that GraalPy is built on and the Sulong LLVM runtime that GraalPy can leverage for managed execution of Python's native extensions allow us to completely virtualize all filesystem accesses of Python programs, including those to the standard library and installed packages.
9
+
With GraalVM Python implementation (GraalPy), you can distribute Python applications or libraries as standalone binaries or JAR files without any external dependencies.
10
+
The [Truffle framework](https://github.com/oracle/graal/tree/master/truffle) that GraalPy is built on, and the [Sulong LLVM runtime](https://github.com/oracle/graal/tree/master/sulong) that GraalPy leverages for managed execution of Python's native extensions enables users to completely virtualize all filesystem accesses of Python programs, including those to the standard library and installed packages.
11
11
12
12
GraalPy comes with a module that can create standalone binaries or Java project skeletons.
13
-
The binaries simply bundle everything into one native image.
13
+
The binaries bundle everything into one native executable.
14
14
The Java skeletons are set up with Maven to build and run self-contained JAR files.
15
-
They can also be used to generate a standalone binaries from those JARs later, so the Java skeletons offer more flexibility and control over the steps.
15
+
They can also be used to generate a standalone binary from those JARs later, so Java skeletons offer more flexibility and control over the steps.
16
+
17
+
### Prerequisite
18
+
19
+
Set `JAVA_HOME` to use a GraalVM distribution.
16
20
17
21
## Creating GraalPy Binaries
18
22
19
-
Suppose we have a simple Python script `my_script.py` that does some useful work when run directly.
20
-
If we wanted to distribute it as a standalone native image, we run the following command:
23
+
Suppose there is a simple Python script,`my_script.py`, that does some useful work when run directly.
24
+
To distribute it as a standalone native binary, run the following command:
This will generate a standalone `my_binary` file which includes the Python code, the GraalPy runtime, and the Python standard library in a single, self-contained executable.
30
+
It generates a standalone `my_binary` file which includes the Python code, the GraalPy runtime, and the Python standard library in a single, self-contained executable.
27
31
Use `graalpy -m standalone binary --help` for further options.
28
32
29
-
## Embedding GraalPy in Java Applications
33
+
## Embedding GraalPy in a Java Application
30
34
31
-
Suppose now we wanted to distribute our `my_script.py` script as a JAR that can run on any GraalVM that includes GraalPy.
32
-
To prepare such a Java project, the `standalone` GraalPy tool has another command:
35
+
You can distribute the Python script as a JAR file that runs on GraalVM and includes GraalPy.
36
+
To achieve this, run the `java` subcommand of GraalPy's `standalone` module:
The target folder `MyJavaApplication`includes a `pom.xml` that makes it easy to generate a JAR or a GraalVM native image with Maven.
42
+
It creates a Java project _MyJavaApplication_. It includes a `pom.xml` that makes it easy to generate a JAR or a GraalVM native executable with Maven.
39
43
You can open this Maven project with any Java IDE and edit the main class that was created to modify the Python embedding.
40
-
To build, you can use either `mvn -Pjar package` to create a JAR, or `mvn -Pnative package` to create a GraalVM native image.
41
-
For both, make sure to set your `JAVA_HOME` to use a GraalVM distribution.
44
+
To build the application, either `mvn -Pjar package` to create a JAR file, or `mvn -Pnative package` to create a GraalVM native executable.
42
45
43
46
Take a look at the generated `pom.xml`.
44
47
There are some options to tweak the performance and footprint trade-off.
45
-
Review also our general documentation on Python native images to find out how to remove other unwanted components and further reduce the binary size.
48
+
Review the [Python Native Images documentation](PythonNativeImages.md) to find out how to remove other unwanted components and further reduce the binary size.
46
49
47
50
The generated project should be viewed as a starting point.
48
51
It includes the entire Python standard library, so the Python code can invoke all of the standard library code.
49
52
The resources can be manually pruned to reduce the included Python libraries to the necessary amount, reducing both the size of the package and the time to start up.
50
-
The Java code demonstrates some useful default options for the Python context, but other settings may be desirable to further control what the Python code is allowed to do.
53
+
This Java example demonstrates some useful default options for the Python context, but other settings may be desirable to further control what the Python code is allowed to do.
51
54
52
-
## Security considerations
55
+
## Security Considerations
53
56
54
-
Creating a native image or a JAR that includes the Python code could be seen as a mild form of obfuscation, but it does not protect your source code.
57
+
Creating a native executable or a JAR that includes the Python code could be seen as a mild form of obfuscation, but it does not protect your source code.
55
58
While the Python sources are not stored verbatim into the image (only the GraalPy bytecode is), that bytecode is easy to convert back into Python sources.
56
-
If stronger protection for the included Python source code is required, this needs to be handled by a means of e.g. encryption of the resources before building the native image, and adding approproate decryption into the generated virtual file system.
59
+
If stronger protection for the included Python source code is required, consider, for example, encryption of the resources before building the native executable, and adding appropriate decryption into the generated virtual file system.
0 commit comments