Easier way to create python executable standalone program in window - python-3.x

Creating a python executable in pycharm is harder than creating it in visual studio or creating a vb.net executable.
I thought I'd understand how to do this already but it looks like I have not.
I created a question regarding this in Dec of last year, here >> Create a simple python executable program and an icon to it - Hello World.
But I don't understand the answer or if it was answered completely. So I did my research again and found 2 helpful sites below that helped me create python executable without visual studio involved,
https://pythonhosted.org/PyInstaller/installation.html
https://pythonhosted.org/PyInstaller/usage.html#using-pyinstaller.
I followed the instructions exactly:
In command line, set path to C:\Python34\Scripts, then type pyinstaller "C:\Users\Desktop\PROGRAMs\TEST22.py".
This creates a TEST22.spec file in C:\Python34\Scripts and creates a folder TEST22 in C:\Python34\Scripts\dist and in there, are the TEST22.exe. I click on this executable to run the program, it runs completely fine. But when I copied the executable to the desktop, for example, it doesn't run. It seems like in the scripts folder there are files needed to run it and when it's a stand alone program there are no file to run it. Could someone let me know an easier way to create executable and please advise me on how I could fix it when the executable is copied to desktop or anywhere else and run it ?

Your answer lies in pyinstaller documentation. The dist folder itself is a standalone program directory. To get a single file exe with pyinstaller is not possible even thou pyinstaller has -F option for single exe since it dosnt contain data files.
I would suggest use py2exe and inno setup for creating installer

Related

Anaconda Prompt(Anaconda3) does not recognize files in a directory

I took to heart the message that support for anaconda2 was dropped. I installed anaconda3 and used 2to3 to convert my *.py codes. Everything seemed to work as expected. I have several 2 line bat codes that cd to a particular directory and then call python to execute a particular *.py code in that directory. By loading Anaconda Prompt(Anaconda3) and invoking the bat file I could run pythons in that directory as expected.
Then, a couple of days ago, I made such a run and got an error message saying the *.py file could not be found. I immediately looked in the directory and the python file was there as expected. I then did a dir command in Anaconda Prompt(Anaconda3) and found that almost no files in the directory were listed. This was happening on my windows 10 laptop.
On my desktop, everything continues to work as expected. Are there any suggestions as to what could cause such a problem and how to fix it.
Thanks,
Mack Elrod
Responding to the request for additional information, I have a bin directory that is in path. In bin\MackData.bat is
c:
cd %USERPROFILE%\Documents\Medev\MackData
copy sugarhist.png sugarhist.old.png
python MackData.py
When I open Anaconda Prompt(Anaconda3) and enter MackData I get
(base) C:\Users\Mack>MackData
(base) C:\Users\Mack>c:
(base) C:\Users\Mack>cd C:\Users\Mack\Documents\Medev\MackData
(base) C:\Users\Mack\Documents\Medev\MackData>copy sugarhist.png sugarhist.old.png
1 file(s) copied.
(base) C:\Users\Mack\Documents\Medev\MackData>python MackData.py
python: can't open file 'MackData.py': [Errno 2] No such file or directory
(base) C:\Users\Mack\Documents\Medev\MackData>
But MackData.py is a python file in the directory MackData.
This code worked on this laptop until a few days ago and continues to work on my desktop computer. I can, of course, publish MackData.py but that seems irrelevant. The point is that Anaconda can't find it.
Thanks,
Mack
I must submit a sincere apology to this community. I have determined my problem and Anaconda is not responsible in any way. I thank all of you for you concerns and comments.
For any who might be interested I will give a brief outline of what happened. First, I installed Anaconda3. Several days later my directory, MackData, was moved to a new location. I conjecture that happened by me inadvertently letting my hand drag on the touch pad. Then, not knowing the location had changed I updated my laptop from my desktop. My update program not finding MackData where expected recreated it and put only the most recently changed files from the desktop into the new MackData. New but in the correct location. Then of course running my script in the Anaconda3 prompt failed. Doing a dir in the Anaconda3 prompt showed only a few files were there. I then went to Windows File Explore and looked at MackData and saw all of the expected files. I used the quick access feature which unknown to me pointed to the old directory in its new location.
That is not intended to be an excuse but only as an explanation. Again, I do apologize to the community.
Mack Elrod
I'm pretty sure newer anaconda launchers users different environment paths, and not even windows ones.
I suggest either checking the environment path within the anaconda launcher and configurations or, a better solution in my opinion, run these scrips using the regular python engine, without using anaconda (simpler, more determinant and works within your standard environment)

Converting .py to .exe with pandas imported for all users not having pandas

