Skip to content Skip to sidebar Skip to footer

How To Make Python API Using Py2exe?

Is it possible to 'compile' a Python script with py2exe (or similar) and then allow the user access to modify the top-level Python scripts? Or possibly import the compiled module

Solution 1:

I figured out how to get it to work. I placed my "head" framework file in the "includes" list in the setup.py file. Then, I have a compliled runner that uses the imp module to dynamically load regular Python scripts, and those scripts call upon that head framework file. This is exactly the kind of hidden framework, yet reachable API that I was looking for.

For example, let's say we have a directory called "framework" with a master file "foo" that contains all of the API calls. The line in the py2exe setup.py file would look like this:

includes = ['framework.foo', 'some_other_module', 'etc']

I then make a target for this runner script:

FrameworkTarget = Target(
    # what to build
    script = "run_framework.py",
    dest_base = "run_framework"   
    )

Then add the target to the setup() command in the setup.py script among the other things:

console = [FrameworkTarget],

The compiled runner script is passed the name of the "test suite" script from the command line:

test_suite_name = sys.argv[1]
file_name = test_suite_name + ".py"
path_name = os.path.join(os.getcwd(), file_name)
print "Loading source %s at %s"%(file_name, path_name)
module = imp.load_source(file_name, path_name )

Then, in the file called by the imp.load_source() command, I have this:

import framework.foo

When I didn't have 'framework.foo' in my includes, it couldn't find the compiled version of framework.foo. Maybe someone will find this useful in the future. I don't know if I could do one useful thing without Stackoverflow!


Solution 2:

Is it possible to "compile" a Python script with py2exe (or similar) and then allow the user access to modify the top-level Python scripts?

I'm not particularly familiar with py2exe, but looking at the tutorial page, it would seem relatively straightforward to replace the hello.py script with something along the lines of...

import sys
import os

# Import your framework here, and anything else you want py2exe to embed
import my_framework

TOP_LEVEL_SCRIPT_DIR = '/path/to/scripts'
MAIN_SCRIPT = os.path.join(TOP_LEVEL_SCRIPT_DIR, 'main.py')

sys.path.append(TOP_LEVEL_SCRIPT_DIR)
execfile(MAIN_SCRIPT)

...and put any scripts you want the user to be able to modify in /path/to/scripts, although it'd probably make more sense to define TOP_LEVEL_SCRIPT_DIR as a path relative to the binary.

The reason that I am using py2exe is because I have some troublesome libraries (paramiko/pycrypto, plus some internally developed ones) that I don't want to require my customers to trudge through those installations. I also don't want them to have open access to my framework files.

If the goal is ease of installation, it might also suffice to create a regular InstallShield-esque installer to put all the files in the right places, and just include the .pyc versions of your "framework files" if you don't want them reading the source code.


Post a Comment for "How To Make Python API Using Py2exe?"