Python to .bat conversion

19,343

Solution 1

Unless your script is trivial it will not be possible to 'translate' it into a batch file. However two options exist:

  • Create a batch file to run the python script
  • Attempt to compile the script into an executable

The first option is trivial. Simply create a batch file as so:

@ECHO OFF
PATH_TO_PYTHON\python.exe PATH_TO_SCRIPT.py

If you are in a corporate environment you could put a python installation on a network and create a batch file to run the script from there. Otherwise you will need the user to install python. If python is on their path then the batch file can be simplified to:

@ECHO OFF
python PATH_TO_SCRIPT.py

Alternatively, there are options available that attempt to compile scripts into .exe files. I've never had any success with them, but py2exe seems the most common.

Solution 2

No, I don't think you can reasonably expect to do this.

Batch files are executed by the Windows command interpreter, which is way way more primitive.

Python is a full-blown programming language with a rich and powerful library of standard modules for all sorts of tasks. All the Windows command interpreter can do is act like a broken shell.

On the other hand, Python is available on Windows, so just tell the user to install it and run your program directly.

Share:
19,343
JJ Beck
Author by

JJ Beck

Updated on June 15, 2022

Comments

  • JJ Beck
    JJ Beck almost 2 years

    I would like a user on Windows to be able to run my Python program, so I want to convert it to a .bat file. Is there a way to convert it? I've tried searching, but didn't find anything.

    • mgilson
      mgilson over 11 years
      I would assume it is as easy as having the user install python (binaries are probably available), and then putting an instruction in a .bat file to have python run the program...
    • Mr. Squig
      Mr. Squig over 11 years
      Do you want a batch file that runs a Python script, or do you actually want to translate your script into a batch file?
    • Jon Clements
      Jon Clements over 11 years
      @mgilson That's how I read it..., but I have a strange feeling this is heading towards py2exe...
    • JJ Beck
      JJ Beck over 11 years
      @Mr.Squig Either will do. I just want the effect of the Python script to somehow take place.
    • tripleee
      tripleee over 11 years
      Or then it's just @python path\to\script.py, save and exectute.
  • Jon Clements
    Jon Clements over 11 years
    Ummm, how dare you bash cmd.exe - it's terrific cough ;)
  • Jon Clements
    Jon Clements over 11 years
    @JJBeck Unless your Python script is remarkably simple - then yes, you'll have to install Python, or use py2exe or similar
  • inspectorG4dget
    inspectorG4dget over 11 years
    I would do p2exe and a scheduled task to run the exe
  • Aacini
    Aacini over 11 years
    @unwind: Excuse me, but I disagree. Batch files can manage Arrays, structures and linked-lists or Macros with parameters, for example...
  • Arif
    Arif over 4 years
    Is it possible to pass arguments?