2

I'm trying to run a Python program from the command line so I created a batch file like so:

@py.exe C:\MyPythonScripts\program.py%*@pause

The script should print a simple message, but when I type "program" in the Windows Run program cmd appears for a second and closes itself. What am I doing wrong?.

This is the code:

#!python3def printPicnic(itemsDict,leftWidth,rightWidth):    print('PICNIC ITEMS'.center(leftWidth + rightWidth,'-'))    for k,v in itemsDict.items():        print(k.ljust(leftWidth,'.') + str(v).rjust(rightWidth))picnicItems = {'sandwiches':4, 'apples': 12, 'cups': 4, 'cookies':8000}printPicnic(picnicItems,12,5)printPicnic(picnicItems,20,6)

This is my PATH variable:

> C:\Program Files (x86)\Common> Files\Intel\Shared> Files\cpp\bin\Intel64;C:\ProgramData\Oracle\Java\javapath;C:\Program> Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files> (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS> Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program> Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program> Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files> (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files> (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files> (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files> (x86)\AMD\ATI.ACE\Core-Static;C:\Program Files> (x86)\Skype\Phone\;C:\Program> Files\Java\jdk1.8.0_121\bin;C:\MyPythonScripts;C:\Python34;C:\Program> Files (x86)\Common Files\Intel\Shared> Files\cpp\bin\Intel64;C:\ProgramData\Oracle\Java\javapath;C:\Program> Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files> (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS> Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program> Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program> Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files> (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files> (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files> (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files> (x86)\AMD\ATI.ACE\Core-Static;C:\Program Files> (x86)\Skype\Phone\;C:\Program> Files\Java\jdk1.8.0_121\bin;C:\Python34\Scripts
Arkady's user avatar
Arkady
15.2k8 gold badges45 silver badges47 bronze badges
askedMar 14, 2017 at 13:51
Xcecution's user avatar
3
  • @Farhan.K Code added...CommentedMar 14, 2017 at 14:07
  • So the batch file is calledprogram.bat?CommentedMar 14, 2017 at 19:49
  • Have a look at my answer in here:stackoverflow.com/questions/54847871/…CommentedJul 2, 2019 at 14:18

3 Answers3

4

Regarding your batch file, I would suggest perform these changes:

You were missing a space inbetween thepath and the%*, also enclose thepath in quotes to prevent it frombreaking when spaces are in the path

@echo offpy.exe "C:\MyPythonScripts\program.py" %1 %2 %3 %4 %5 %6 %7 %8 %9pause
answeredMar 14, 2017 at 14:00
Sam Denty's user avatar
Sign up to request clarification or add additional context in comments.

4 Comments

I edited the batch file like in your answer and now it works!. But I'm not sure why, I mean what does@echo off do and what's the difference between@py.exe and justpy.exe?
@Xcecution the@ symbol hides the command input, but can be replaced by using@echo off, which prevents you from having to use@ before every command
That is wrong,%* doesnot include the path of the batch file, so it is equivalent to%1 %2 %3 ...! Quoting paths is always a good practice and avoids trouble with white-spaces and other special characters, but the main problem here was the missing space between the path and%*...
If the .py file association is configured to execute via py.exe, then you can run "C:\MyPythonScripts\program.py" directly. The first line of the script can be a shebang to run a particular version of Python. This can be a fully-qualified path such as#!"C:\Program Files\Python36\python.exe" or a virtual Unix-style shebang like#!/usr/bin/python3.
2

The problem in your batch file is a missingSPACE:

@rem               THERE MUST BE A SPACE:@rem                                 |@rem                                 V@py.exe C:\MyPythonScripts\program.py %*@pause

In addition, I strongly recommend to put quotation marks around paths in order to avoid trouble with white-spaces and other spacial characters.

answeredMar 14, 2017 at 19:49
aschipfl's user avatar

Comments

0

You can do it using the following method :

@ECHO OFFsetlocalset PYTHONPATH=C:\Python35 (you version of python)C:\MyPythonScripts\program.py  %1<-- (you you have arg)  endlocal
Kristof Mols's user avatar
Kristof Mols
3,5752 gold badges41 silver badges51 bronze badges
answeredMar 14, 2017 at 14:01
LetzerWille's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.