Shebang line problems and python

B

Blaine

Hello,

Scripts that have "#!/usr/bin/python" at the top do not parse
correctly. Bash treats scripts with that shebang as if they are bash
scripts.

E.g.:
blaine@attila ~/apps/rs-mu $ /usr/sbin/env-update
/usr/sbin/env-update: line 6: import: command not found
/usr/sbin/env-update: line 8: syntax error near unexpected token `('
/usr/sbin/env-update: line 8: `def usage(status):'

Scripts with "#!/usr/bin/env python" at the top work fine. In fact,
`python` and `env python` both bring me to a python interpreter.

I've tried other types of scripts. #!/bin/bash works, #!/usr/bin/perl
works, #!/usr/bin/ruby works, etc. (and so do their #!/usr/bin/env
<interpreter> counterparts.) Because of this, I'm not sure if it is a
Python issue or a system issue.

Any and all advice appreciated, thanks.

P.S. some system info:
blaine@attila ~/apps/rs-mu $ uname -a
Linux attila 2.6.27-gentoo #5 SMP Sun Oct 19 19:13:17 MST 2008 i686
AMD Athlon(tm) 64 X2 Dual Core Processor 4000+ AuthenticAMD GNU/Linux

blaine@attila ~/apps/rs-mu $ python --version
Python 2.6.2

blaine@attila ~/apps/rs-mu $ bash --version
GNU bash, version 4.0.28(2)-release (i686-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/
gpl.html>
 
C

Chris Rebert

Hello,

Scripts that have "#!/usr/bin/python" at the top do not parse
correctly. Bash treats scripts with that shebang as if they are bash
scripts.

E.g.:
blaine@attila ~/apps/rs-mu $ /usr/sbin/env-update
/usr/sbin/env-update: line 6: import: command not found
/usr/sbin/env-update: line 8: syntax error near unexpected token `('
/usr/sbin/env-update: line 8: `def usage(status):'

Scripts with "#!/usr/bin/env python" at the top work fine. In fact,
`python` and `env python` both bring me to a python interpreter.

I've tried other types of scripts. #!/bin/bash works, #!/usr/bin/perl
works, #!/usr/bin/ruby works, etc. (and so do their #!/usr/bin/env
<interpreter> counterparts.) Because of this, I'm not sure if it is a
Python issue or a system issue.

Any and all advice appreciated, thanks.

Admittedly stupid questions:
1. Is there actually anything located at /usr/bin/python ?
2. What does "which python" output in bash?

Cheers,
Chris
 
R

Rami Chowdhury

Is it possible that python is installed not in /usr/bin but in, say, /
usr/local/bin?

I'd suggest you try 'which python' to find out where it is. On my OS
X, for instance:

$ which python
/Library/Frameworks/Python.framework/Versions/Current/bin/python
 
B

Blaine

Chris Rebert, Rami Chowdhury:
blaine@attila ~/tmp $ which python
/usr/bin/python

Ben Finney:
blaine@attila ~/tmp $ echo $SHELL
/bin/bash
blaine@attila ~/tmp $ cat ./shebang-test
#!/usr/bin/python
import sys
sys.stdout.write("Hello, world.\n")
blaine@attila ~/tmp $ ./shebang-test
../shebang-test: line 2: import: command not found
../shebang-test: line 3: syntax error near unexpected token `"Hello,
world.\n"'
../shebang-test: line 3: `sys.stdout.write("Hello, world.\n")'

blaine@attila ~/tmp $ cat shebang-test2
#!/usr/bin/env python
import sys
sys.stdout.write("Hello, world.\n")
blaine@attila ~/tmp $ ./shebang-test2
Hello, world.

Thanks.
 
S

Steven D'Aprano

blaine@attila ~/tmp $ cat ./shebang-test
#!/usr/bin/python
import sys
sys.stdout.write("Hello, world.\n")
blaine@attila ~/tmp $ ./shebang-test
./shebang-test: line 2: import: command not found ./shebang-test: line
3: syntax error near unexpected token `"Hello, world.\n"'
./shebang-test: line 3: `sys.stdout.write("Hello, world.\n")'


I've seen something similar to this, which was caused by invisible ctrl-Z
characters somehow getting into my text file. If you view a hexdump of
the file, are there any unexpected characters in the file, particularly
before or between the # ! characters?

If not, what happens if you run the file directly with Python?

python shebang-test
 
B

Blaine

I've seen something similar to this, which was caused by invisible ctrl-Z
characters somehow getting into my text file. If you view a hexdump of
the file, are there any unexpected characters in the file, particularly
before or between the # ! characters?

I'm not sure if there is a Ctrl+Z in here... but, here's the output:
blaine@attila ~/tmp $ hexdump shebang-test
0000000 2123 752f 7273 622f 6e69 702f 7479 6f68
0000010 0a6e 6d69 6f70 7472 7320 7379 730a 7379
0000020 732e 6474 756f 2e74 7277 7469 2865 4822
0000030 6c65 6f6c 202c 6f77 6c72 2e64 6e5c 2922
0000040 000a
0000041

If not, what happens if you run the file directly with Python?

python shebang-test

blaine@attila ~/tmp $ python shebang-test
Hello, world.
 
J

Johan Grönqvist

