3

I seem to be having a problem with executing a Python script from a Win+R terminal.

I completed the following steps:

  1. Used the shebang line before typing my script for all .py files. An example of what I did below for a script called Primefactorization.py.

    #! python3
    
  2. I created a batch file in the same folder with the same name and input the following code:

    @python.exe C:\Python Scripts\Primefactorization.py %*
    
  3. I added the path (C:\Python Scripts) to the PATH variable in the environment variables window.

  4. When I try to invoke the script using the Run command in Windows 7, the shell opens and immediately disappears.

  5. Based on a past answer to a similar problem on Stack Overflow, I also added the following code prompting the user for input before exiting. But that doesn't seem to work.

    x = input('press enter to close')
    

Could you please let me know where the problem might be?

Frédéric Hamidi
  • 240,249
  • 39
  • 455
  • 462
Kannaj
  • 5,036
  • 8
  • 29
  • 63
  • 2
    If you open a command prompt, and then type `python` there, do you get a python prompt? If yes, if you type `python yourscript.py` at a command prompt, is your script executed? – WoJ Jun 03 '15 at 13:53
  • Is your script running without errors? Errors from a Python script will immediately close command line windows if not explicitly caught. – SuperBiasedMan Jun 03 '15 at 13:55
  • 1
    If i type python in my command prompt. It does open the python console. however , 'python pythonscript.py' doesnt work. i get the following error 'Cant open file myscript.py: [Errno2] no such file or directory.' Even though the script is in the same directory as the python exe file. – Kannaj Jun 03 '15 at 14:47
  • 1
    @SuperBiasedMan - yes the script runs fine when i run it from IDLE – Kannaj Jun 03 '15 at 14:51
  • 1
    Perhaps your environment variables are off in the command prompt. In the command line, open Python and then check what the directory is by typing in `import os` and then `print os.getcwd()` to print the directory the python executable is in and see if it matches the path you expect. – SuperBiasedMan Jun 03 '15 at 14:55
  • os.getcwd() points to C:/Python34 where all the scripts are stored. the script seems to run after chaging my directory path to C:/Python34. C:\Python34 python myscript.py seems to executing. But i'm still having trouble running the script directly from win+r – Kannaj Jun 03 '15 at 16:12
  • Run the batch file from the command line instead of Win+R. Then the console won't disappear immediately and you'll be able to see what the error is. – Zenadix Jun 03 '15 at 16:39
  • i tried running the bat file directly from the command prompt. I get the following error **python.exe : can't open file 'C:\Python': [Errno2]** it should be C:\Python34 right? how do i fix this? – Kannaj Jun 03 '15 at 17:00

1 Answers1

1

Your path has a space in it. Enclose it in double quotes. For example:

python.exe "C:\Python Scripts\Primefactorization.py"
Zenadix
  • 11,375
  • 3
  • 21
  • 39
  • this is in the batch file? should i skip the %* – Kannaj Jun 03 '15 at 20:11
  • Yes, in the batch file. You can skip the `%*`, as I assume your program doesnt require any arguments. Obviously, also make sure that `C:\Python Scripts\Primefactorization.py` exists. – Zenadix Jun 03 '15 at 20:15
  • Thanks .. this finally seems to be a workaround :). Any idea why this works? – Kannaj Jun 04 '15 at 09:36
  • Your path had a space in it. Without quotes, you were telling `python.exe` to run run the file `C:\Python` with the argument `Scripts\Primefactorization.py` – Zenadix Jun 04 '15 at 14:39