猿问

如何仅通过在 Windows 10 cmd 行上键入脚本名称来运行 python 程序?

如何仅通过在 Windows 10 cmd 行上键入脚本名称来运行 python 程序?也无需更改目录。我已经将我的脚本文件夹和 python 文件夹添加到路径中。也试过你运行 assoc py.=PythonScript ftype PythonScript=python.exe %1 %*


这是程序的内容:


#! 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)

我添加了一个屏幕截图,其中包含到目前为止我尝试过的所有命令。

哈士奇WWW
浏览 75回答 1
1回答

慕神8447489

我认为您想要的是运行文件'mapIt.py'而不调用关键字python,即:>mapIt.py代替>python&nbsp;mapIt.py在 Linux 或 macOS 中执行此操作的方法很简单,您可以添加#!/usr/bin/env&nbsp;python在文件顶部,将文件从 mapIt.py 重命名为 mapIt 以使脚本可执行:chmod&nbsp;+x&nbsp;mapIt但是对于 Windows,没有直接的解决方案。一种方法是将文件转换为 exe 或首先为所有 '.py' 文件添加一个 python.exe 关联>&nbsp;assoc&nbsp;.py=Python接着>&nbsp;ftype&nbsp;Python="<path&nbsp;of&nbsp;your&nbsp;python.exe>"&nbsp;"%1"&nbsp;%*将尖括号 (<>) 中的文本替换为 python.exe 文件的路径。
随时随地看视频慕课网APP

相关分类

Python
我要回答