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

时间:2021-11-06 20:50:11

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:

我可以用一个例子来解释我的问题。我最近下载了Python for Windows,安装到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 any folder:

但是,如果我可以在任何文件夹中执行此操作,那将是非常酷的:

> python myscript.py

How do I make that "global"?

我该如何制作“全球”?

3 个解决方案

#1


You just need to add the path C:\Python to your Path Environment Variable which can be modified from the Advanced tab of the System Properties control panel.

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

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

#2


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

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

  1. Add python.exe to your system PATH environment variable, if it's not already there.
  2. 将python.exe添加到系统PATH环境变量(如果它尚不存在)。

  3. Add ;.py to the end of your PATHEXT system environment variable.
  4. 将.py添加到PATHEXT系统环境变量的末尾。

Then, instead of typing

然后,而不是打字

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

or

> python myscript.py

you can just type

你可以输入

> myscript.py

#3


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.

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

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

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

Marc

#1


You just need to add the path C:\Python to your Path Environment Variable which can be modified from the Advanced tab of the System Properties control panel.

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

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

#2


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

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

  1. Add python.exe to your system PATH environment variable, if it's not already there.
  2. 将python.exe添加到系统PATH环境变量(如果它尚不存在)。

  3. Add ;.py to the end of your PATHEXT system environment variable.
  4. 将.py添加到PATHEXT系统环境变量的末尾。

Then, instead of typing

然后,而不是打字

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

or

> python myscript.py

you can just type

你可以输入

> myscript.py

#3


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.

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

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

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

Marc