[英]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?如何仅通过在 Windows 10 cmd 行上键入脚本名称来运行 python 程序? Also without having to change directory.也无需更改目录。 I already added my scripts folder and python folder to the path.我已经将我的脚本文件夹和 python 文件夹添加到路径中。 tried also tu run assoc py.=PythonScript ftype PythonScript=python.exe %1 %*也试过你运行 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:我认为您想要的是在不调用关键字 python 的情况下运行文件“mapIt.py”:
>mapIt.py
instead of代替
>python mapIt.py
the way to do that in Linux or macOS is simple enough, you can add在 Linux 或 macOS 中执行此操作的方法很简单,您可以添加
#!/usr/bin/env python
to the top of the file, rename your file from mapIt.py to mapIt make the script executable:到文件的顶部,将文件从 mapIt.py 重命名为 mapIt 使脚本可执行:
chmod +x mapIt
But for windows there is no straightforward solution.但是对于 Windows,没有直接的解决方案。
One way you can do it is convert the file into an exe or一种方法是将文件转换为 exe 或
first add a python.exe association for all '.py' files首先为所有“.py”文件添加一个 python.exe 关联
> 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.将尖括号 (<>) 中的文本替换为 python.exe 文件的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.