Why don't python scripts run unless I run them with explicit invocations of python?
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.
Why don't python scripts run unless I run them with explicit invocations of python?
When I try to run solaar 'bare' it returns
Quote:
/usr/bin/solaar: line 21: from: command not found
/usr/bin/solaar: line 24: syntax error near unexpected token `('
/usr/bin/solaar: line 24: `def init_paths():'
If I run it as 'python /usr/bin/solaar' it works. solaar has
Actually the first line is telling Bash which interpreter to use in order to understand the commands inside the script. Sometimes Bash cannot find python at that location, probably because the python libraries/packages are found under some specific python${VERSION} directory or if there are two versions of python.
However the /usr/bin/env python ensures that python is found.
I started a thread on hashbangs about a month ago in the programming forum, asking how they work internally, which led eventually to this Wikipedia article. It seems that it's the exec function called by the shell (or by any other program that forks a script) which makes the decision on what program to launch to interpret the script. But that still doesn't explain why python wasn't found.
Interestingly, the article recommends using "#! /usr/bin/env python" as a hashbang for python scripts because python can be installed in a variety of locations, but that doesn't seem relevant to your problem because you have given an absolute path which is known to be correct.
I started a thread on hashbangs about a month ago in the programming forum, asking how they work internally, which led eventually to this Wikipedia article. It seems that it's the exec function called by the shell (or by any other program that forks a script) which makes the decision on what program to launch to interpret the script. But that still doesn't explain why python wasn't found.
Interestingly, the article recommends using "#! /usr/bin/env python" as a hashbang for python scripts because python can be installed in a variety of locations, but that doesn't seem relevant to your problem because you have given an absolute path which is known to be correct.
I donno, did he check his PATH ??? is it within it?
Yes, he says he checked it with which. which only works within your path.
yeah I know it only works if it is in your path, I was being lazy and didn't want to have to read every sticnking post in here. just throwing out a bones. now its going to make me really have to think about it and I do not even program with python.
in his error output it states this
Code:
/usr/bin/solaar: line 21: from: command not found
/usr/bin/solaar: line 24: syntax error near unexpected token `('
/usr/bin/solaar: line 24: `def init_paths():'
should he not then be looking at that unexpected token that seems to have caused him problems instead of what to write to get python to work?
what is Line 21 what does that line have on it on/in his code page?
I think the need to actually see that code on lines 21 and 24 are in order if as he states it works running it a different way.
or which ever way it is being done, them lines where the error occurs are what one needs to be looking at to see what that code is trying to do in which ever file is throwing them errors ...
yeah I know it only works if it is in your path, I was being lazy and didn't want to have to read every sticnking post in here. just throwing out a bones. now its going to make me really have to think about it and I do not even program with python.
in his error output it states this
Code:
/usr/bin/solaar: line 21: from: command not found
/usr/bin/solaar: line 24: syntax error near unexpected token `('
/usr/bin/solaar: line 24: `def init_paths():'
should he not then be looking at that unexpected token that seems to have caused him problems instead of what to write to get python to work?
what is Line 21 what does that line have on it on/in his code page?
I think the need to actually see that code on lines 21 and 24 are in order if as he states it works running it a different way.
or which ever way it is being done, them lines where the error occurs are what one needs to be looking at to see what that code is trying to do in which ever file is throwing them errors ...
Those errors are being thrown because the system is trying to execute the script using bash, not python.
Is
Code:
#!/usr/bin/python
the very first line in the file? Nothing above it? No spaces or other characters in there? Was this script written from scratch in a Linux text editor, or did you perhaps copy/paste it from somewhere else or write part of it in Windows? What is the output of
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.