How to remove the dependence of Python extensions on the UCRT - python

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.

Related

How to protect my Python code before distribution?

I have written a python code which takes an input data file, performs some processing on the data and writes another data file as output.
I should distribute my code now but the users should not see the source code but be able to just giving the input and getting the output!
I have never done this before.
I would appreciate any advice on how to achieve this in the easiest way.
Thanks a lot in advance
As Python is an interpreted language by design; and as it compiles code to a bytecode (- which doesn't help the fact you're trying to conceal it, as bytecodes are easier to reverse -) there's no real secure way to hide your source code whereby it is not recoverable, as is true for any programming language, really.
Initially, if you'd wanted to work with a language that can't be so easily reversed- you should've gone for a more native language which compiles directly to the underlying architecture's machine code which is significantly harder to reproduce in the original language let alone read due to neat compiler optimizations, the overhead given by CISC et cetera.
However, some libraries that do convert your source code into an executable format (by packing the Python interpreter and the bytecode alongside it) can be used such as:
cx_Freeze - for freezing any code >=Python 2.7 for any platform, allegedly.
PyInstaller - for freezing general purpose code, it does state additionally that it works with third-party libraries.
py2exe -for freezing code into Windows-only executable format.
Or you might consider a substitute for this, which is code obfuscation which still allows the user to read the source code however make it near-to-impossible to read.
However, an issue brought up with this is that, it'd be harder for code addition as bad code obfuscation techniques could make the code static. Also, on the latter case, the code could have overhead brought by redundant code meant to fool or trick the user into thinking the code is doing something which it is not.
Also in general it negates the standard practice of open-source which is what Python loves to do and support.
So to really conclude, if you don't want to read everything above; the first thing you did wrong was choose Python for this, a language that supports open source and is open source as well. Thus to mitigate the issue you should either reconsider the language, or follow the references above to links to modules which might help aide basic source code concealment.
Firstly, as Python is an interpreted language, I think you cannot completely protect your Python code, .pyc files can be uncompiled to get back .py files (using uncompyle6 for example).
So the only thing you can do is make it very hard to read.
I recommend to have a look at code obfuscation, which consists in making your code unreadable by changing variables/function names, removing comments and docstrings, removing useless spaces, etc. Pyminifier does that kind of things.
You can also write your own obfuscation script.
Then you can also turn your program into a single executable (using pyinstaller for example). I am pretty sure there is a way to get .py files back from the executable, but it just makes it harder. Also beware of cross-platform compatibility when making an executable.
Going through above responses, my understanding is that some of the strategies mentioned may not work if your client wants to execute your protected script along with other unprotected scripts.
One other option is to encrypt your script and then use an interpreter that can decrypt and execute it. It too has some limitations.
ipepycrypter is a suite that helps protect python scripts. This is accomplished by hiding script implementation through encryption. The encrypted script is executed by modifed python interpreter. ipepycrypter consists of encryption tool ipepycrypt and python interpreter ipepython.
More information is available at https://ipencrypter.com/user-guides/ipepycrypter/
One other option, of course, is to expose the functionality over the web, so that the user can interact through the browser without ever having access to the actual code.
There are several tools which compile Python code into either (a) compiled modules usable with CPython, or (b) a self-contained executable.
https://cython.org/ is the best known, and probably? oldest, and it only takes a very small amount of effort to prepare a traditional Python package so that it can be compiled with Cython.
http://numba.pydata.org/ and https://pythran.readthedocs.io/ can also be used in this way, to produce Python compiled modules such that the source doesnt need to be distributed, and it will be very difficult to decompile the distributable back into usable source code.
https://mypyc.readthedocs.io is newer player, an offshoot of the mypy toolkit.
Nuitka is the most advanced at creating a self-contained executable. https://github.com/Nuitka/Nuitka/issues/392#issuecomment-833396517 shows that it is very hard to de-compile code once it has passed through Nuitka.
https://github.com/indygreg/PyOxidizer is another tool worth considering, as it creates a self-contained executable of all the needed packages. By default, only basic IP protection is provided, in that the packages inside it are not trivial to inspect. However for someone with a bit of knowledge of the tool, it is trivial to see the packages enclosed within the binary. However it is possible to add custom module loaders, so that the "modules" in the binary can be stored in unintelligible formats.
Finally, there are many Python to C/go/rust/etc transpilers, however these will very likely not be usable except for small subsets of the language (e.g. will 3/0 throw the appropriate exception in the target language?), and likely will only support a very limited subset of the standard library, and are unlikely to support any imports of packages beyond the standard library. One example is https://github.com/py2many/py2many , but a search for "Python transpiler" will give you many to consider.

