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

戏子无情 提交于 2021-02-17 06:49:34

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/60193121/how-do-i-run-a-python-program-just-by-typing-the-script-name-on-windows-10-cmd-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!