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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-08-2012, 08:24 PM
|
#1
|
LQ Newbie
Registered: Jul 2012
Location: near Philadelphia, PA, USA
Distribution: Ubuntu 12.04 LTE 32-bit on all platforms
Posts: 6
Rep: 
|
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.
|
|
|
07-08-2012, 08:39 PM
|
#2
|
Moderator
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
|
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 may work it is better to use
Code:
#!/usr/bin/env python
|
|
|
07-08-2012, 09:21 PM
|
#3
|
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: 
|
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 ?!
|
|
|
07-08-2012, 09:32 PM
|
#4
|
Moderator
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
|
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 ~ :) %
|
|
|
07-08-2012, 11:56 PM
|
#5
|
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: 
|
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.
|
|
|
07-09-2012, 12:34 AM
|
#6
|
Moderator
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
|
I am not using Windows for coding and I assume many people here don't also. So something like that can be easily forgotten.
|
|
|
07-09-2012, 08:11 PM
|
#7
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,425
|
@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.
|
|
|
All times are GMT -5. The time now is 10:54 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|