[Tutor] Re: Running Python from UNIX

Derrick 'dman' Hudson dman@dman.ddts.net
Tue, 6 Aug 2002 10:08:22 -0400


--ikeVEW9yuYc//A+q
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Tue, Aug 06, 2002 at 04:30:15PM +0530, Anand Ramakrishna wrote:
| Hi Valdas,
| Thanks a lot. It was an indentation problem as you correctly
| guessed. Thanks a lot. I have some confusing questions.
|=20
| 1. From my UNIX command prompt (I am using bash shell), if I type
| python, I go to python shell, then when I type=20
| print 'Hello World'  --> This prints Hello World.
|=20
| When I type
| print 'Hello World' in a file (say newfile.p), then give execute
| permissions for the file and if I type in my UNIX command prompt
| python newfile.p  --> This prints Hello World.
|=20
| When I type
| python print 'Hello World' in a file and then execute the file. It
| gives out an error saying python not found.

How are you executing the file?  If the file contains exactly

------
python print 'Hello World'
------

And you run it like

$ ./foo.py

Then you should get this error message :

python: can't open file 'print'


What happens in this case is bash tries to interpret the script itself
since there is no #! line.  If 'python' is in $PATH then bash can run
python, but python treats those arguments as the names of the files
containing the python script.

| When I type
| #! python print 'Hello World' in a file and then execute the file,
| it gives out an error saying python not found.

The #! line needs an absolute path, and you must put a line break in
between the #! line and the rest of the file.

------
#!/usr/bin/env python
print 'Hello World'
------

(There are various tradeoffs in using /usr/bin/env versus
/usr/bin/python2.2, but I won't get into that in this thread.  If you
are interested, take a look in the archives; it was discussed within
the last month or two.)

| When I type from my UNIX command prompt
| python print 'Hello World'  --> This gives an error message saying
| print command not found.

I get this error :

$ python print 'Hello World'
python: can't open file 'print'

for the same reason as above -- python treats those arguments as
filenames.  If you want to run a one-line script from the command line
like that use the -c option :

$ python -c "print 'Hellow World'"
Hellow World

| I am not able to understand this observation. Hope an expert like
| you can help me through.

HTH,
-D

--=20
Failure is not an option.  It is bundled with the software.
=20
http://dman.ddts.net/~dman/

--ikeVEW9yuYc//A+q
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAj1P2FUACgkQO8l8XBKTpRRJPwCfURD766bpnSbdHVgK8WlP8bz3
2YUAoKWmtoqe6eNJ5r9kWQQfJROn25YL
=sgrb
-----END PGP SIGNATURE-----

--ikeVEW9yuYc//A+q--