简体   繁体   中英

How do i run a python program just by typing the script name on windows 10 cmd line?

How do i run a python program just by typing the script name on windows 10 cmd line? Also without having to change directory. I already added my scripts folder and python folder to the path. tried also tu run assoc py.=PythonScript ftype PythonScript=python.exe %1 %*

Here's the program's content:

#! python3
# mapIt.py  - Launches a map in the browser using an address from the command line or clipboard
import webbrowser, sys, pyperclip
if len(sys.argv) > 1:
    address = ' '.join(sys.argv[1:])
else:
    address = pyperclip.paste()

webbrowser.open('https://www.google.com/maps/place/' + address)

I added a screenshot with all the commands i tried so far. 在此处输入图片说明

I think what you want is to run the file 'mapIt.py' without invoking the keyword python that is:

>mapIt.py

instead of

>python mapIt.py

the way to do that in Linux or macOS is simple enough, you can add

#!/usr/bin/env python

to the top of the file, rename your file from mapIt.py to mapIt make the script executable:

chmod +x mapIt

But for windows there is no straightforward solution.

One way you can do it is convert the file into an exe or

first add a python.exe association for all '.py' files

> assoc .py=Python

and then

> ftype Python="<path of your python.exe>" "%1" %*

replace the text in angular brackets (<>) with the path of your python.exe file.

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