LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-08-2012, 08:24 PM   #1
pascor
LQ Newbie
 
Registered: Jul 2012
Location: near Philadelphia, PA, USA
Distribution: Ubuntu 12.04 LTE 32-bit on all platforms
Posts: 6

Rep: Reputation: Disabled
Smile How to get the shebang line to work in python scripts ?


As an Linux/Ubuntu (12.04) newbie I can't figure out the combination of requirements to be able to use the shebang (#!/usr/bin/python) on line #1 to get the file to be interpreted by the python interpreter:

- Does the .py file have to be marked executible ?
- Does the .py file have to be in $PATH ?
- Does any particular shell have to run the py file ?
- Do any extra python environment variables have to be defined ?
- Will I simply only have to type "mypyfile.py <Enter>" at the command line to get the script run when things are set up properly ?

TIA

Last edited by pascor; 07-08-2012 at 08:27 PM.
 
Old 07-08-2012, 08:39 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
If you just want to type the name of your script to run it there are some requirements for that:
1. The file must be executable.
2. The file must be in the $PATH. That means on most distros that you have to prefix the the name with ./ if you are in the same directory as the script.
3. The shebang must be correct. While
Code:
#!/usr/bin/python
may work it is better to use
Code:
#!/usr/bin/env python
 
Old 07-08-2012, 09:21 PM   #3
pascor
LQ Newbie
 
Registered: Jul 2012
Location: near Philadelphia, PA, USA
Distribution: Ubuntu 12.04 LTE 32-bit on all platforms
Posts: 6

Original Poster
Rep: Reputation: Disabled
Nope ! I'm using Ubuntu 12.04 LTE :

$ PATH="$PATH:/home/mack/Desktop/FINDFILE"
$ echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/mack/Desktop/FINDFILE
$
$ ll *.py
-rwxr-xr-x 1 mack mack 15522 Jul 8 21:14 findfile.py*
$
$ ./findfile.py
: No such file or directory


What gives ?!
 
Old 07-08-2012, 09:32 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
Can't say, works here and on any other Linux system I own:
Code:
tobi@dragon ~ :) % cat test.py
#!/usr/bin/env python

print "Hello World!"
tobi@dragon ~ :) % ls -l test.py
-rwxr-xr-x 1 tobi users 44 Jul  9 03:30 test.py
tobi@dragon ~ :) % ./test.py
Hello World!
tobi@dragon ~ :) %
 
Old 07-08-2012, 11:56 PM   #5
pascor
LQ Newbie
 
Registered: Jul 2012
Location: near Philadelphia, PA, USA
Distribution: Ubuntu 12.04 LTE 32-bit on all platforms
Posts: 6

Original Poster
Rep: Reputation: Disabled
The mystery thickens :

$ ls -l *.py
-rwxrwxr-x 1 mack mack 15522 Jul 8 22:33 findfile.py
-rwxrwxr-x 1 mack mack 44 Jul 8 22:32 test.py
$
$ ./test.py
Hello World!
$
$ ./findfile.py
: No such file or directory
$
$ cp findfile.py abc.py
$
$ chmod 775 abc.py
$ ./abc.py
: No such file or directory
$
$ cp test.py abc.py
$ ./abc.py
Hello World!
$

---------------------------------------

I edited file abc.py and replaced it's original content with that of file findfile.py :

---------------------------------------

$ ./abc.py

$ isFrozen : False
+ SCRIPT_ROOT : /home/mack/Desktop/FINDFILE/./abc.py
$ sys.executable : /usr/bin/python
$ sys.prefix : /usr
$ sys.argv : ['./abc.py']

./abc.py usage :
$ python ./abc.py [ options ] filename [, file ...] [ dir [, dir ...] ]

---------------------------------------

File abc.py now works as expected !

THE REASON:

The EOL marker from MSW-created text files is confusing Ubuntu.

THE FIX:

Run utility sed on the MSW-created-text-file-copied-to-Ubuntu as follows :

$ sed -i 's/\r//g' {filename}

----

BTW: Kate does not convert files with "\n\r" EOL markers even though it is set to save in posix EOL format. Character "\r" is an illegal character in posix text files.

----

I'm very surprised and saddened this thread didn't produce a dozen+ replies suggesting doing something like this.

Last edited by pascor; 07-08-2012 at 11:57 PM.
 
Old 07-09-2012, 12:34 AM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
I am not using Windows for coding and I assume many people here don't also. So something like that can be easily forgotten.
 
Old 07-09-2012, 08:11 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
@OP: you'd be amazed how many different ways you can screw up a file ...
Anyway, as above all the experienced guys find it easier/quicker to edit direct on *nix anyway.
If you had mentioned you created it on MSW, that would have been the first reply
Anyway, there is a tool to do this properly http://linux.die.net/man/1/dos2unix and reverse http://linux.die.net/man/1/unix2dos.
NB: on some other Unix systems its unixtodos, dostounix.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Scripts need full path on shebang line; affects urxvt catkin Linux - Software 3 05-02-2011 12:48 PM
[SOLVED] Using python to merge two text files line for line simplified Programming 1 07-06-2010 02:09 PM
Init scripts shebang lincoln.runwal Linux - Newbie 1 03-25-2009 06:22 AM
compiling Python scripts lex1dr Linux - Software 1 06-16-2003 05:24 PM
Linux won't recognize shebang line tjanzer Linux - General 2 03-15-2003 11:33 AM


All times are GMT -5. The time now is 07:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration