如何编写一个批处理文件,显示可执行文件的路径和 Python 版本,在 Windows 上处理 Python 脚本?
对于直接调用 Python 运行的脚本 (python myscript.py
) 以及直接运行的脚本 (myscript.py
),它应该显示可执行文件的路径和 Python 版本。脚本不应该对系统的配置做出太多假设。例如,它应该处理可能没有可用的 Python 的情况。
基本原理
我正在尝试设置运行 Python 脚本的环境的不同方法,我认为让一个脚本告诉我当前的配置是什么会很有帮助。我关心操作系统提供的标准方法 - PATH
环境变量以及文件类型与处理程序的关联(assoc
和 ftype
命令以及 PATHEXT
环境变量)。这使得 pylauncher 超出了这个问题的范围。
It should display path to executable and version of Python for scripts run with direct invocation of Python (python myscript.py
) as well as for scripts run directly (myscript.py
). Script should not make too many assumptions on the configuration of the system. For instance it should handle situation where there might be no available Python.
Rationale
I'm playing with different ways of setting environment for running Python scripts and I thought it would be helpful to have a script telling me what the current configuration is. I'm concerned with the standard means provided by OS - the PATH
environment variable and association of files' types with handlers (assoc
and ftype
commands as well as PATHEXT
environment variable). This leaves pylauncher outside of the scope of this question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能就是您正在寻找的:
This might be what you're looking for:
这是我的第一个解决方案:
解决方案 1
但是,我遇到了问题。当没有与 Python 文件关联的有效 Python 解释器时,尝试使用
some_script.py
打开 Python 文件时,会弹出打开方式系统对话框。解决这个问题需要对批处理文件有很好的了解。因此,试图提出一个解决方案,我提出了以下问题:原始批处理文件的改进版本现在如下所示:
解决方案 1b
这是避免上述两个问题的另一种方法:
解决方案2
...以及改进版本:
解决方案 2b
Here's my first solution:
SOLUTION 1
I run into problem with this, however. When there's no valid Python interpreter associated with Python files trying to open Python file with
some_script.py
pops up Open With system dialog. Solving this problem requires very good knowledge of batch files. Therefore trying to come up with a solution I've asked the following questions:Improved version of the original batch file looks now like this:
SOLUTION 1b
This is another take avoiding problems of the above two:
SOLUTION 2
... and improved version:
SOLUTION 2b