I've looked at some projects like py2exe and a couple other ones. But I want really small under 1mb would be cool but under 300kb would be ideal. Are there any project to possibly extern python into a C compiler or even convert python to C?
Maybe there is just a really small python interpreter that I could add only the required libraries to? ( I tried one, it didn't really seem to be "python")
I've looked at a lot of options but I haven't tried them out properly... What can you recommend?
I am willing to put the effort into a more complex process if the payoff(small size) is worth it, but I need to narrow down the list.
The Shedskin solution in Is it feasible to compile Python to machine code? is what you look for however it is not compile an arbitrary python code. And it is not compile to C but to C++.
I mean the closest you will get with python is py2exe and UPX Create the exe but don't bundle anything to it instead make a library then use upx and do the best compression. It has little overhead and can trim another few mb off your final product. Its not the best way but its one of them.
The other option would be to write a snub launcher in C or C++ to fire up a python interpreter and run your source code. While this would be much smaller it would probably be far more complicated to make, if you are looking to follow this method then this link will help you with that.
Related
I read somewhere you could run python without an OS. How would I do this? Would I need to compile it? Can I run it raw? And if I did need to compile it, what tool would I use and what format would I compile it to?
As far as I know there's not really any way to do this easily but I could be wrong. There are "portable" versions of python but these are operating system dependent. I think what you're referencing is some guys at PyCon managed to run python from the GRUB Bootloader. Your best bet would be installing some minimalist Linux distribution, with essentially only Python and some core packages required to run. The problem is that there's a lot of types of hardware out there, all with their own drivers and assembly language. Python can work as a low level language when you need it to but it seems like configuration would be a nightmare. I haven't looked into it super thoroughly but it seems difficult and impractical. Having an OS above python gives you access to the package managers IDEs and compilier options that make python worth using.
yea that's one of the options, pretty much all the "light" distros will be similar, if you want more to try out try here. Not sure why you're worried about speed though, if you're having speed issues it's far more likely to be the IDE you're using or your code bogging down the computer, not any sort of compiler issues.
I am using cython to generate *.c files, to be later compiled with the MS Visual Studio 2017 as C/C++. It all works splendid, with the minor exception that all python *.lib were dynamically linked.
Since my goal is to produce a self-contained exe (large exe size is not a problem), I would like to ask if it is possible to static-link all the Python *.lib. I already tried specifying the \MT release option and defining all Python libraries on the Debugger include.
Unfortunately, all my efforts were futile, since the dynamically linked executable can't find the python3.dll when copied to another computer. Currently I plan to copy the Entire python install directory together with the executable and specify the proper include links when compiling.
Therefore, I am interested in any option, it it exists, to produce a self-contained portable executable.
I would appreciate your help and advice.
xaav is correct.
I cannot comment so Instead I will post this as a solution in the hopes it will direct you to right path.
Cython exists for a reason. You get your python code, add a few changes and bam, your code is cythonised.
This is good for two reasons. To obfuscate the code and it can speed up the code (depends).
Why do you not use cython and pyinstaller? This is tried and tested. Pyinstaller even says that it supports it. The approach you are taking can be done in theory but it is so overly complicated and not even needed.
Possible concerns:
But can't they steal my source code? No, it's cythonised so yes but not easily.
Can't I use Nuitka? Yes, if you want it to be buggy and not work as intended.
What about the libraries, they do not work on another pc? Spec files exist for a reason. A bit of manual handling and this can work.
Can't I compile to c++ and then make it standalone? Take a look at the number of unanswered questions and people who could not get it to work. Also, it is not needed when pyinstaller and cython exist and does the same thing. Cython is widely supported. It just feels like you are doing things the long and hard way.
But won't compiling to c++ be easier. No way, pyinstaller already does most of the leg work. You might have to adjust the spec file here and there, but otherwise it's the only way to go. Keep in mind it also has integration with pyupdater too.
I'm having some trouble with distributing my python programs. for linux it's easy enough, just tell people which packages they need and provide the source. for windows however it's a different story altogether. I can get my program compiled with py2exe. the problem there however is that it always seems to miss one dll file or the other.
my question therefore is, is there some easy way to package a python script for multiple platforms? right now I keep having to go on a wild goosechase after dll files I don't have. and this isn't even mentioning the 32bit vs. 64bit problem which leaves a lot to desire.
any and all pointers are welcome. the ideal solution would be some sort of script that would create two zipfiles. one being linux/source and one for windows64bit/windows32bit. I really don't care all that much about mac support, but it would be welcome.
If you support linux, you probably support mac in the same way.
I typically use py2exe to distribute to windows, but there are alternatives, such as cxfreeze, and others. I haven't bothered to care much about 64/32 bit and just deliver a 32 bit solution.
Here are a list of packagers:
http://www.freehackers.org/Packaging_a_python_program
Is it possible to deploy python applications such that you don't release the source code and you don't have to be sure the customer has python installed?
I'm thinking maybe there is some installation process that can run a python app from just the .pyc files and a shared library containing the interpreter or something like that?
Basically I'm keen to get the development benefits of a language like Python - high productivity etc. but can't quite see how you could deploy it professionally to a customer where you don't know how there machine is set up and you definitely can't deliver the source.
How do professional software houses developing in python do it (or maybe the answer is that they don't) ?
You protect your source code legally, not technologically. Distributing py files really isn't a big deal. The only technological solution here is not to ship your program (which is really becoming more popular these days, as software is provided over the internet rather than fully installed locally more often.)
If you don't want the user to have to have Python installed but want to run Python programs, you'll have to bundle Python. Your resistance to doing so seems quite odd to me. Java programs have to either bundle or anticipate the JVM's presence. C programs have to either bundle or anticipate libc's presence (usually the latter), etc. There's nothing hacky about using what you need.
Professional Python desktop software bundles Python, either through something like py2exe/cx_Freeze/some in-house thing that does the same thing or through embedding Python (in which case Python comes along as a library rather than an executable). The former approach is usually a lot more powerful and robust.
Yes, it is possible to make installation packages. Look for py2exe, cx_freeze and others.
No, it is not possible to keep the source code completely safe. There are always ways to decompile.
Original source code can trivially be obtained from .pyc files if someone wants to do it. Code obfuscation would make it more difficult to do something with the code.
I am surprised no one mentioned this before now, but Cython seems like a viable solution to this problem. It will take your Python code and transpile it into CPython compatible C code. You also get a small speed boost (~25% last I checked) since it will be compiled to native machine code instead of just Python byte code. You still need to be sure the user has Python installed (either by making it a pre-requisite pushed off onto the user to deal with, or bundling it as part of the installer process). Also, you do need to have at least one small part of your application in pure Python: the hook into the main function.
So you would need something basic like this:
import cython_compiled_module
if __name__ == '__main__':
cython_compiled_module.main()
But this effectively leaks no implementation details. I think using Cython should meet the criteria in the question, but it also introduces the added complexity of compiling in C, which loses some of Python's easy cross-platform nature. Whether that is worth it or not is up to you.
As others stated, even the resulting compiled C code could be decompiled with a little effort, but it is likely much more close to the type of obfuscation you were initially hoping for.
Well, it depends what you want to do. If by "not releasing the source code" you mean "the customer should not be able to access the source code in any way", well, you're fighting a losing battle. Even programs written in C can be reverse engineered, after all. If you're afraid someone will steal from you, make them sign a contract and sue them if there's trouble.
But if you mean "the customer should not care about python files, and not be able to casually access them", you can use a solution like cx_Freeze to turn your Python application into an executable.
Build a web application in python. Then the world can use it via a browser with zero install.
I don't get what Clyther is or how to use it.
My stuff:
ATI OpenCl SDK (just dl'd)
clyther beta (just dl'd)
windows 7 pro 64 bit
active python 3.1.2
Xfxs Ati radeon 5850 video card
I downloaded the ATI OpenCl SDK and the clyther beta from sourceforge. Then I tooke the sample 'reduce' function from the sourceforge documents and pasted the code into notepad and named it clythersample.py. When I double-click the file or open it in the interactiveshell, it gives an error message on the first line.
Is naming the file .py wrong? I guess clyther is its own lqnguage and not really python? Can I write python code and in the middle of the program, write a chunk of clyther code? Will python IDEs (esp. Wing understand and debug it?) Will it work with python 3 or do I need 2.6? Is 64 bit os ok?
(I'm not a programme or technically competent, so things like its a python API for OpenCl or it had C bindings for python don't mean a whole lot).
Clyther is a Python package for High-Performance Computing (HPC) using, for example, video cards with multiple Graphics Packaging Units (GPUs) or (less frequently) multi-core processors. Clyther is for parallel processing of algorithms or data sets that would normally take a lot of time to process serially. Meaning, if you have a problem that can be split into many smaller problems, then Clyther is a useful package to use. Additionally, your problem must be something that can use numpy arrays.
Clyther is a nice package to use if you have the problem it is intended to solve. It makes it fairly easy to write Python code to run on multiple processes.
If that's not the problem you need to solve, then Clyther probably won't help you.
When you name the file .py (the file extension associated with python.exe) and double-click it, how is windows supposed to know it's supposed to run the file with CLyther?
Is naming the file .py wrong? I guess clyther is its own lqnguage and not really python?
Documentation compares it with Cython - so I suppose it's an extension to the language, i.e. they take Python and bolt more features on it.
Can I write python code and in the middle of the program, write a chunk of clyther code?
From the goals of the project, it should (like Cython) ultimately be able to accept all or most Python code. So, yes - but of course you'd have to run the whole program with CLyther.
Will python IDEs (esp. Wing understand and debug it?)
Not natively, I suppose.
Side note: The project is in a very early stage of developement (very first beta-release), so don't expect things to run smoothly right now.