Blaine skrev:
I'm not sure if there is a Ctrl+Z in here... but, here's the output:
blaine@attila ~/tmp $ hexdump shebang-test
0000000 2123 752f 7273 622f 6e69 702f 7479 6f68
0000010 0a6e 6d69 6f70 7472 7320 7379 730a 7379
0000020 732e 6474 756f 2e74 7277 7469 2865 4822
0000030 6c65 6f6c 202c 6f77 6c72 2e64 6e5c 2922
0000040 000a
0000041

You can perhaps use "hexdump -c shebang-test"
to get characters instead of hexadecimals.

/ johan
 
B

Blaine

Hello,

Scripts that have "#!/usr/bin/python" at the top do not parse
correctly. Bash treats scripts with that shebang as if they are bash
scripts.

E.g.:
blaine@attila ~/apps/rs-mu $ /usr/sbin/env-update
/usr/sbin/env-update: line 6: import: command not found
/usr/sbin/env-update: line 8: syntax error near unexpected token `('
/usr/sbin/env-update: line 8: `def usage(status):'

Scripts with "#!/usr/bin/env python" at the top work fine. In fact,
`python` and `env python` both bring me to a python interpreter.

I've tried other types of scripts. #!/bin/bash works, #!/usr/bin/perl
works, #!/usr/bin/ruby works, etc. (and so do their #!/usr/bin/env
<interpreter> counterparts.) Because of this, I'm not sure if it is a
Python issue or a system issue.

Any and all advice appreciated, thanks.

P.S. some system info:
blaine@attila ~/apps/rs-mu $ uname -a
Linux attila 2.6.27-gentoo #5 SMP Sun Oct 19 19:13:17 MST 2008 i686
AMD Athlon(tm) 64 X2 Dual Core Processor 4000+ AuthenticAMD GNU/Linux

blaine@attila ~/apps/rs-mu $ python --version
Python 2.6.2

blaine@attila ~/apps/rs-mu $ bash --version
GNU bash, version 4.0.28(2)-release (i686-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/
gpl.html>

I think I've discovered the problem. Someone from IRC had directed me
here: http://bugs.gentoo.org/show_bug.cgi?id=279915
 
D

Diez B. Roggisch

Blaine said:
Hello,

Scripts that have "#!/usr/bin/python" at the top do not parse
correctly. Bash treats scripts with that shebang as if they are bash
scripts.

E.g.:
blaine@attila ~/apps/rs-mu $ /usr/sbin/env-update
/usr/sbin/env-update: line 6: import: command not found
/usr/sbin/env-update: line 8: syntax error near unexpected token `('
/usr/sbin/env-update: line 8: `def usage(status):'

Scripts with "#!/usr/bin/env python" at the top work fine. In fact,
`python` and `env python` both bring me to a python interpreter.

I've tried other types of scripts. #!/bin/bash works, #!/usr/bin/perl
works, #!/usr/bin/ruby works, etc. (and so do their #!/usr/bin/env
<interpreter> counterparts.) Because of this, I'm not sure if it is a
Python issue or a system issue.

Any and all advice appreciated, thanks.

Do you have a DOS-file-ending in your script by any chance?

Diez
 
D

Dennis Lee Bieber

Blaine skrev:

You can perhaps use "hexdump -c shebang-test"
to get characters instead of hexadecimals.
<ctrl-z> shouldn't be that difficult to figure out... "z" is 26
letter of alphabet, so <ctrl-z> should be decimal 26... 16 + 10... or
'1A'x

Not seen above...
 
B

Blaine

Hello,

Scripts that have "#!/usr/bin/python" at the top do not parse
correctly. Bash treats scripts with that shebang as if they are bash
scripts.

E.g.:
blaine@attila ~/apps/rs-mu $ /usr/sbin/env-update
/usr/sbin/env-update: line 6: import: command not found
/usr/sbin/env-update: line 8: syntax error near unexpected token `('
/usr/sbin/env-update: line 8: `def usage(status):'

Scripts with "#!/usr/bin/env python" at the top work fine. In fact,
`python` and `env python` both bring me to a python interpreter.

I've tried other types of scripts. #!/bin/bash works, #!/usr/bin/perl
works, #!/usr/bin/ruby works, etc. (and so do their #!/usr/bin/env
<interpreter> counterparts.) Because of this, I'm not sure if it is a
Python issue or a system issue.

Any and all advice appreciated, thanks.

P.S. some system info:
blaine@attila ~/apps/rs-mu $ uname -a
Linux attila 2.6.27-gentoo #5 SMP Sun Oct 19 19:13:17 MST 2008 i686
AMD Athlon(tm) 64 X2 Dual Core Processor 4000+ AuthenticAMD GNU/Linux

blaine@attila ~/apps/rs-mu $ python --version
Python 2.6.2

blaine@attila ~/apps/rs-mu $ bash --version
GNU bash, version 4.0.28(2)-release (i686-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/
gpl.html>

Okay, in case anyone is curious, here is how I fixed the problem:
downgraded app-admin/eselect-python-20090801 to app-admin/eselect-
python-20090606
`rm /usr/bin/python/`
`ln -s -T /usr/bin/python2.6 /usr/bin/python`
`emerge python`
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top