# How do I run a Python script?



## markpatterson (Sep 9, 2004)

I would like to get past this hurdle: How do I do hello world in a Python script. I took the advice of http://developer.apple.com/unix/crossplatform.html, where they recommend doing this:

```
#!/usr/bin/env python

print "Hello!"
```
I saved that as hello.py, chmod +x'ed it, tried to run it and got this:

```
Pattersons-eMac:~/dev/scripts mark$ hello.py
print "Hello!": No such file or directory
```
What am I doing wrong? It seems to be ignoring python altogether.

TIA,

Mark


----------



## scruffy (Sep 9, 2004)

python is in /usr/bin.  Not sure where the /usr/bin/env comes from - or is that a typo?  The first line should just be:
#!/usr/bin/python

As for the syntax - I don't know python, so I don't know if that's right or not...


----------



## markpatterson (Sep 9, 2004)

scruffy said:
			
		

> python is in /usr/bin.  Not sure where the /usr/bin/env comes from - or is that a typo?  The first line should just be:
> #!/usr/bin/python
> 
> As for the syntax - I don't know python, so I don't know if that's right or not...



I just copied that from the Apple page. See the link in my first post. That is their way to make it cross-platform, so that it will run on unices with things in different places.

I changed the script to:

```
#!/usr/bin/python

print "Hello!"
```
which is a link to the real python, and got this:

```
Pattersons-eMac:/usr/bin mark$ hello.py
print: bad interpreter: No such file or directory/python
```

The python is correct, though. I just tried this:

```
Pattersons-eMac:~/dev/scripts mark$ python hello.py
Hello!
```


----------



## markpatterson (Sep 9, 2004)

I think I've solved it. I used HexEditor (www.ex-cinder.com), and found that the line breaks were 0D. I changed them to 0A and finally got

```
Pattersons-eMac:~ mark$ hello.py
Hello!
```
from

```
#! /usr/bin/env python

print "Hello!"
```


----------



## scruffy (Sep 9, 2004)

interesting  - from the env manpage:

     env [-i] [name=value ...] [utility [argument ...]]

DESCRIPTION
     env executes utility after modifying the environment as specified on the
     command line.  The option name=value specifies an environmental variable,
     name, with a value of value.  The option `-i' causes env to completely
     ignore the environment it inherits.

So, in this case, "/usr/bin/env python" is exactly equivalent to "/usr/bin/python", since it doesn't alter any environment variables...  I wonder why they do it that way then.


----------



## markpatterson (Sep 10, 2004)

If you look at this page: http://python.org/doc/current/tut/node4.html there are 2 different places for python from /usr/bin:
"The Python interpreter is usually installed as /usr/local/bin/python ... /usr/local/python is a popular alternative location"

Down the page a few paragraphs it gives the same idea as the Apple page:
"On BSD'ish Unix systems, Python scripts can be made directly executable, like shell scripts, by putting the line

#! /usr/bin/env python

(assuming that the interpreter is on the user's PATH) at the beginning of the script"

So I guess env is used because it is always at that location, and can be used to find something on the path.


----------

