"py_compile" result execution/sharing?
Hello. Happy new year everybody.
My Name is Roberto, and I've been around quiet for a little while... :)
So, I want to share a little app with somebody, but I don't want to distribute my code. There's no way to compile to an app anymore it seems, at least not Python 3, right? Anyway, I was thinking into compiling to at least bytecode "pyc", and I did compile it using the console by typing:
import py_compile py_compile.compile('myprogram.py')
And I've got a "pycache" folder with "myprogram.cpython-36.pyc" correctly, but when I tap on it, it won't execute or anything, but instead, it ask me "Open with...".
I personally think Pythonista is the best Python app for iOS, but it would be great if you can share/distribute your creation without giving away the whole source code. Of course, I'd prefer to be able to generate a native separate app with my python code, but I also understand the reasons why it's not available anymore as well; still, I think it would be a good idea to have Pythonista generate/compile py files, and be able to execute pyc files as any regular py file, including setting it as a widget and all...
Thank you for your awesome work.
Oh I just saw this: https://forum.omz-software.com/topic/5310/when-will-omz-release-an-xcode-template-for-python3
Last time I checked was a couple of weeks ago, and now I did a search about compiling instead. I am sorry.
I will take a look a that. Still, I think the bytecode compilation, execution and sharing, would be an "amazing" addition to Pythonista; as it will allow us to share/distribute an app without the need of giving away the code, and at the same time, people will need to get Pythonista from the App store too, right?... :-)
JonB last edited by
You should be able to import pyc files that are in your path (e.g site-packages).
For instance you could provide a script that imports a pyc in the same folder then runs its main method. That way you distribute a zip file, then unzip it and can run the loader file via the Pythonista menu.
I'm not 100% sure, but I think you can use execfile on pyc files.
Note that pyc files offer practically zero security, just from casual observation, but won't protect sensitive algorothms and are easily decompiled. Dont stick passwords in a pyc thinking it will be safe.
Also, they only work with the same version of python.
@JonB Thanks for the suggestion. Yeah I understand that it’s not impossible to transform it back to the original "or similar" code, but well, at least, it's a "little" more secure as it requires some expertise and steps to decompile it as well, and if you see it just like that, you will see just garbage, right? Then there's something not less important, is that byte-compiled modules will increase the performance a little bit, load time is shorter, etc. I think it is a good idea to be able to compile any code/module and then be able to distribute/execute the .pyc file instead of the .py source code... I also know you can write a wrapper using marshal, passing the first bytes, and exec, but it would be cool if you can simply execute it directly as you do with .py files with Pythonista... :-)
JonB last edited by
it may decrease load time a little, but wont affect performance (all code gets byte compiled, pyc just lets you do it beforehand). Except for the tiny bit of obfuscation you get, there is no advantage, and many disadvantages (upgrading pythonista now breaks whatever is around).
Don't mess with this. Your code is not so special that anyone is going to steal and monetize it.
"it may decrease load time a little, but wont affect performance (all code gets byte compiled, pyc just lets you do it beforehand). Except for the tiny bit of obfuscation you get, there is no advantage, and many disadvantages (upgrading pythonista now breaks whatever is around)."
Yeah well, point is, it will gain some performance... There's a reason why Python supports it... Plus, it allows me to distribute my code less "easy to decompile" for some people... Yeah I know, I can always create a full app, but also, I think it is convenient for the creator to get people buying Pythonista and use my byte-code app than just everybody creating separate apps... I think...
"Don't mess with this. Your code is not so special that anyone is going to steal and monetize it."...
Hmmm, wether my code is or isn't special is completely off topic, concerning my question about executing/distributing pyc.
But thanks for your opinions on the matter!