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
@@ -101,7 +100,7 @@ To run this example, first install the required R library:
101
100
R -e 'install.packages("https://www.rforge.net/src/contrib/jpeg_0.1-8.tar.gz", repos=NULL)'
102
101
```
103
102
104
-
This example also uses [image_magix.py](http://graalvm.org/docs/examples/image_magix.py) and works
103
+
This example also uses [image_magix.py](https://www.graalvm.org/resources/img/python/image_magix.py) and works
105
104
on a JPEG image input (you can try with [this image](https://www.graalvm.org/resources/img/python_demo_picture.jpg)). These files have to be in the same folder that the script below is located in and executed from.
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
+
12
+
GraalPy comes with a module that can create standalone binaries or Java project skeletons.
13
+
The binaries bundle everything into one native executable.
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 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.
20
+
21
+
## Creating GraalPy Binaries
22
+
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:
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.
31
+
Use `graalpy -m standalone binary --help` for further options.
32
+
33
+
## Embedding GraalPy in a Java Application
34
+
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:
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.
43
+
You can open this Maven project with any Java IDE and edit the main class that was created to modify the Python embedding.
44
+
To build the application, either `mvn -Pjar package` to create a JAR file, or `mvn -Pnative package` to create a GraalVM native executable.
45
+
46
+
Take a look at the generated `pom.xml`.
47
+
There are some options to tweak the performance and footprint trade-off.
48
+
Review the [Python Native Images documentation](PythonNativeImages.md) to find out how to remove other unwanted components and further reduce the binary size.
49
+
50
+
The generated project should be viewed as a starting point.
51
+
It includes the entire Python standard library, so the Python code can invoke all of the standard library code.
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.
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.
54
+
55
+
## Security Considerations
56
+
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.
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.
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