Sharing pygame/python withought using any compiler

I have tried many times to use a compiler like cx_freeze and other programs, but for some reason nothing seems to be working. I made a little game which I want to send to a friend, but he needs python installed.
Can't I just put python.exe and pygame into the folder that I will send my friend and won't python be installed then, and all he needs to do is run the program .py and it will work? Sorry if I'm not being clear.. I'm just trying to find a simple way to compile my code to let users not waste time on downloading pygame and python.
py2exe allows you to package python applications for Windows. Right now it supports everything from 2.4 - 3.1 of python. You do however need to be able to redistribute MSVCR90.dll.
There are a range of distribution tools and you can find a list here.
Since you've had difficultly with several tools now updating your question with error codes and speific problems will yeild better responses.
From my knowledge, just putting a bunch of your stuff in one folder and sending doesn't work. It would be easier to make a .exe
That way your stuff will be protected, and users can easily start it. Otherwise idk. Try using pyinstaller again. It should work if you have a python.x
CX_Freeze is known for having many bugs and problems, Pyg.exe is new to me also. Your best bet is just keep trying until you find a solution.
Putting python.exe and your script together in a folder will not work for distribution. You require all Python dependencies - at best your would need to include all of your Python folder, and it still might not work. The best method would be compilation or packaging with programs such as py2exe, cx_freeze, Cython, pyg.exe, etc.

What can I do to let PyCharm find the source of PySide?

I'm using PySide with PyCharm. When I want to jump to the source of a PySide class of method in editor, it always tell me:
Cannot find declaration to go to
I checked the dir python27/Lib/site-packages/PySide, there are many .pyc and .pyd files, but few *.py. I also download the source package from PySide site, but there are many .cpp and .xml files there.
How can I find the .py sources of PySide and let PyCharm to find them? Or I have to read the docs, which is not that convenient.
You may be misunderstanding something. .pyd files (that file extension is specific to windows, but the principle also applies to .so files on Linux and whatever file extension other systems use) are extensions written in C or C++ compiled to native code and put into a dynamically-linked library.
While you could look at the source if the project in question is open-source (in the case of PySide, it is), it's not Python (do you know C? C++? Quite a few good Python programmers don't) and littered with boilerplate code that contributes nothing but making the cross-language functionality happen. To make matters worse, in the case of PySide the C++ source code is generated automatically (from a tool called Shiboken, which presumably processes the xml files you've seen). You could also look at the source of the Qt functions that are wrapped, but again, this unlikely to buy you much.
So the bottom line is: You'll have to stick with the documentation (or read the original Qt documentation; I found it simple enough to translate to Python when I was using PyQt a while ago). Unless it's absolutely terrible (and the original Qt documentation isn't), it will also answer your question:
Sooner and with less effort. No need to make your way through thousands of lines of code just to get a rough idea of what that paramter implies.
Better. Things like best practices, deprecation warnings and gotchas aren't always visible from the code alone.
More correctly. Can you understand the code correctly, especially at first glance? I wouldn't bet I could.

Python4Delphi-powered program, how to deploy it?

My Delphi program uses Python4Delphi, and the Python script uses some standard libs. When deploying my program I don't want an entire Python installation, and it must work with python27.dll. What are the minimal set of necessary files? The document in Python4Delphi is dated and it's not clear to me...
Thanks for your help.
When I did this, I made the list myself, of what I needed for my embedded python application to work.
I remember this worked with python15.dll:
PythonXX.dll should work, without any other external files other than the Visual C++ Runtime DLLs, which require a side-by-side manifest (see the p4d wiki page) to work.
If you want to IMPORT something, then you need to ship it and anything it depends on. That means, either you pick part of the python standard libraries you want, or you pick all of it. There is no way you need all of Python's standard libraries. But I wouldn't want to live without OS and a a few other key ones. BUt the decision is yours.

Smallest possible windows executable from python script

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.

Categories