简体   繁体   中英

How to make python 3 scripts execute from .bat file on Windows 10?

Very new to programming even newer to asking on stackoverflow so apologies if badly worded and/or formatted.

I'm using Python 3.7.2 on Windows 10. I'm trying to make .bat files so I can execute my python code without having to go into IDLE.

My python code is

#! python
import webbrowser
webbrowser.open('https://killsixbilliondemons.com/')

and the .bat file reads

@ C:\Program Files (x86)\Python37-32\python.exe C:\Users\User\Desktop\Codes\PROGS\mapIt.py

When I run it a command prompt window momentarily opens then closes but nothing happens in my browser. What's going wrong?

The line import webrowser contains a typo, it should be import webbrowser . Fixing that line should fix your issue.

Furthermore, if you want to see the output of a .bat file, try running it from a pre-made cmd prompt by hitting windows + r , entering cmd in the prompt that appears, going to the directory the .bat file is in through the cd <FULLPATH> command and calling it through <NAMEOFBATFILE>.bat .

  • You need to add a pause to your bat file to see any output of the code.
  • Also, Surround your parts of code with Quotation marks whenever there are spaces in command or parameters.

So your bat file content should read.

@ "C:\Program Files (x86)\Python37-32\python.exe" "C:\Users\User\Desktop\Codes\PROGS\mapIt.py"   
pause

Make sure to fix errors (if any) in the Python code.

First, if that's all your code, don't use python at all and just use start "" https://killsixbilliondemons.com instead in batch.

Now, the best way to run python from batch script is

@echo off
python foo.py
pause>nul

pause>nul will pause the program without Press any key to continue . . . Press any key to continue . . . , so the console won't just close at the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM