This page does not make any sense to me? I understand the fact that chmod +x helloworld.py makes it executable, but the second half of the page of "allowing it to be ran anywhere" is so confusing to me. Can anyone help explain it to me?
Executable Python Programs
This applies only to Linux/Unix users but Windows users might be curious as well about the
first line of the program. First, we have to give the program executable permission using
the chmod command then run the source program.
$ chmod a+x helloworld.py
$ ./helloworld.py
Hello World
The chmod command is used here to change the mode of the file by giving execute
permission to all users of the system. Then, we execute the program directly by specifying
the location of the source file. We use the ./ to indicate that the program is located in the
current directory.
To make things more fun, you can rename the file to just helloworld and run it as
./helloworld and it will still work since the system knows that it has to run the program
using the interpreter whose location is specified in the first line in the source file.
What if you don't know where Python is located? Then, you can use the special env
program on Linux/Unix systems. Just change the first line of the program to the following:
#!/usr/bin/env python
The env program will in turn look for the Python interpreter which will run the program.
So far, we have been able to run our program as long as we know the exact path. What if
we wanted to be able to run the program from anywhere? You can do this by storing the
program in one of the directories listed in the PATH environment variable. Whenever you
run any program, the system looks for that program in each of the directories listed in the
PATH environment variable and then runs that program. We can make this program
available everywhere by simply copying this source file to one of the directories listed in
PATH.
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/swaroop/bin
$ cp helloworld.py /home/swaroop/bin/helloworld
$ helloworld
Hello World
We can display the PATH variable using the echo command and prefixing the variable name
by $ to indicate to the shell that we need the value of this variable. We see that
/home/swaroop/bin is one of the directories in the PATH variable where swaroop is the
username I am using in my system. There will usually be a similar directory for your
username on your system. Alternatively, you can add a directory of your choice to the PATH
variable - this can be done by running PATH=$PATH:/home/swaroop/mydir where
'/home/swaroop/mydir' is the directory I want to add to the PATH variable.
This method is very useful if you want to write useful scripts that you want to run the
program anytime, anywhere. It is like creating your own commands just like cd or any
24
Python en:First Steps
other commands that you use in the Linux terminal or DOS prompt.
If I have made a program in a folder, then when I am inside that folder all I need to type to run my program is ./name_of_program.
If I then go to a different folder and enter ./name_of_program the terminal will give an error as it can't see the program I am trying to run in the current directory.
bash: ./my_program: No such file or directory
In Linux there is an environment variable called PATH which is a list of directories to search through to find a program when someone asks to run it. If you add the directory your program lives in to this PATH variable then the operating system will be able to run your program no matter which directory you are in when you type ./name_of_program
You can edit the $PATH variable in Linux by simply adding this to your ~/.bashrc file near the end.
PATH=$PATH:/directory/to/add/to/path:/another/directory
Any directories you add should be full paths to the directory from root.
So to add a folder in your home directory you would type: /home/username/mybin
EDIT:
You need to make sure all the directories exist when using cp.
Use mkdir using the path you want to copy to.
So in your formal example you need to say mkdir /home/metulburr/bin
cp helloworld.py /home/metulburr/bin/helloworld
Of course once ~/bin exists you no longer need to say mkdir to create the bin. Then use my above example to add your bin folder to the path if it isn't there already.
i think i might of figured it out, not sure though, but to make it executable by just typing in 'helloworld' into the terminal was my goal. i think however that by default my terminal is already in home, so i did sudo cp helloworld.py /usr/bin/helloworld
and after that typing in the name without .py worked, is this successful? i am so confused. lol
i would like to learn this, so any suggestion or comments would be helpful, thanks in advance.
Yes you copied your file into a directory that is listed in the PATH so the operating system was able to find it to run it.
It's worth noting that copying it into this directory did not make it executable - it was already executable - you just made it available to be run from anywhere.
The other option is to add a directory of your own into the path so anything you put in there can be found and run.