如何仅通过在Windows 10 cmd行上键入脚本名称来运行python程序? [英] How do i run a python program just by typing the script name on windows 10 cmd line?

查看:25
本文介绍了如何仅通过在Windows 10 cmd行上键入脚本名称来运行python程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 %*

这是程序的内容:

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

推荐答案

我认为您想要的是运行文件'mapIt.py',而无需调用以下关键字python:

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

>mapIt.py

代替

>python mapIt.py

在Linux或macOS中执行此操作的方法非常简单,您可以添加

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

#!/usr/bin/env python

在文件顶部,将文件从mapIt.py重命名为mapIt使脚本可执行:

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

chmod +x mapIt

但是对于Windows,没有直接的解决方案.

But for windows there is no straightforward solution.

一种方法是将文件转换为exe或

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

首先为所有".py"文件添加python.exe关联

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

> assoc .py=Python

然后

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

用python.exe文件的路径替换尖括号(<>)中的文本.

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

这篇关于如何仅通过在Windows 10 cmd行上键入脚本名称来运行python程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