Coder Social home page Coder Social logo

pyone-onefile-python-interpreter's Introduction

Onefile Python Interpreter

Make your application's python runtime compact and easy to maintain.
This is aimed toward people who want to keep/have their python application to run as scripts and not a compiled executable.

How do we make a python interpreter onefile?

To simply put we take advantage of the way how python scripts are compiled by tools like Nuitka or PyInstaller.

A compiled python script contains the following:

  1. Scripts
  2. Data/Files
  3. Python Installation it is was compiled with.

So we compile this specific base code for every onefile python interpreter.

from sys import argv, exit
from runpy import run_path
if len(argv) == 1: exit()
file = argv[1]; args = {}
for index, arg in enumerate(argv[2:]): args[index] = arg
run_path(file, init_globals=args, run_name='__main__')

We take advantage of runpy.run_path to execute our python scripts.

Advantages of making my own "Onefile Interpreter"

Note: These are advantages when you are dealing with only .py files and not python zipapps.

  1. Single/One file executable that can be used as your application's python interpreter.
  2. Add pip packages easily into your runtime by just importing them. (Compare this against, embedded python's method of including pip packages.)
  3. Easy to maintain.

Limitations

Onefile python interpreters have some limitations in regards with how executed scripts parse command-line arguments.
This all boils down to how runpy.run_path passes init_globals to the script that has to be executed.

Say:

example.exe file.py -a -b -c

is executed via the command-line.

Now in our compiled onefile interpreter sees the following:
['example.exe', 'file.py', '-a', '-b', '-c']

What our executed script sees:
['file.py', 'file.py', '-a', '-b', '-c']

We have a duplicate filepath entry.
Thus, this can mess with any scripts that deal with sys.argv or argparse.

Workarounds

Its actually easy to fix/workaround these limitations.

  1. Fixing duplicate filepath entires.
    Add the following at the beginning your script.
    Customize this workaround according to your needs!

    from sys import argv
    if len(argv) >= 2:
       if argv[0] == argv[1]:
          argv = argv[1:]   
  2. Fixing parsing issues with argparse.
    argparse doesn't respect the first workaround directly so we will need to explictly tell what arguments to parse.

    1. Add the first workaround for sys.argv.

    2. Find the .parse_args() function in your code and add the following argument:

      .parse_args(argv[2:])

      OR

      .parse_args(sys.argv[2:])

Adding in PIP Packages/User Made Modules

Its actually really easy!

Just import them into the base code:

import package1
import module1

from sys import argv, exit
from runpy import run_path
if len(argv) == 1: exit()
file = argv[1]; args = {}
for index, arg in enumerate(argv[2:]): args[index] = arg
run_path(file, init_globals=args, run_name='__main__')

and just compile the script!

Compiling the Onefile Python Interpreter

To compile your onefile python interpreter, do the following.

  1. Install the following PIP packages.
    pip install nuitka zstandard ordered-set
  2. Run build.bat and you are good to go!

pyone-onefile-python-interpreter's People

Contributors

aetopia avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.