6

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 Nov 1, 2012 at 14:46
5
  • 1
    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... Commented Nov 1, 2012 at 14:47
  • 1
    Do you want a batch file that runs a Python script, or do you actually want to translate your script into a batch file? Commented Nov 1, 2012 at 14:49
  • 1
    @mgilson That's how I read it..., but I have a strange feeling this is heading towards py2exe... Commented Nov 1, 2012 at 14:50
  • @Mr.Squig Either will do. I just want the effect of the Python script to somehow take place. Commented Nov 1, 2012 at 14:51
  • Or then it's just @python path\to\script.py, save and exectute. Commented Nov 1, 2012 at 15:00

5 Answers 5

4

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.

answered Nov 1, 2012 at 15:00
Sign up to request clarification or add additional context in comments.

1 Comment

Is it possible to pass arguments?
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.

answered Nov 1, 2012 at 14:47

4 Comments

Ummm, how dare you bash cmd.exe - it's terrific cough ;)
@JJBeck Unless your Python script is remarkably simple - then yes, you'll have to install Python, or use py2exe or similar
I would do p2exe and a scheduled task to run the exe
@unwind: Excuse me, but I disagree. Batch files can manage Arrays, structures and linked-lists or Macros with parameters, for example...
1

(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 Dec 27, 2022 at 11:16

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

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 Mar 17, 2020 at 18:49

Comments

-3

Just create a batch file that contains this two lines:

yourfilename.py
pause

Then run the batch file by double-clicking it.

answered Dec 19, 2014 at 7:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy 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.