Python to .bat conversion

Question:

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.

Asked By: JJ Beck

||

Answers:

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.

Answered By: unwind

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_PYTHONpython.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.

Answered By: Mr. Squig

Just create a batch file that contains this two lines:

yourfilename.py
pause

Then run the batch file by double-clicking it.

Answered By: sven tan

you can do it in 2 ways :

  1. create a normal text file with an extension .bat and write
@ECHO OFF

"python.exe location" "your file.py location"
  1. create a normal text file with an extension .bat and write
"python.exe location" "your file.py location"
pause
Answered By: Shashank Goud

(sorry for my English Im from armenia)
After 10 years I finaly did that without py2exe and some other things

I used openai playground to do that and thats worked:

I just writed "convert python code into .bat file: [my code]"

Answered By: C3mTri
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.