How to get the shebang line to work in python scripts ?
Linux - NewbieThis 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.
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.
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 ?
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
./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.
@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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.