windows 如何在不键入完整路径的情况下使程序可从命令行运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1145231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:50:07  来源:igfitidea点击:

How can I make a program runnable from the commandline without typing its full path?

windowscommand-line

提问by soapergem

I can best explain my question with an example. I recently downloaded Python for Windows, installed to C:\Python. So if I'm in folder X that contains myscript.py, and I want to invoke it, I have to call this:

我可以用一个例子来最好地解释我的问题。我最近下载了适用于 Windows 的 Python,安装到 C:\Python。因此,如果我在包含 myscript.py 的文件夹 X 中,并且我想调用它,我必须调用它:

> C:\Python\python.exe myscript.py

But it would be super-cool if I could just do this, from within anyfolder:

但是,如果我可以从任何文件夹中执行此操作,那就太酷了:

> python myscript.py

How do I make that "global"?

我如何使这个“全球”?

回答by Mark Biek

You just need to add the path C:\Pythonto your PathEnvironment Variable which can be modified from the Advancedtab of the System Propertiescontrol panel.

您只需要将路径C:\Python添加到您的路径环境变量中,该变量可以从系统属性控制面板的高级选项卡中进行修改。

回答by Patrick Cuff

To eliminate the need to type pythonbefore your script, you can do the following:

要消除在python脚本之前键入的需要,您可以执行以下操作:

  1. Add python.exeto your system PATH environment variable, if it's not already there.
  2. Add ;.pyto the end of your PATHEXT system environment variable.
  1. 添加python.exe到您的系统 PATH 环境变量中(如果它尚未存在)。
  2. 添加;.py到 PATHEXT 系统环境变量的末尾。

Then, instead of typing

然后,而不是键入

> C:\Python\python.exe myscript.py

or

或者

> python myscript.py

you can just type

你可以输入

> myscript.py

回答by marc_s

Another possible solution would be to add an entry to the registry:

另一种可能的解决方案是在注册表中添加一个条目:

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\python.exe

and then set the (default)value of that to the path (+ program name) where your python.exe resides, e.g. C:\Python\Python.exe.

然后将其(default)值设置为您的 python.exe 所在的路径(+ 程序名称),例如C:\Python\Python.exe.

That way, you can call python.exe from anywhere - no path or other stuff needed.

这样,您可以从任何地方调用 python.exe - 不需要路径或其他东西。

Marc

马克