This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I make an EXE file from a Python program?
I am looking for a method in which i can run python program without having packages installed in the system.
Is there any way by which we can directly run any python code as an *exe.
or Should I make a executable file which has all packages in it.
The main reason behind this is, able to run python program on system which is not having python/packages in it.
http://www.pyinstaller.org/ is another good option. Does a lot of the same things py2exe does and suffers from the same problems.
py2exe is what you want. This will build an executable from your source.
The downside is: it packs an interpreter with the exe, so the filesize might become bigger. And when you use external libraries, especially C-bindings, you'll have to make manual steps.
Related
This question already has answers here:
How exactly is Python Bytecode Run in CPython?
(4 answers)
Closed 2 years ago.
Is it possible to run python bytecode files without the need of the C Python interpreter to be installed on the host OS ?
No, python interpreter is required.
You can use apps such as pyinstaller to make a executable of your scripts so that all required packages and python libs including the interpreter is self contained in a single executable. It runs like any other programs so nothing else needs to be done except double click and run.
Also .pyc files require the specific version of python to run with which they are compiled so it is really not a recommended way of distributing python code if thats what you are planning.
This answer has more details: https://stackoverflow.com/a/36027342/4289062
This question already has answers here:
Using both .so and .dll on Windows
(3 answers)
Closed 4 years ago.
I working on one stuff which is taking a lot of time to execute by using Python code in Windows OS. Hence I decided to use Cython. But in Windows 10 configuring c compiler by using Mingw, felt like a lot of things to done and its not working also. Hence decided to go with Linux to generate .so file and later use that in windows by importing it.
First of all my question, Is that possible to import in windows a .so generated in Linux. If Yes, How can I do that?
Thanks
While it's technically possible, it's almost certainly not what you want or reasonable effort. Some methods that come to mind are using a loader (also known as dynamic linker) that understands the appropriate format, such as the venerable cross-elf (archive.org snapshot), use an emulation layer such as qemu, or a high level virtualization shim like User-mode Linux. In each of these cases, you'd need to run your entire CPython under that same layer, which means it wouldn't have access to Windows features. One of the few projects that did go as far as implementing their own dynamic linker is XFree86.
This question already has answers here:
Create a directly-executable cross-platform GUI app using Python
(13 answers)
Closed 8 years ago.
I wrote a small program in Python. Actually it's not even a program really :p.
It's just this:
print(1+1)
When I saved it, it saved it as py1.py (a python file). And apparently, Python files can be executed, and it executed fine.
Is there anyway to be able to compile it into bytecode? Also is there a way to make it a stand alone application?
I could be getting terms wrong, I'm more of a Java person. I'm new to Python.
Thanks!
First of all, as #emh pointed out this is a duplicate of several other questions. One of them is stackoverflow.com/questions/2933/an-executable-python-app
There are several sets of scripts and modules that are available that allow you to convert a .py file into an standalone executable file. Some of the more popular ones are py2exe, py2app, Pyinstaller, and Cx_freeze. The one that is best for you depends you how you are using it. First, it depends on what operating you are using. py2exe is meant specifically for creating .exe files or windows executable files, py2app is the same as py2exe except it builds .app files for Mac OS, pyinstaller and cx_freeze can build an executable for multiple systems, including Windows, Mac OS, and Linux. Next, what you use depends on what version of Python you have. Cx_Freeze is the only one that I mentioned that supports python 3.X; the others only support Python 2.X. The advantage pyinstaller has, is it builds all the dependent files into one executable file that unpacks right before execution, whereas the others create a folder with lots dependent files along with the executable file. I use Cx_freeze, because of its python 3.x support and relatively easy building process.
As for converting it to bytecode, there are a couple python modules for this. One is py_compile. an example of this is:
import py_compile
py_compile.compile('filepathandname')
this will create a .pyc file, which python will put in a folder labeled __pycache__ in the same directory as the original file.
Hope this helped.
This question already has answers here:
How do I protect Python code from being read by users?
(29 answers)
Closed 9 years ago.
I'm rather new to Python, though hope to use it for both programming and scripting. I've written basic scripts, and did some digging for compiling. I'm currently using py2exe (With a different setup.py script someone else made) so that it becomes ONE simple .exe, without dependencies (python DLL, etc.)
You're probably wondering what my problem is. Well, I decided to check the security of the executable, and view it in Resource Hacker. I was able to view all the parts of the script I DIDN'T want people to be able to find out. (Ex: Password inputs).
Can anyone give me a simple, working method, for converting PYTHON code to a STANDALONE executable that CANNOT allow viewing of the original python script via something like Resource Hacker?
I am not thoroughly knowledgeable in the field.. I'm also not developing commercially (Yet), I just want to make things for myself, that I may also make for other people.. Though I might freelance for random people online doing things. Anyways point being, if I had a script where it prompted you for a password, and if you got it correct it continued, else, it cancelled and exited.... then once I make it a .exe, opening in Resource Hacker, and viewing the "Python Script", I scroll to the bottom, and bam! It shows the passwords. Now when I say I'm new, I mean, really REALLY new. Anyways, if you don't mind explaining, "Encryptions", "Hash's", etc... I would prefer to be enlightened towards these subjects.'
Your help is appreciated.
Here are the simple steps (with freeze)-
You will need to use a python installation which has all its modules installed as shared libraries
freeze.py usually resides under <python_install>/Tools/freeze/freeze.py
e.g: Python-2.4.2/linux/Tools/freeze/freeze.py
Now to integrate freeze a very simple program, which does not have dependency on any custom python module you will just need to call freeze in this fashion:
e.g:
cat hello.py
#!/usr/bin/env python
print "Testing"
To Freeze:
a. Python-2.4.2/linux/Tools/freeze/freeze.py hello.py
b. make
you will see there is a executable hello.
file hello
hello: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.4.0, dynamically linked (uses shared libs), not stripped
that's it:
now invoke hello will produce:
[0:22:47]% ./hello
Testing
This question already has answers here:
How do I protect Python code from being read by users?
(29 answers)
Closed 9 years ago.
I am writing code (Python and wxpython for GUI) which will run on Debian OS on Raspberry PI. I want to protect/hide the source code. Is there any way to do it? Probably py2exe, or converting it to a library or something else?
The compiled code (.pyc files) can be used if you wish for others to be able to execute but not to read or modify the source code (.py, .pyw).
Simply:
run your application
then copy all the relevant .pyc files into another folder and you should be able to
run it all from the new location
So long as all the appropriate modules are still able to be loaded, everything will work. This will require the version of python to be the same (can't run .pyc files from python 2.4 with python 2.7 and vice-versa)
The other thing to know is that strings will be preserved. You should open them up in a good text editor (I use vim) and inspect the content if you are worried about what others can see.
py2exe is of course another example, but you lose the ability to have cross-platform code at that point -- and if your application is for the Raspberry Pi -- that won't work.
Since you provided no other information about how you intend to run the code, it's not clear if the source will be a module or intended to be run directly. You should read this post to learn more.