1

I am following this guide for python 2.7 : http://docs.python.org/tutorial/interpreter.html I do all it says: I have a python file:

#! /usr/bin/env python

print "hello world !\n"

And from terminal, in the directory where is the file I type:

chmod +x hello_world.py

The file is name hello_world.py; But nothing happens, it doesn't print "hello world\n".

2 Answers 2

4

sorry if this is insultingly obvious, but

> chmod +x hello_world.py

only changes the file so that you can run it. next you need to actually run the file by typing:

> ./hello_world.py
hello world !
2
  • And where's the executable? The .py one is source code. Before doing this I could run it just typing: "python hello_world.py", this wasn't obvious, what's the difference and where's the executable? Commented Feb 24, 2012 at 0:44
  • python is not a compiled language. what you are doing is making an executable shell script. when you run it, as a script, the first line "says" to use python to run the program. however, python does often create a bytecode file that is "half-compiled" - that is in the same directory and ends in ".pyc" (but i think this example is too simple to create one). Commented Feb 24, 2012 at 0:49
2

To give a bit more description: the chmod command changes the permissions of a file on a Unix-style system. The +x in the command:

chmod +x hello_world.py

Sets the "Executable" bit for the hello_world.py file, thereby making it a script which can be executed. Thus to run the script:

./hello_world.py

The ./ in front indicates that the file is in the current directory. Alternatively, you can always run a script by invoking the python interpreter directly (regardless of permissions) like so:

python hello_world.py

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.