问题
I'm building a Python program that also runs a Perl script from within. Is there a way to create an executable where the user of my program doesn't have to install Perl but has Python?
回答1:
Here is an example of how you can use pp to build an executable (that does not depend on the perl
executable being installed).
I am using perlbrew
with perl
version 5.30 on Ubuntu 20.04.
First install
pp
:cpanm PAR::Packer
Create a test Perl script
hello.pl
(you may need to installPath::Tiny
first):use feature qw(say); use strict; use warnings; use Path::Tiny; # <-- NOTE: non-core module used say "Hello world! CWD = ", Path::Tiny->cwd;
Pack it into an executable:
pp -o hello hello.pl
Test that the Perl script is independent of the
perl
executable, by erasingPATH
:$ PATH= ./hello Hello world! CWD = /home/hakon/pp
Create a test Python script,
t.py
:import os os.system("./hello")
Run the Python script:
$ python3 t.py Hello world! CWD = /home/hakon/pp
I also tested this with a Docker container where I transferred the compiled hello
executable to the container and then ran hello
from within the container.
Note:
If you transfer this executable to a machine with a different version of the core libraries (like glibc
) than those used on the machine where the executable was built, the executable might fail to run on the target machine. See this post for similar issue in Python and further discussion of this problem.
来源:https://stackoverflow.com/questions/61486665/is-there-a-way-to-create-an-executable-that-runs-both-python-and-perl-script