如果您是编码员或程序员,您可能会花费大量时间使用命令提示符来执行程序或编译代码。为了完成这些任务,您很可能必须使用系统上安装的库或软件包(如Python)中的命令。
默认情况下,这些程序中的大多数都会将自己的自定义快捷方式添加到Windows环境变量中。Windows中最常用的环境变量可能是PATH变量。它基本上允许您在命令提示符处运行位于变量中指定的路径内的任何可执行文件,而无需提供可执行文件的完整路径。
在本文中,我将向您展示如何向Windows PATH变量添加更多路径,以防您希望从自己的自定义目录运行可执行文件。值得注意的是,以下过程适用于Windows 10 ,但对于(Windows 10)Windows 7也几乎完全相同。
将目录添加到 PATH 变量
要开始使用,请右键单击桌面上的计算机(Computer)或此 PC(This PC)图标,然后选择属性(Properties)。如果您的桌面上还没有该图标,您可以轻松添加任何丢失的桌面图标(add any missing desktop icons)。
在“系统(System)”对话框页面上,您会在左侧看到“高级系统设置”链接。(Advanced system settings)
这将打开“系统属性(System Properties)”对话框,该对话框应该已经打开到“高级(Advanced)”选项卡。继续并单击最底部的环境变量按钮。(Environment Variables)
在“环境变量(Environment Variables)”对话框中,您将看到两组变量:一组用于用户变量,另一组用于系统变量。两个列表都有PATH变量,因此您必须决定要编辑哪一个。
如果您只需要您自己的用户帐户的命令,则编辑用户变量。如果您需要它在计算机系统上工作而不管哪个用户登录,然后编辑系统变量。单击(Click)路径(Path),然后单击编辑(Edit)。
在编辑环境变量(Edit environment variable)对话框中,您将看到当前在PATH变量中的所有路径的列表。如您所见,Node.js和Git已经添加了它们的路径,这样我就可以在命令提示符下从任何地方运行Git命令和Node.js命令。(Node.js)
要添加新路径,只需单击新建(New),它将在列表底部添加一个新行。如果您知道路径,只需输入或复制粘贴即可。如果您愿意,也可以单击浏览(Browse),然后导航到所需的路径。
要编辑任何路径,只需选择它,然后单击“编辑(Edit)”按钮。您还可以使用删除(Delete)按钮删除路径。请注意,您还可以在列表中上下移动项目。当您在命令提示符下键入命令时,Windows必须搜索存储在PATH变量中的每个目录,以查看该可执行文件是否存在。如果您希望更快地找到可执行文件,只需将该路径移至列表顶部即可。
如果您在不同的路径中有同一命令的多个版本并且需要运行一个而不是另一个,这也可以派上用场。当您键入命令时,将运行列表中显示较高的那个。
最后,如果您单击Edit text,它将加载一个对话框,您可以在其中使用旧界面编辑Path变量,其中所有路径都列在一个文本框中。(Path)
这里的所有都是它的!如果您想了解有关环境变量的更多信息,请务必查看我关于如何创建自己的自定义环境变量的帖子。享受!
How to Add to Windows PATH Environment Variable
If you’re a coder or programmer, you probably spend a decent amount of time using the command рrompt to executе programs or compile code. In order to complete thoѕe tasks, yoυ most likelу have to use a command frоm a librarу or software package installed (like Python) on your system.
By default, most of these programs will add their own custom shortcuts to the Windows environment variables. The most used environment variable in Windows is probably the PATH variable. It basically allows you to run any executables that are located inside the paths specified in the variable at the command prompt without having to give the full path to the executable.
In this article, I’ll show you how you can add more paths to the Windows PATH variable in case you want to run executables from your own custom directories. It’s worth noting that the procedure below is for Windows 10, but it’s almost exactly the same for Windows 7 also.
Add Directories to PATH Variable
To get started, right-click on the Computer or This PC icon on the desktop and select Properties. If you don’t have that icon on your desktop already, you can add any missing desktop icons easily.
On the System dialog page, you’ll see an Advanced system settings link on the left-hand side.
This will bring up the System Properties dialog, which should already be open to the Advanced tab. Go ahead and click on the Environment Variables button at the very bottom.
On the Environment Variables dialog, you’ll see two sets of variables: one for user variables and the other for system variables. Both lists have the PATH variable, so you have to decide which one to edit.
If you only need the commands for your own user account, then edit the user variable. If you need it to work across the computer system regardless of which user is logged in, then edit the system variable. Click on Path and then click on Edit.
On the Edit environment variable dialog, you’ll see a list of all the paths that are currently in the PATH variable. As you can see, Node.js and Git already added their paths so that I can run Git commands and Node.js commands from anywhere while in the command prompt.
To add a new path, simply click on New and it’ll add a new line to the bottom of the list. If you know the path, simply type it in or copy and paste it. If you prefer, you can also click Browse and then navigate to the desired path.
To edit any path, simply select it and then click on the Edit button. You can also delete paths using the Delete button. Note that you can also move items up and down on the list. When you type a command at the command prompt, Windows has to search through each directory stored in the PATH variable to see if that executable exists or not. If you want your executable to be found faster, just move that path up to the top of the list.
This can also come in handy if you have multiple versions of the same command in different paths and need to have one run instead of the other. The one that shows up higher in the list will be run when you type in the command.
Lastly, if you click on Edit text, it will load a dialog where you can edit the Path variable using the old interface where all the paths are listed in one text box.
That’s all there is to it! If you want to learn more about environment variables, make sure to check out my post on how to create your own custom environment variables. Enjoy!