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
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.
11
11
12
-
GraalPy comes with a module, `py2bin`, that creates a Maven project skeleton to bundle and run self-contained JAR files with Python code.
13
-
The generated skeleton can also be used to generate a standalone binary that includes all the Python code using GraalVM Native Image.
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.
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.
14
16
15
-
## Usage
17
+
## Creating GraalPy Binaries
16
18
17
19
Suppose we have a simple Python script `my_script.py` that does some useful work when run directly.
18
20
If we wanted to distribute it as a standalone native image, we run the following command:
The target folder `my_script_application` will now include a `pom.xml` that makes it easy to generate a native image with maven.
25
-
Make sure to set your `JAVA_HOME` to use a GraalVM distribution that includes the native-image binary:
26
+
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.
27
+
Use `graalpy -m standalone binary --help` for further options.
28
+
29
+
## Embedding GraalPy in Java Applications
30
+
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:
Afterwards, the standalone binary can be found in the `target` directory.
31
-
It can be moved freely and launched directly, and does not require any additional resources.
38
+
The target folder `MyJavaApplication` includes a `pom.xml` that makes it easy to generate a JAR or a GraalVM native image with Maven.
39
+
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.
32
42
33
43
Take a look at the generated `pom.xml`.
34
44
There are some options to tweak the performance and footprint trade-off.
35
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.
36
46
37
47
The generated project should be viewed as a starting point.
38
-
It includes the entire Python standard library.
39
-
This can be manually pruned to reduce it to the necessary amount, reducing both the size of the binary and the time to start up.
48
+
It includes the entire Python standard library, so the Python code can invoke all of the standard library code.
49
+
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.
40
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.
41
51
42
52
## Security considerations
43
53
44
-
Creating a native image that includes the Python code could be seen as a mild form of obfuscation, but it does not protect your source code.
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.
45
55
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.
46
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.
Bundle the Python core, stdlib, venv, and module into a zip file.
85
87
"""
@@ -197,18 +199,16 @@ def main(args):
197
199
198
200
subparsers=parser.add_subparsers(required=True)
199
201
200
-
module_argument_args= {
201
-
"dest": "module",
202
-
"help": "Python file or module folder to run",
203
-
}
204
-
venv_argument_args= {"dest": "venv", "help": "Python venv to use", "nargs": "?"}
205
-
206
202
parser_bin=subparsers.add_parser(
207
203
"binary", help="Create a standalone binary from the Python code directly."
208
204
)
209
-
parser_bin.add_argument(**module_argument_args)
210
-
parser_bin.add_argument(**venv_argument_args)
211
-
parser_bin.add_argument('-o', '--output', required=True, help='Output filename for the binary')
205
+
parser_bin.add_argument(
206
+
"-m", "--module", help="Python file or module folder to run", required=True
207
+
)
208
+
parser_bin.add_argument("--venv", help="Python venv to bundle")
209
+
parser_bin.add_argument(
210
+
"-o", "--output", help="Output filename for the binary", required=True
211
+
)
212
212
parser_bin.add_argument(
213
213
"-Os", action="store_true", help="Optimize the binary for size, not speed"
214
214
)
@@ -226,10 +226,15 @@ def main(args):
226
226
help="Create a Java project from the Python code. This gives the most flexibility, as the project can be used to build both standalone Jar files or native binaries using Maven.",
227
227
)
228
228
parser_jar.add_argument(
229
-
"target", help="The directory to write the Java project to."
229
+
"-m", "--module", help="Python file or module folder to run", required=True
230
+
)
231
+
parser_jar.add_argument("--venv", help="Python venv to bundle")
232
+
parser_jar.add_argument(
233
+
"-o",
234
+
"--output-directory",
235
+
help="The directory to write the Java project to.",
0 commit comments