I'm new to Python, but I have set up an python script for searching some specific Values in 2 different excel sheets printing out matches (in excel).
Problem is, that our work machines are heavily locked down and without admin privileges, we can't really install anything (we can download though). Is there any version of Python that is Windows 7 compatible that will run standalone without requiring any sort of installer?
I have tried pyInstaller, but the problem is that in my script we need PANDAS.
And there is no possibility to pip install pandas to our local machines. All is blocked. ("pip install pandas" is not possible. I did the code with Anaconda)
So my question is: how can I set up a file for my coworkers, who have no permission to download pandas?
Can I set up an exe file (all use windows 7/10) in my private computer where pandas is already installed and forward it to the workers?
It should be very easy for them to use--> double click for executing the python script
Thanks in advance for any advice.
You can also use pyinstaller which personally I find the easiest to use. It can bundle executables for both Linux and Windows, but it must be run on that architecture that you wish to have executable for, i.e. if you want to have Linux executable the pyinstaller command with your code must be run on Linux OS of some kind.
More here: https://www.pyinstaller.org/
This is old so you may already have found a solution but this might help others.
Python by default is an interpreted language. This means that compiling it into an .exe file is impossible.
However, using some modules it is indeed possible to convert a .py script into a windows executable.
You can try py2exe.
py2exe is a Python Distutils extension which converts Python scripts
into executable Windows programs, able to run without requiring a
Python installation.
They have a tutorial here.

How to convert file .py to .exe, having Python from Anaconda Navigator? (in which command prompt should I write installation codes?)

I created a Python script (format .py) that works.
I would like to convert this file to .exe, to use it in a computer without having Python installed.
How can I do?
I have Python from Anaconda3.
What can I do?
Thank you!
I followed some instruction found here on Stackoverflow.
.I modify the Path in the 'Environment variables' in the windows settings, edited to the Anaconda folder.
.I managed to install pip in conda prompt (I guess).
Still, nothing is working. I don't know how to proceed and in general how to do things properly.
I personaly use pyinstaller, its available from pip.
But it will not really compile, it will just bundle.
The difference is compiling means translating to real machine code while bundling is creating a big exe file with all your libs and your python interpreter.
Even if pyinstaller create bigger file and is slower than cython (at execution), I prefer it because it work all the time without work (except lunching it).

Allow my friends to run my python script? [duplicate]

