Getting cmd window with python exe from py2exe - python

I have generated python exe using py2exe but getting cmd window when I run my program.
I have changed my files from .py to .pyw and again generated .exe file but I am still getting cmd window.
How to generate exe that doesnot show cmd.
Note I am using Tkinter in my code.
Thanks

There is a simple way of doing it in Python:
Change your directory with cd into this one where your desired .py file is.
Pip install Pyinstaller.
when you succeeded typing following line: pyinstaller.exe --onefile -w NAME.py and hit enter. NAME is the name of your specific py file.
Now have a look in your chosen directory where are new files created. Look for the exe file which should be in the Dist folder.
Just copy paste it where ever you want and delete the rest.
Still
questions?
Just watch this video.

Related

Python executable - creating directories and file access

I have created a simple Python script test.py that is supposed to check if a directory exists, and, if it does not, to create it.
The code is as follows:
import os
def crete_dir(dir_name):
if not os.path.exists(dir_name):
os.makedirs(dir_name)
crete_dir("test_dir")
I am on Mac and use PyCharm to write and test my code.
The script works as expected so long as I run it in PyCharm, but as soon as I turn it into an executable (using pyinstaller: pyinstaller --onefile --clean --windowed test.py) and run it, it does not work (does not create the directory, even though it does not exist). The terminal window, that opens alongside the executable, does not return any file-access-permission or other error.
What should I please do to make the executable work normally?
Many thanks.

How to convert Python package to exe?

Our Programs is not always a Python source file !.
The Python may be associated with a file or an image,
that is located inside the source of the application to the file path inside the package.
For example, when setting the application icon in Tkinter Or Include image for app background
root.iconbitmap('favicon.ico')
PhotoImage(file = 'python_logo.gif')
In Pyinstaller only the source file expires in the exe format.
If the program contains a file or image path inside the source, then the program will not be executed , In that event,The source of the application and files are in a package (on a route).
Please provide a solution to this problem, So as possible convert a package containing source and file into exe
1-Install cx_Freeze, (open your command prompt and type pip install cx_Freeze
2-Install idna, (open your command prompt and type pip install idna.
3-Write a .py program named myprogram.py
4-Create a new python file named setup.py on the current directory of your script.
5-In the setup.py, code below and save it.
6-With shift pressed right click on the same directory, so you are able to open a command prompt window.
7-In the prompt, type python setup.py build
8-If your script is error free, then there will be no problem on creating application.
9-Check the newly created folder build. It has another folder in it. Within that folder you can find your application. Run it. Make yourself happy.
this works on python 3.5 and above!
Add heres the open source project to convert to exe
https://github.com/brentvollebregt/auto-py-to-exe
I have not used Pyinstaller myself but in cx_Freeze I know you can include none project files like images in the setup file. With Pyinstaller you want to look into the spec file. Here is a link to the docs where it talks about added files to project. https://pythonhosted.org/PyInstaller/spec-files.html#adding-files-to-the-bundle I hope thisis of some help.

Pyinstaller exectuable using data from other folders?

I've written a program that generates a random name and displays a random image along with it using Python and tkinter. However, I want the user to be able to add and remove pictures as well as edit the names of students. That being said, I don't want to package these inside the executable where it can't be changed by the user.
To use PyInstaller, I go into the command prompt and navigate to the working directory and type:
pyi-makespec --windowed --onefile --icon=Assets\\icon.ico random_student.py
Then, in the spec file I change datas to:
datas=[('Assets\\icon.ico', 'Assets')],
Then, I run
pyinstaller random_student.spec
The program runs just fine using PyCharm. And I've done this exact same method on a couple of other .py files an it works. However, they don't need to pull images/text into their programs. This will create an executable, but I can't run it. It gives me a Fatal Error "Failed to execute script random_student". I've tried placing the executable in the working directory and in the pictures folder, but neither work.
I'm currently using Windows 10 64-bit and Python 3.6.6
I'd appreciate any kind of help I can get with this!
SOLUTION: I removed the --windowed option so I could actually read the error. Then realized I didn't have Pillow installed so it was unable to be packaged. Thank you for the help.

Windows- Pyinstaller unless folder contains file appear error “failed to execute script"

I have a problem to over come the following error, when i am running my program packed using pyinstallet into standalone executable.
In the program i am using an icon file to set iconbitmap. The file is located in the same folder as the python script.
master = Tk()
master.wm_title("P&P Util ")
master.iconbitmap('logo.ico')
In addition i am adding and icon to the executable in the packing process.
pyinstaller -F -w -i "C:\temp\Util\logo.ico" main.py
Now after some testing i located that when i add file itself to the packed executable, the program is running without any issues. I understand that i need to change the way i am using the file but i don't know how.
Your advice will be very much appreciated.
It looks like it is crashing because you have not added the "logo.ico" file to the package, so when master.iconbitmap('logo.ico') looks for the icon, it is not there. With the command -i 'C:\temp\Util\logo.ico' you are telling pyinstaller to set that icon file as the windows icon, but this is not the same as including it in the package. To do that try including this in your pyinstaller command:
--add-data "C:\temp\Util\logo.ico;."
The "." after the semi-colon tells pyinstaller where to put the file. In this case it will put it into the same folder as the .exe file.

How to make a Python script run without it opening the console window?

I want to double click on my Python script (which uses a Tkinter GUI) and I just want it to open the Tkinter window, not the console window.
To do this, I changed the extension from .py to .pyw and this seems to work fine on Windows but when I double click my .pyw file on a Linux machine, it doesn't work. It simply froze and I had to restart my system.
Please suggest a platform-independent solution that would help me to run the Python script without opening the terminal/command prompt.
it's been a while since i tried on linux, but i believe it should be fairly simple, firstly you need to put a shebang at the top of the script so your shell knows which executable to use:
#!/usr/bin/python
or if you want a specific version you can expand this to:
#!/usr/bin/python3.2
using whichever version you want (only works for first 2 digits)
then you need to mark it as executable:
chmod 711 myfile.py
for more info on this see this page
then when you double click it, on the rpi (last i used linux) it asks if you want to execute it, or execute it in the terminal.
if you choose to execute it without the terminal, you should only see the tkinter GUI
You can use pyinstaller to create the executables for the different platforms you want.
For example,
The syntax of the pyinstaller command is:
pyinstaller [options] script [script ...] | specfile
In the most simple case, set the current directory to the location of your program myscript.py and execute:
pyinstaller myscript.py
PyInstaller analyzes myscript.py and:
Writes myscript.spec in the same folder as the script.
Creates a folder build in the same folder as the script if it does not exist.
Writes some log files and working files in the build folder.
Creates a folder dist in the same folder as the script if it does not exist.
Writes the myscript executable folder in the dist folder.
In the dist folder you find the bundled app you distribute to your users.
Normally you name one script on the command line. If you name more, all are analyzed and included in the output. However, the first script named supplies the name for the spec file and for the executable folder or file. Its code is the first to execute at run-time.
For certain uses you may edit the contents of myscript.spec (described under Using Spec Files). After you do this, you name the spec file to PyInstaller instead of the script:
pyinstaller myscript.spec
You may give a path to the script or spec file, for example
pyinstaller options... ~/myproject/source/myscript.py
or, on Windows,
pyinstaller "C:\Documents and Settings\project\myscript.spec"
pyinstaller

Categories