I have written a Python script that I need to share with folks who may or may not have Python installed on their machine. As a dirty hack I figured I could copy my local Python3.6 install into the same folder as the script I made, and then create a .bat file that runs python from the copied Python source ie.
Python36\python.exe script.py %*
In this way I could just send them the folder, and all they have to do is double click the .bat file. Now this does work, but it takes about 2 - 5 mins for script.py to begin executing. How could I configure the copied python source so that it runs like it "should"?
In terms of speed that is little you can do. You could convert your Python script into a compiled extension, this increases speed of a Python script greatly. Cython can do this and once compile you then do as you have done already.
Honestly you will notice little difference if you do this, and that is about the best you will do with that method. A better method is to turn it into an executable directly.
What you are doing currantly is:
The batch command starts and executes (this is slow by itself). This starts the Python interpreter.
The Python interpreter loads the file and then starts.
You should use a tool such as Cx_Freeze or Pyinstaller to convert your script into an executable, then it could be run just like any other appliocation. You could also use Cython to achieve this.
You can use installers as well.
Are you using any libraries? A quick solution would be converting the python script to executable using py2exe. More details are also in this post.
from distutils.core import setup
import py2exe
setup(console=['sample.py'])
And then run the command
C:\Tutorial>python setup.py py2exe
Related
I have a python script that calls other python scripts using subprocess.Popen. These other python scripts are located in different directories.
My end goal is to make the program executable using pyinstaller, in an attempt to make the code more secure.
One solution would be to import the other scripts, instead of using subprocess. In this way they would be embedded in the executable file and would thus be protected.
But importing them is not an option.
For this reason, I guess I also have to make the other scripts executable using Pyinstaller and use subprocess to call each executable.
I've read the Pyinstaller's Multipackage Bundles
but I'm not sure if this is what I need.
My question is, how can I call the other python scripts using subprocess (or call their executable) and at the same time protect their source code?
I’ve recently tried making an .exe from my .py file.
(This is my first time making a .exe)
It’s a long script and includes this cool function.
If I compile this to a .exe
It works just fine on the PC I code on, but not on another PC. Which makes me believe there is something missing during the pip installer ?
To compile this script I need to have ctypes Installed,
and in the cmd say: pyinstaller filename.py
Am I missing something ?
I don't have any error to show unfortunately, it just stays blank after it tries to run wait_until_idle.
The most trusted/automated way that I found to make an exe out of python code is this package:
https://pypi.org/project/auto-py-to-exe/
I tested it myself on multiple situations with whole projects not just single/small scripts.
The main disadvantage of this is the size of the exe file, it's really big and specially if you are using external libraries like pandas, numpy could easily make your exe pass 100 Mega bytes.
It's a simple graphical UI that you could use to generate exe, There is no external dependencies you need to install separately.
I have a small program written with Python3 that I'd like to install on friends computers. The main issue is that not every computer has a Python3 installed on it (mostly Python2-). Do I have to install it on each computer I want my program to run?
I thought it would be possible to install Python3 inside a separate directory, as shown below.
Then, I would be able to use a shebang to run the right version of Python installed inside myProgram folder.
#!C:\myProgram\python3 python
# Test
a = input('Entrer un nom:')
print(a)
When I double-click on myProgram.py file, a window opens and closes immediatly...
Is this a bad idea or not? Is there a way to achieve that differetnly if so?
You could create an executable from the python script rather than installing all of python 3 on their machine. There's a few ways you can do it, see this answer.
I'd have a look at pyinstaller.
Add Python 3 to your friends machine to execute your program.
What'is the utility to run such a small program on their computers?
The way you propose won't work.
Windows does not know about shebangs.
Even if it did, this one would only work if a user installed the program to C:\myProgram (which is not a really appropriate place, and the user might not have the rights to install it there).
There are ways to make a python program portable w/o having the need to have a Python runtime installed. Alas, I currently don't know their name.
Or, if that is feasible, install Python 3 on their machines, but I don't know if that might break any programs wanting to use Python 2.
There're different tools which can pack you program to single .exe file or make an installer. Here's my solution from 2016 year - Python - create an EXE that runs code as written, not as it was when compiled
Now it will be little easier :)
I want to put the Python interpreter and all the source files of a fairly large Python application with multiple .py files into an executable Linux file.
Thus when running the application, the Python interpreter will run the Python source code that is embedded into the executable.
Using Python 3.
Is such a thing possible?
Edit: in addition to the selected answer another option is to use Cython.
Sounds like you're looking for a packaging module, like py2exe or cx_Freeze (I prefer the latter). They bundle the interpreter and your files together so that a machine without an installation of Python can run your program.
I've been searching through SO for a while now trying to come up with an answer to this but due to my inexperience with programming I don't understand much of the documentation, nor am I confident enough to experiment too much.
Would anyone be able to describe in slightly simpler terms how I would use programs like Py2exe, PyInstaller, cx_freeze etc.? I just want a way for others (mainly friends) to be able to run my (simple, text only) program without having to download python themselves. If there is an easier way to do this I'd appreciate knowing that too.
Running Vista 32bit, python 2.7
There are two distinct ways of freezing python scripts to create executables:
Packing the interpreter and *.pyc files into one exe file-container. Such an approach is used by tools like PyInstaller, Py2exe, cx_freeze.
Creating native code from Python source, usually using a middle step of converting Python-source to C or C++ code. This is done by such tools as Shed-skin and Nuitka. The problem of this aproach is that such tools do not always support all the functionality of Python (e.g. they can have some typing limitations and so on)
The point where you have to start is reading the documentation. Such tools are not just push-and-run style tools, they usually have some configuration that must be implemented (that's the problem of possibly all build systems, and as the project grows, the configuration and number of hooks also grows).
You can start with Py2exe tutorial and 'hello-world' to get acquainted with that how compilation is done. As far as I know it's a simplest way to get your goal.
And the last thing, you can't create cross-platform native executables as their file formats are strongly operating system and hardware dependent.
Download py2exe
Download this msvcp90.dll
Copy your FileCode.py AND msvcp90.dll to C:\Python27\
In C:\Python27\ create new text file, then enter this code inside it:
from distutils.core import setup
import py2exe
setup(console=['Avril.py'])
Replace Avril.py with YourFileName.py
Save the file as setup.txt
Open CMD and type this:
cd C:\Python27\
python setup.txt py2exe
Now go to C:\Python27\dist\ and there's your .exe program.
Source: Manvir Singh
Python scripts can be made directly executable, like shell scripts, by putting the python environment path in the top of the script file.
#!/usr/bin/env python3.5
The Python installer automatically associates .py files with python.exe so that a double-click on a Python file will run it as a script. The extension can also be .pyw, in that case, the console window that normally appears is suppressed.
Detailed description also for linux is here.
Install pyinstaller, a program that converts .py to .exe for python 2.7 to where python is located:
cd C:\python27\scripts
pip install pyinstaller
then move whatever python file you want to compile to C:\python27\scripts, compile from there by using:
pyinstaller --onefile yourfile.py
the --onefile is optional but it packages the whole thing(in this example yourfile.py) into one .exe. Once everything is done there will be 2 new folders along with a .spec file. From C:\python27\scripts open the folder dist. Your .exe will be located there in one file which you can double tap to execute and distribute to anyone who doesn't have python. Hope it helps.