Start with the first error you received. It is important to understand the error messages.
-bash: helloworld.py: command not found
This means that helloworld.py is not a command that can be executed. To run a file, you have two options:
- Run it using the python interpreter.
python helloworld.py
- Make an executable file and run it directly.
./helloworld.py
To make files executable in a * nix environment, you must change their mode to allow execution. To do this, you use the chmod command ( man chmod
for more information).
chmod +x helloworld.py
It is assumed that you are in the directory containing the helloworld.py file. If not, cd
there first or use the full path.
./
necessary because it tells the shell to run the file located here, rather than looking in $PATH
. $PATH
- a list of possible executable places. When you try to run helloworld.py
directly, the shell tries to find it in $PATH
. You want to run a local file, so you need to attach it to ./
, which means "from here."
As a note, pay attention to the first line of your python script:
This is called the shebang line and tells the system to use the /usr/bin/python
executable to load the file. Internally, this means that the program loader will execute /user/bin/python helloworld.py
.
Finally, when you called python
with no arguments, you were deleted in an interactive Python interpreter session. >>> helloworld.py
in this environment does not refer to a file of this name, it is simply interpreted as python code. Invalid python code. That's why you get your second error, NameError: name 'helloworld' is not defined
.
source share