I have a simple code with few "print" and "input".
I used cx_freeze to convert my python code on ".exe", but obviously when I launch my exe, it's run in background without any GUI (ofc I don't have GUI).
How to open it on cmd to interact with some "input" and see "print", like I do with my IDE (pycharm) cmd ?
Do you have to run it as an .exe, or is your goal to just run the script in cmd? To execute an .exe file in cmd, you should be able to just run by entering in the filename while you're in that file's folder directory:
Change to directory where program.exe is located by using cd:
cd C:\Users\johndoe\folder\
Run the program program.exe program.exe
However to save you compiling the python script to a .exe each time, if you already have python installed in your windows and you just want to run the python script, it should work with user inputs in the script by just entering python and the python filename (e.g. pyscript.py) into the windows cmd like below:
python pyscript.py
If your python script is a class / function that accepts arguments input1 and input 2:
python pyscript.py input1 input2
Related
I have written the following code within a .bat file, (to execute a python script):
conda activate
cd OneDrive - My path/Documenti/Python/Seguridad/Remote
py Seguridad_Python.py
If I execute that command in cmd.exe, the python file is executed.
But, if I run the .bat file with those commands, it opens the Command Prompt window, but does not execute the python script.
Can someone help?
I have also tried it with the next one, (everything in the same line), and it does the same.
C:\ProgramData\Anaconda3\python.exe C:\Users\------ MY PATH --------\\Documenti\Python\Seguridad\Remote\Seguridad_Python.python
Adding a call before was enough.
If I have the batch file and the python script at the same level, this was enough:
call conda activate
call py Seguridad_Python.py
Maybe you can try as below. Specify the batch file extension in the command prompt. Once you hit enter, it invokes your python script and run the program
C:\Users\------ MY PATH --------\\Documenti\Python\Seguridad\Remote\xyz.bat
I'm trying to make my .py script clickable!
What I Have
The CLI program I created using Python works. I open Command Prompt and enter python my_script.py and everything works as expected. I want to make it clickable so a user doesn't have to run any commands!
What I've Tried
I created a .bat file in vim that runs the python my_script.py command. When I click it, the desired behavior is not achieved. Command Prompt opens for about a millisecond then closes.
My Question
How can I create a better .bat file so that my CLI program works with the click of a button?
Python, for better or worse does not allow you to specify the path to the script, and will not search the path for the script.
The official word, and I hate to say this, is that you must associate the Python.exe executable you want to run python scripts with the extension .py and then you can double click your script.py or type \path\to\script.py into the CMD prompt, and it will run the script.
I hate this from a standpoint of not wanting to accidentally execute a script by clicking it, and from the perspective of wanting to have multiple versions of python installed and choosing which one would do the execution.
There is an (IMHO) annoying (Kludgy) work-around.
You can create a File type for each instance of Python, you might have, and then associate it with the .py
so you could create the FTypes you need and associate them in a script at the time of to launch the script from the batch file:
FTYPE Py23=C:\Path\To\Pthon-23\python.exe %*
FTYPE Py38=C:\Path\To\Pthon-38\python.exe %*
Then your script could do this:
#Echo off
ASSOC .py=Py38
"C:\Path\To\Script.py"
ASSOC .py=
Python supports being called in a mode where it can ignore the first line of the script with -x. This coupled with a wrapper batch file means you can actually put the entire Python script in a batch file.
This little script shows how this can be done. It's a self-contained
#echo off
rem = """ # This is run in the batch file to launch Python
python -x "%~f0" %*
goto endofPython
rem = """
# Your python code goes here ..
def main():
print("Hello from Python")
if __name__ == "__main__":
main()
rem = """ # This section is run after Python is done, here just
# just run pause to keep the console window open
:endofPython
pause
rem = """
I wrote a simple script in python 3.7 and I coverted it in .exe from cmd with the following line pyinstaller -w -F -i [myicon.ico] [myscript.py] . My script works if i launch it from command line (python myscript.py ), but it doesn't work when i click on "Myscript.exe" (I have a window with this message :"Failed to execute script Myscript").
I try to create the same script without the icon but result is the same.
Someone can help me?
You can refer to that post:
Windows- Pyinstaller Error "failed to execute script " When App Clicked
Actually the answer is that python doesn't compile the icon and so you need to manually transfert the icon in the folder containing the .exe.
B
I have some python script which is having more number of .py files, So I have generated .exe file for that, and I am trying to run using command prompt. I am unable to run .exe file in cmd prompt but when I tried to run on my friends laptop it is working fine.
I tried in cmd like below:
C:\pythonworkspace\Verification.exe -i C:\pythonworkspace\kpi_verification_2.0\config\Verification_config.xml
getting error as below:
Cannot open self C:\pythonworkspace\Verification.exe or archive
C:\pythonworkspace\Verification.pkg
Please help me where it is going wrong.
I did a application with Python 3 using Tkinter gui, on Raspbian with the RPi. I have three questions:
1) I've turned the python script into executable using:
chmod -x path/myfile.py
I've declared on the first line of script
#!/usr/bin/env python3
When I double click onthe script it pops up a windows asking if I want to execute normally or on the terminal. If i chose Execute, the script runs normally.
Is there any way to execute normally directly, without asking?
2) How do i create a shortcut on my Desktop to execute my python script?
I've created a file on the desktop and I edited the following:
[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=Name of the shortcut
Comment=Comment
Icon=path/pic.gif
Exec=python3 path/myfile.py
Terminal=false
The picture if the shortcut works, but the script it's not launched. It appears the hourglass and nothing happens.
3) How do I make a python script launch when at the startup of Raspbian?
I've already edited the
/etc/xdg/lxsession/LXDE/autostart
and also the
/etc/xdg/lxsession/LXDE-pi/autostart
with:
#python3 path/myfile.py
on the last line of the file. Rebooting the system, nothing happens.
Thank you for your help.