This question already has answers here:
Create a single executable from a Python project [closed]
(3 answers)
Closed 1 year ago.
I'm building a Python application and don't want to force my clients to install Python and modules.
So, is there a way to compile a Python script to be a standalone executable?
You can use PyInstaller to package Python programs as standalone executables. It works on Windows, Linux, and Mac.
PyInstaller Quickstart
Install PyInstaller from PyPI:
pip install pyinstaller
Go to your program’s directory and run:
pyinstaller yourprogram.py
This will generate the bundle in a subdirectory called dist.
pyinstaller -F yourprogram.py
Adding -F (or --onefile) parameter will pack everything into single "exe".
pyinstaller -F --paths=<your_path>\Lib\site-packages yourprogram.py
running into "ImportError" you might consider side-packages.
pip install pynput==1.6.8
still runing in Import-Erorr - try to downgrade pyinstaller - see Getting error when using pynput with pyinstaller
For a more detailed walkthrough, see the manual.
You can use py2exe as already answered and use Cython to convert your key .py files in .pyc, C compiled files, like .dll in Windows and .so on Linux.
It is much harder to revert than common .pyo and .pyc files (and also gain in performance!).
You might wish to investigate Nuitka. It takes Python source code and converts it in to C++ API calls. Then it compiles into an executable binary (ELF on Linux). It has been around for a few years now and supports a wide range of Python versions.
You will probably also get a performance improvement if you use it. It is recommended.
Yes, it is possible to compile Python scripts into standalone executables.
PyInstaller can be used to convert Python programs into stand-alone executables, under Windows, Linux, Mac OS X, FreeBSD, Solaris, and AIX. It is one of the recommended converters.
py2exe converts Python scripts into only executable on the Windows platform.
Cython is a static compiler for both the Python programming language and the extended Cython programming language.
I would like to compile some useful information about creating standalone files on Windows using Python 2.7.
I have used py2exe and it works, but I had some problems.
It has shown some problems for creating single files in Windows 64 bits: Using bundle_files = 1 with py2exe is not working;
It is necessary to create a setup.py file for it to work. http://www.py2exe.org/index.cgi/Tutorial#Step2;
I have had problems with dependencies that you have to solve by importing packages in the setup file;
I was not able to make it work together with PyQt.
This last reason made me try PyInstaller http://www.pyinstaller.org/.
In my opinion, it is much better because:
It is easier to use.
I suggest creating a .bat file with the following lines for example (pyinstaller.exe must be in in the Windows path):
pyinstaller.exe --onefile MyCode.py
You can create a single file, among other options (https://pyinstaller.readthedocs.io/en/stable/usage.html#options).
I had only one problem using PyInstaller and multiprocessing package that was solved by using this recipe: https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Multiprocessing.
So, I think that, at least for python 2.7, a better and simpler option is PyInstaller.
And a third option is cx_Freeze, which is cross-platform.
pyinstaller yourfile.py -F --onefile
This creates a standalone EXE file on Windows.
Important note 1: The EXE file will be generated in a folder named 'dist'.
Important note 2: Do not forget --onefile flag
You can install PyInstaller using pip install PyInstaller
NOTE: In rare cases there are hidden dependencies...so if you run the EXE file and get missing library error (win32timezone in the example below) then use something like this:
pyinstaller --hiddenimport win32timezone -F "Backup Program.py"
I like PyInstaller - especially the "windowed" variant:
pyinstaller --onefile --windowed myscript.py
It will create one single *.exe file in a distination/folder.
You may like py2exe. You'll also find information in there for doing it on Linux.
Use py2exe.... use the below set up files:
from distutils.core import setup
import py2exe
from distutils.filelist import findall
import matplotlib
setup(
console = ['PlotMemInfo.py'],
options = {
'py2exe': {
'packages': ['matplotlib'],
'dll_excludes': ['libgdk-win32-2.0-0.dll',
'libgobject-2.0-0.dll',
'libgdk_pixbuf-2.0-0.dll']
}
},
data_files = matplotlib.get_py2exe_datafiles()
)
I also recommend PyInstaller for better backward compatibility such as Python 2.3 - 2.7.
For py2exe, you have to have Python 2.6.
For Python 3.2 scripts, the only choice is cx_Freeze. Build it from sources; otherwise it won't work.
For Python 2.x I suggest PyInstaller as it can package a Python program in a single executable, unlike cx_Freeze which outputs also libraries.
Since it seems to be missing from the current list of answers, I think it is worth mentioning that the standard library includes a zipapp module that can be used for this purpose. Its basic usage is just compressing a bunch of Python files into a zip file with extension .pyz than can be directly executed as python myapp.pyz, but you can also make a self-contained package from a requirements.txt file:
$ python -m pip install -r requirements.txt --target myapp
$ python -m zipapp -p "interpreter" myapp
Where interpreter can be something like /usr/bin/env python (see Specifying the Interpreter).
Usually, the generated .pyz / .pyzw file should be executable, in Unix because it gets marked as such and in Windows because Python installation usually registers those extensions. However, it is relatively easy to make a Windows executable that should work as long as the user has python3.dll in the path.
If you don't want to require the end user to install Python, you can distribute the application along with the embeddable Python package.
py2exe will make the EXE file you want, but you need to have the same version of MSVCR90.dll on the machine you're going to use your new EXE file.
See Tutorial for more information.
You can find the list of distribution utilities listed at Distribution Utilities.
I use bbfreeze and it has been working very well (yet to have Python 3 support though).
Not exactly a packaging of the Python code, but there is now also Grumpy from Google, which transpiles the code to Go.
It doesn't support the Python C API, so it may not work for all projects.
Using PyInstaller, I found a better method using shortcut to the .exe rather than making --onefile. Anyway, there are probably some data files around and if you're running a site-based app then your program depends on HTML, JavaScript, and CSS files too. There isn't any point in moving all these files somewhere... Instead what if we move the working path up?
Make a shortcut to the EXE file, move it at top and set the target and start-in paths as specified, to have relative paths going to dist\folder:
Target: %windir%\system32\cmd.exe /c start dist\web_wrapper\web_wrapper.exe
Start in: "%windir%\system32\cmd.exe /c start dist\web_wrapper\"
We can rename the shortcut to anything, so renaming to "GTFS-Manager".
Now when I double-click the shortcut, it's as if I python-ran the file! I found this approach better than the --onefile one as:
In onefile's case, there's a problem with a .dll missing for the Windows 7 OS which needs some prior installation, etc. Yawn. With the usual build with multiple files, no such issues.
All the files that my Python script uses (it's deploying a tornado web server and needs a whole freakin' website worth of files to be there!) don't need to be moved anywhere: I simply create the shortcut at top.
I can actually use this exact same folder on Ubuntu (run python3 myfile.py) and Windows (double-click the shortcut).
I don't need to bother with the overly complicated hacking of .spec file to include data files, etc.
Oh, remember to delete off the build folder after building. It will save on size.
Use Cython to convert to C, compile, and link with GCC.
Another could be, make the core functions in C (the ones you want to make hard to reverse), compile them and use Boost.Python to import the compiled code (plus you get a much faster code execution). Then use any tool mentioned to distribute.
I'm told that PyRun is also an option. It currently supports Linux, FreeBSD and Mac OS X.

cx-freeze can't create .exe file

I have wrote a simple Python script for searching files & finding the duplicates as I am currently learning Python. Nothing complicated at all. This is also my first try of using cx-freeze to build an Windows executable. I'm using Python 3.3.
I'm using the included cxfreeze script to create .exe file. So, I've typed cxfreeze search.py --target-dir dist . Everything goes OK during buildup, and I get the 'dist' folder with files in my home directory (using Lubuntu Linux). Problem is, the Lubuntu recognises the file with the name 'search' as executable, but it hasn't got an .exe extension. On the Windows platform, it is showed just as an file without an extension. So when I try to run it on Windows XP, it just won't work. Adding an .exe extension also do not help.
Do someone have a clue what am I doing wrong?
Thanks.

Resources