I used this (python -m nuitka --follow-imports program.py) command to compile my script using Nuitka . It made an .exe file which worked quite well for me other than --onefile commands , as they were showing me import error. But here the problem is , a cmd console opens up of my .exe file after is start it, then my tkinter gui console shows up. I tried to use .pyw file to compile my script, but it didn't help me. I need help to shutdown or hide that cmd console window which pops up for my .exe file.
Related
I'm still a beginner with coding so maybe this question will be trivial for some of you. My apologies in advance.
For a project, i made a webcrawler using python and selenium, with an user interface made with Tkinter. I use a chromedriver to open chrome for the webcrawling part.
I converted my Tkinter file with pyinstaller into an executable file. When doing so, i typed the following flags: --onefile -w
I was told that the latter one prevents opening a command prompt when running the file, however, when i run the executable, my pc opens the command prompt for the chromedriver. How can i fix this?
-w will prevent the creation of a console for the main exe, not for the execution of the chromedriver.
You should have a look at ChromeDriver console application hide thread for removing the console created by chromedriver
I have a python code file named testcode.py and I have open a python.exe window open in order to run the code. I read online that I would run the code by putting
python testcode.py
into the exe window but it is saying that a syntax error and points to the name of my py file. Is there a reason why this is happening? Does the py file need to be saved in a certain place?
You're getting an error because you're trying to run shell code in the Python interpreter. Open a CMD or PowerShell window and run python testcode.py there.
Code in question:
from tkinter import *
root = Tk()
test_label = Label(root, text = "Hello")
test_label.pack()
root.mainloop()
I can only run this python code from the IDLE, if I run it any other way the window flashes on the screen and closes immediately. I've tried:
-adding an "input" line to keep it from closing
-running from the windows powershell
-compiling the code into an EXE with pyinstaller
and now I can't find any other suggestions. I tried making a simple 1-line program that just asks for input, and that works normally and stays open fine. The tkinter program works fine in IDLE but not any other circumstance.
What's happening here?
EDIT: If I run the program from the command line instead of windows 10 powershell, I get the following output:
Traceback (most recent call last):
File "C:\Users\Cam\Desktop\CSCI Notes\Programs\test.py", line 1, in
<module>
import tkinter
ImportError: No module named tkinter
However, the tkinter file is in the python library on my computer, and importing tkinter in python shell or IDLE works fine.
When I run python on command prompt, it shows python 2.7, so I changed tkinter to Tkinter and the program worked by importing the .py file
Using cmd:
Pyinstaller works(through canopy cmd prompt):
the first line is what I put into my IDLE's cmd prompt
Which populates the folder when executed:
And inside dist, I run test.exe which shows the window
NOTE: my system command prompt is using python 2.7 while the canopy(python environment) command prompt uses 3.5 with pyinstaller
Okay, I think I've solved this one! I read that windows can sometimes try to open .py files with python 2.7 instead, even if they're written in 3.6. I uninstalled python 2.7 from my computer, and now the file runs normally.
So for anybody having this problem, try to make sure your computer is opening python 3, not python 2.
I have also had this problem and as far as I can see no one has a proper solution.
The best I have found is putting -i before your filename. For example:
python -i yourfile.py
this will start the IDLE in the command line
This could also happen if you are using a modulo that isn't installed in your computer even if you have installed it using your IDLE. Try installing it with the command "pip install modulo" else try installing it manually.
I want to make my program executable.
I used TkInter to write the GUI, and I read somewhere that you have to save your file as .pyw to hide the console when the program is executed.
The problem is that after making it an executable with PyInstaller, the console shows up again, even though the file converted was .pyw.
How can I hide the console also in the .exe file?
Did you try --windowed command line flag ?
You can just save the file with .pyw extension and you can just use pyinstaller --onefile Filename.pyw
I think you renamed it. You save it as .pyw don't rename it.
The screenshot are below:
Output:
But it takes a while to open.
Thank you
-Levers
What are you using to make the executable?
If you use py2exe and you use:
setup(windows=[pythonscriptnamehere])
in the setup script instead of:
setup(console=[pythonscriptnamehere])
it will run the executable without launching a terminal in the background.
From The PyInstaller Documentation:
Using a Console Window
By default the bootloader creates a command-line console (a terminal
window in GNU/Linux and Mac OS, a command window in Windows). It gives
this window to the Python interpreter for its standard input and
output. Your script’s use of print and input() are directed here.
Error messages from Python and default logging output also appear in
the console window.
An option for Windows and Mac OS is to tell PyInstaller to not provide
a console window. The bootloader starts Python with no target for
standard output or input. Do this when your script has a graphical
interface for user input and can properly report its own diagnostics.
As noted in the CPython tutorial Appendix, for Windows a file
extention of .pyw suppresses the console window that normally appears.
Likewise, a console window will not be provided when using a
myscript.pyw script with PyInstaller.
Rather than saving/renaming/changing the extension to .pyw, you can just add --noconsole to the command line and use the standard .py file with it and this would hide that console window.
Example:
We have a file named GUI_name.py in our folder that uses Tkinter. Now we could easily create .exe file that doesn't show the console window by typing pyinstaller --onefile --noconsole GUI_name.py in cmd.
I am using Pyinstaller to create exe file from Python source code, and I keep the cmd window when the exe is running
python pyinstaller.py --onefile script.py
Whenever there is an error, the error shows on the cmd window but disappears very quickly, is there any way to keep the error message on the cmd window?
Create a batch file. Inside of it, put:
C:\path\to\your\file.exe
pause
and double click the batch file. Alternatively, just open a cmd window, navigate to the directory containing the exe and run yourExe.exe.