Is there a way to create an executable that runs both Python and Perl script?

爱⌒轻易说出口 提交于 2020-07-10 07:20:25

问题


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 install Path::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 erasing PATH:

    $ 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!