简体   繁体   English

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

[英]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.

相关问题 如何通过仅在终端上键入名称来使python程序运行? - How to make a python program run just by typing the name alone at the terminal? 如何获取 python 脚本以在 Windows (10) 中运行命令 shell (10),它在程序特定位置打开? - How do I get a python script to run a command shell in Windows (10) where it opens in a program specific location? 如何在 pypy3 上运行 python 脚本而不使用 windows 10 上的命令行? - How do I run a python script on pypy3 without using the command line on windows 10? 只需键入 python nameofthescript.py 即可在 Windows 10 cmd 上运行 python 脚本 - Running python scripts on windows 10 cmd just by typing python nameofthescript.py 如何使用cmd运行Spider编写的python程序 - How do I use the cmd to run a python program written in Spider 如何在 Windows 10 中的 cmd shell 上的笔记本电脑上本地运行 mapreduce 程序 - How to Run mapreduce Program locally on laptop on cmd shell in windows 10 如何在Windows10中从bash提示符运行python&#39;__main__&#39;程序文件? - How do I run python '__main__' program file from bash prompt in Windows10? 当我在刚刚安装在 Windows 10 上的 python 3.9 上从 cmd 运行 pip 时,它给出了一个奇怪的错误并停止 - when i run pip from cmd on python 3.9 i just installed on windows 10, it gives a wierd error and stops Windows 10-如何确保只要打开计算机,Python脚本就能运行? - Windows 10- how do I ensure Python script will run as long as computer is on? 如何在 cmd 上运行 Python 程序? - How can I run Python program on cmd?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM