Significance of a PATH explained

大城市里の小女人 提交于 2021-02-19 01:20:18

问题


This is probably a rudimentary question but I am still kinda new to programming and I've wondered for awhile. I've done multiple projects in Python, C#, and Java, and when I try to use new libraries (especially for Python) people always say to make sure its in the right PATH and such. I just followed an online tutorial on how to install Java on a new computer and it rekindled my question of what a path really is. Is the Path just were the programming language looks for a library in the file system? I get kinda confused on what it's significance is. Again, I'm sorry for the wide question, its just something that I've never quite gotten on my own programming.

EDIT: I just wanted to thank everyone so much for answering my question. I know it was a pretty dumb one now that I've finally figured out what it is, but it really helped me. I'm slowly working through as many C#, Java and Python tutorials as I can find online, and it's nice to know I have somewhere to ask questions :)


回答1:


A PATH is a file directory on your computer. If you need to install a programming language, you might need to put it in your system PATH variable. This means that the system looks to these files for different information, IE where the libraries for the code you are using are.
Hope that helped!




回答2:


The PATH is an environment variable which the shell (or other command interpreter) uses to search for commands. Usually (always?) commands are found with a greedy algorithm, so entries that come first in the PATH are returned first. For example, a command in /usr/local/bin will override a command in /usr/bin given a PATH such as

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

while the purpose is consistent, the syntax is slightly different on WINDOWS - you would use

C:\> ECHO %PATH%

to "echo" your PATH.

First my shell is going to search /usr/local/sbin then /usr/local/bin then /usr/sbin and then /usr/bin before searching /sbin and /bin if the command isn't found then it will report that it couldn't find such a command...

# Like so
$ thisprogramdoesntexist
thisprogramdoesntexist: command not found

Now, on Linux at least, there's also a LD_LIBRARY_PATH which the system will use to search for dynamic libraries (greedily), on Windows I think it just uses the PATH. Finally, Java uses a CLASSPATH which is similar (but used to search for classes and JARs).

On Linux one might add an entry to the PATH like so,

$ export PATH="$PATH:/addNewFolder"

While on Windows you might use

set PATH=%PATH%;c:\addNewFolder

Sometimes, you might manipulate your PATH(s) to enable specific functionality, see update-java-alternatives on Ubuntu for an example.




回答3:


The best resource (so far) about PATH information, you can see in this question:

https://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them

Stack Overflow is not the best place to search about this, always check the amazing https://superuser.com/ for this kind of question.




回答4:


PATH is a symbolic name usually associated to string values (directory names) separated by a semicolon. This symbolic name is handled by the operating system and could be modified by the end user through the command SET PATH=........

Because of this standardization, it is common practice for tools like compilers or other programming tools to look at this symbolic name and use the list of string values for searching files that are not directly available in the current folder used by the tools.

So, if an installation procedure set the PATH symbol in this way

SET PATH=%path%;C:\PROGRAM FILES\MYTOOLFOLDER;

it means, set the PATH symbol to the previous value (%PATH%) and add another string value to it (C:\PROGRAM FILES\MYTOOLFOLDER).

Then the tool, when it needs to search for a particular file or library, could read the PATH symbol values, split them at the semicolon and iteratively look at the directories listed one by one looking for the library required.

In C# programming, for example, the tool code could contain something like this

string pathSymbol = Environment.GetEnvironmentVariable("PATH");
string[] pathFolders = pathSymbol.Split(';');
foreach(string folder in pathFolders)
{
    if(File.Exists(Path.Combine(folder, "mylibrary.dll"))
    {
        ..... do whatever you need to do with the file
    }
}

This example assumes a Windows environment.




回答5:


Exactly as other said, PATH is a list of folders that is included in the search -other than the current folder- and you can always access straight away. It's one of the Environment Variables.

For example, we have the python folder in C:\Python27. I'm sure you know that to run a python file, we commonly use python script.py.

What happens is that the command line searches for python.exe in your current folder, and if not found, search it in the folders in the path variable.

To read the path, you can, straightforwardly use:

$ PATH

If you're on windows, like i am, an easy way to deal with this is to just use System Properties. Just type it in the start menu, open it, and go to the 'advanced' tab. Click on the Environment Variables, there! You'll see a PATH variable, and you can modify it as you want.

I myself use more than one version of Python, and to deal with this, i appended all the folders to PATH, and changed my python.exe to pythonversion_number.exe. Problem solved! Now, i can run this in the command line:

$ python26 script.py
$ python33 script2.py

Some further reading on this, if you're interested, here's a good question asked

Hope this helps!



来源:https://stackoverflow.com/questions/20294504/significance-of-a-path-explained

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!