Fail to run the exectuable via cmd or Python - python

My tutor sent me an executable to run the simulation. The usage is to put the executable, settings files and initial files to the same folder and double click the executable, and the executable will generate the result text file.
Recently I try to integrate this function into my python script. The goal is when I run the python script, the script could generate the basic input file in the folder and then run the executable to generate the result. A part of the script looks like this:
path_exe = r"D:\direction\xxx.exe"
subprocess.run(path_exe,shell=True)
However, it returns:
ERROR LOADING Keyfile_0 FILE
I'm sure that the "keyfile" is in the same direction, because it works well when I double click the executable.
Then I try to run it via cmd. I drag the executable into cmd window and press enter, the same error happens.
I don't know if it is blamed on the executable, or is the code error.

Related

I used pyinstaller to turn my .py file to a executable file, but it doesn't work

EDIT: I'm using the "random" library and the "os" library, could they be interfering with the file's execution?
Good morning, I'm creating a hangman game that runs all in the terminal, doesn't open any graphical window, I used pyinstaller to turn my .py file into an executable. When I run it through the command ./My_file it works normally, but if I try to run it by double clicking it doesn't open anything! Does anyone know how to solve?

I want to run a python script or an executable everytime i open command prompt either through Run or search or using the toolbar in the Explorer

Can I run a script or a .exe file every time the command prompt is executed from various methods?
like maybe through Run or the toolbar in Explorer or anywhere.
I want to run a script and show something in the command prompt as an output.
If you are asking to run Python script/code on cmd then run this code:
import py_compile
py_compile.compile(filename.py,output_name.pyc)
This will create a .pyc file which you can then run in cmd using command:
python output_name.pyc
note: filename and output_name are both strings that should include fileextensions and you can write any name here.

How to run python in exe window

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.

How to associate .py files to open CMD with `py` then the filename?

How to associate .py files to open CMD with py then the filename? Maybe in a .bat file?
sorry about my poor English, and if I insist on subjects you already master, it's my first constructive answer here ;p
I'm not sure about what you want to achieve but from your question and its tags I assume tha you want to :
run ".py" file containing a python script from the file explorer by double clicking it
have a cmd.exe window open after this action with your python script interpreted
have a way to review this scipt output without relying on superman eyes able to gasp 65536 characters per millisecond
So basically, if you have a script printing "Hello World !", you want to click on it, and see in a cmd.exe window the text "Hello World !" displayed to validate that your script is working properly ? To make it short you are RIGHT, a .bat file will be enough to do the trick, even if there is a whole bunch of alternatives including executable generation to embed a full python interpreter (see http://www.py2exe.org/), or simply adding a wait loop at the end of your script, but having a batch script associated is probably the lightest and easiest solution in your case.
As you figured out, associating .py files with the python interpreter will run your scripts but the console window will dissapear immediatly on completion without letting you the time to consider the output. You just need to associate .py files (right click -> open with, if you want to do it programatically it's possible to set this in the windows registry) with a .bat script that will do the job, that is, run the script and wait until you are ready to "leave".
This batch script will take the python script you clicked on as an argument, run it with your python interpreter and pause it's execution, waiting for your input before leaving. Since the default windows file association will execute your target program and pass it the file executed (should it be a click or a "start XXX" command) it's pretty straightforward, the bricks to do this in batch are :
program_name argument : to directly call an external command, so "python my_script.py" will run the python.exe program (no need to add the ".exe" or "'.com" part since it's an obvious case for windows) with my_script.py as argument, provided that your python executable directory is in your PATH environment variable, otherwise you will have to provide the full path, ie: "C:\Python27\python.exe my_script.py" .
%X : to reference command line arguments sent to your script (%1 for the first one, then %2 etc.,)
pause : a command that will display the message "Press any key to continue ...", and obviously wait for any key before leaving your script
evantually, #echo off : to avoid printing each batch command before its execution
So, assuming that your python interpreter is installed in C:\Python27 (please replace with whatever version / location for your python.exe, or just "python" if it's in your PATH) your batch script could look like something like this :
#echo off
C:\Python27\python.exe %1
pause
Save it somewhere, associate it with .py files, and you are done. HTH
You can do it in two separate ways:
First, you can rename your .py file to .pyw and just open it and the script would be executed immediately (with pythonw.exe) but this is not showing you a console output.
Or you can simple associate your .py files with standard python.exe which will show the console output.

Run Python script without opening Pythonwin

I have a python script which I can run from pythonwin on which I give the arguments.
Is it possible to automate this so that when I just click on the *.py file, I don't see the script and it asks for the path in a dos window?
You're running on Windows, so you need an association between .py files and some binary to run them. Have a look at this post.
When you run "assoc .py", do you get Python.File? When you run "ftype Python.File", what do you get? If "ftype Python.File" points at some python.exe, your python script should run without any prompting.
Rename it to *.pyw to hide the console on execution in Windows.
You can also wrap it in a batch file, containing:
c:\path to python.exe c:\path to file.py
You can then also easily set an icon, run in window/run hidden etc on the batch file.
how does your script ask for or get its parameters? If it expects them from the call to the script (i.e. in sys.argv) and Pythonwin just notices that and prompts you for them (I think Pyscripter does something similar) you can either run it from a CMD window (commandline) where you can give the arguments as in
python myscript.py argument-1 argument-2
or modify your script to ask for the arguments itself instead (using a gui like Tkinter if you don't want to run from commandline).

Categories

Resources