使用 Windows 从命令行调用 Python 脚本(在 PATH 中的目录中) [英] Call a Python script (in a directory in the PATH) from command line with Windows

查看:44
本文介绍了使用 Windows 从命令行调用 Python 脚本(在 PATH 中的目录中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 Linux,如果您在 PATH 中的文件夹中创建一个具有 +x 权限的文件 mycommand,其中包含 Python shebang #!...然后从命令行调用 mycommand 工作,无论当前目录如何.

With Linux, if you create a file mycommand with +x permission in a folder which is in the PATH, containing a Python shebang #!..., then calling mycommand from command line works, no matter the current directory.

如何在 Windows 上使用等效程序? 即我有一个文件夹 D:\myscripts\ 位于 PATH 中,其中包含一些个人脚本,例如 mycommand.py.

How to have the equivalent on Windows? i.e. I have a folder D:\myscripts\ which is in the PATH, containing a few personal scripts like mycommand.py.

如何使从命令行调用 mycommand 或者只是 python mycommand 都可以工作,无论当前工作目录如何?

How to make that calling mycommand or maybe just python mycommand from commandline will work, no matter the current working directory?

TL;DR:我想避免不得不做

TL;DR: I'd like to avoid to have to do

python D:\myscripts\command.py

每次我想从 D:\myscripts\

注意:.py 文件与我的文本编辑器相关联(即当双击 .py 文件时,它是用文本编辑器打开的),我想保留它.

Note: .py files are associated with my text editor (i.e. when double-clicking on a .py file, it is opened with the text editor) and I want to keep this.

推荐答案

解决方案 1

我终于做到了:

  • put D:\myscripts\ in the PATH (with setx, see How to update system PATH variable permanently from cmd?)

创建一个批处理文件:D:\myscripts\mycommand.bat 包含:

create a batch file: D:\myscripts\mycommand.bat containing:

  @python d:\myscripts\mycommand.py

就是这样!

以下是来自@ErykSun 的各种评论的解决方案:

Here is a solution from various comments from @ErykSun:

  • 打开系统环境变量编辑器并确保D:\myscripts"位于 PATH(不要使用引号)和.PY"中;在 PATHEXT 中(不要使用引号).

  • Open the system environment variables editor and ensure that "D:\myscripts" is in PATH (do not use quotes) and ".PY" is in PATHEXT (do not use quotes).

使用 import sys; 行创建一个测试文件 D:\myscripts\test_command.py打印(系统.可执行文件);打印(sys.argv).

Create a test file D:\myscripts\test_command.py with the line import sys; print(sys.executable); print(sys.argv).

在从资源管理器打开的新命令提示符(以获取更新的环境变量)中,从另一个目录中以 test_command spam egg 运行测试脚本.如果它以预期的 Python 安装和命令行参数垃圾邮件"运行,和鸡蛋"正确通过,然后你就完成了.

In a new command prompt that was opened from Explorer (to get the updated environment variables), run the test script from another directory as test_command spam eggs. If it runs with the expected Python installation and the command-line arguments "spam" and "eggs" are correctly passed, then you're done.

在命令提示符下双击并运行时,shell 执行文件类型的默认操作.如果文件类型没有明确定义默认操作,shell 将使用open"动作,如果没有定义它使用文件类型的第一个定义的动作,无论是什么.Python 配置了一个开放的"对其Python.File"的操作使用 IDLE 进行编辑的文件类型和其他上下文菜单(右键单击)操作.

When double-clicking and running at the command prompt, the shell executes the default action for the filetype. If the filetype doesn't explicitly define a default action, the shell uses the "open" action, and if that's not defined it uses the filetype's first defined action, whatever that is. Python configures an "open" action for its "Python.File" filetype and additional context-menu (right-click) actions for editing with IDLE.

您还可以使用其他方法.您可以使用外壳链接.添加.LNK"到 PATHEXT 并删除.PY".然后,对于您要运行的每个脚本,在 D:\myscripts 中创建一个 shell 链接(.LNK 快捷方式文件),以path\to\python.exe"的形式显式运行脚本.\path\to\script".将工作目录字段留空,以便它继承父进程的工作目录.例如,创建 D:\myscripts\test_command.lnk 来运行 D:\myscripts\test_command.py.

There are other directions you can go with this. You can use shell links. Add ".LNK" to PATHEXT and remove ".PY". Then for each script that you want to run, create a shell link (.LNK shortcut file) in D:\myscripts that runs the script explicitly as "path\to\python.exe" "\path\to\script". Leave the working directory field empty, so that it inherits the working directory of the parent process. For example, create D:\myscripts\test_command.lnk to run D:\myscripts\test_command.py.

在这种情况下,您可以将脚本作为 test_command 运行,它将查找并执行test_command.lnk".要获得默认的 Sublime 编辑操作,您可以使用.PY"显式运行 test_command.py扩展名.

In this case, you would run the script as test_command, which will find and execute "test_command.lnk". To get the default Sublime edit action, you would explicitly run test_command.py with the ".PY" extension.

使用以下方法在 c:\python37\scripts\ 中创建一个 .exe 文件:如何从C:\Python37\Scripts?但它更复杂,因为它需要一个包等.

Create an .exe file in c:\python37\scripts\ with this method: How to create a .exe similar to pip.exe, jupyter.exe, etc. from C:\Python37\Scripts? but it's more complicated since it requires a package, etc.

这篇关于使用 Windows 从命令行调用 Python 脚本(在 PATH 中的目录中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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