View Single Post
jldiaz's Avatar
Posts: 48 | Thanked: 40 times | Joined on Apr 2008 @ Spain
#8
Originally Posted by cmdowns View Post
Problem: I'm trying to teach myself Python. The Python tutorial I'm using at byteofpyton.org says, in order to execute a python program that I write from any directory within the CLI, I need to edit the "PATH environment variable". That is, if I want to run my Python programs without first cd'ing to the dir in which the programs exist, I need to set up the "PATH environment variable".
Background about paths

In order to execute a program (any program, not just python programs) from the CLI, you have to write the full path to that program. You can write an absolute path (which starts at the root directory named "/"), or a relative path (which starts at the "current directory"). For example, if you have written a program named "mytest.py", and placed that program in the folder /home/user/myscripts/, then in order to execute that program you have to write:

Code:
/home/user/myscripts/mytest.py
The above command works always, no matter what your current directory is. If it happens that you "current directory" is /home/user/myscripts (because you used the cd command to reach it), then you can simply write:

Code:
./mytest.py
of course, this command only works when the program is in the same directory than you (i.e: in your current directory).

If you do not specify any path before the program name, i.e: if you simply write: mytest.py in the CLI, and press Return, then the PATH variable come into action. This variable holds a list of directories, separated by colons. The CLI will search the program you want to run in this list, one directory at turn. When the program is found, it is run. If the list is exhausted and the program is not found, you obtain the message:

Code:
-sh: mytest.py: not found
You can look at the content of the PATH variable, by issuing the command (in the CLI)

Code:
echo $PATH
The default value is /usr/bin:/bin. This means that when you write a program name in the CLI and hit Return, the shell looks first into the directory /usr/bin and, if it is not found there, then in the directory /bin.

You can alter the PATH variable, by assigning it a new value, and "exporting" it. For example:

Code:
export PATH=/home/user/myscripts
This way, you can write simply "mytest.py" in the CLI, and your script will be found and executed, no matter what you current directory is. But, wait a minute... the other commands like "ls", "mkdir", etc.. stopped working! What happened? You have altered the PATH variable, so the default directories /usr/bin and /bin are no longer searched for commands!!

No panic. The changes to the PATH variable are local. They dissapear if you end the CLI session, so close the xterm and open a new instance. That is, you have your PATH restored, everything works again.

If you want to *add* a new directory to your PATH variable, as opposed to *replace* it with a new directory, you can use the following syntax:

Code:
export PATH=$PATH:/home/user/myscripts
The symbol $PATH is "expanded" (i.e: substituted for the value of the PATH variable). So the above line is equivalent to write:

Code:
export PATH=/usr/bin:/bin:/home/user/myscripts
only better, because it works for any value of the PATH variable, appending a new directory at the end. (You can also append it to the beginning, it depends on what search order you prefer, which is important if you keep several programs with the same name at different directories).

So this is the whole story about the PATH variable? Well, it is if you dont mind to write the PATH assignation each time you open xterm. Remember that changes to the PATH variable are lost when xterm is closed.

If you want to make these changes more permanent, you will be glad to know that, if you write a shell script named .profile and put it in your $HOME, it will be run each time an xterm is open (to be precise, when a shell session is started, even if you start it from ssh). So you can write a .profile which assigns your PATH variable as you like, and have this value assigned automatically in each session.

Creating files from command line

Now for your question. To write .profile (or any other file), you should use an editor. vi is an editor which comes with the OS, but it is difficult to use. However, for very short files, you can write them from the command line, using the echo command, or the cat command. I prefer the later:

Code:
cat >> $HOME/.profile
PATH=$PATH:/home/user/myscripts
export PATH
Ctrl-D
(The last Ctrl-D is not to be copied. It means "Press the Ctrl plus the 'D' key")

The cat command, used as above, appends all text you type (up to the Ctrl-D, which means "end of file") to the file you specify after the ">>" You should type carefully, because you cannot edit a line after you press Return. Even before pressing Return, the editing capabilities are very restricted. You only can delete chars (using backspace) and write them again, but you cannot move the cursor in the line. However, for short texts, or for text which are "copy&pasted", cat can be a quick way of creating file contents.
__________________
--ル Diaz

Last edited by jldiaz; 2008-04-16 at 19:26. Reason: Adding section marks
 

The Following 9 Users Say Thank You to jldiaz For This Useful Post: