LinuxQuestions.org
Help answer threads with 0 replies.
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 09-29-2016, 12:49 PM   #1
RandomTroll
Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 477

Rep: Reputation: 40
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
Quote:
#!/usr/bin/python
# -*- python-mode -*-
# -*- coding: UTF-8 -*-
at its beginning. python is in /usr/bin.
 
Old 09-29-2016, 12:54 PM   #2
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 2,184

Rep: Reputation: Disabled
maybe because python is a subset of programming languages like JAVA is it needs its own interpreter to run properly. but that is just off of the cuff.
 
Old 09-29-2016, 01:07 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 7,015

Rep: Reputation: 2535Reputation: 2535Reputation: 2535Reputation: 2535Reputation: 2535Reputation: 2535Reputation: 2535Reputation: 2535Reputation: 2535Reputation: 2535Reputation: 2535
While I don't have an immediate solution for you, what's happening is that the script is being executed as BASH, not as Python.

What happens if you do this?

Code:
/usr/bin/python /usr/bin/solaar
And what happens if you do this?

Code:
which python

Last edited by dugan; 09-29-2016 at 01:09 PM.
 
Old 09-29-2016, 01:55 PM   #4
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 294

Rep: Reputation: 62
Try:
#!/usr/bin/env python
 
1 members found this post helpful.
Old 09-30-2016, 05:06 AM   #5
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 206

Rep: Reputation: 82
Hi,

c0wb0y is right.
Code:
#!/usr/bin/env python
will certainly work.

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.
 
Old 10-02-2016, 04:57 PM   #6
RandomTroll
Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 477

Original Poster
Rep: Reputation: 40
'which python' returns /usr/bin/python

/usr/bin/python /usr/bin/solaar returns the same as python /usr/bin/solaar

#!/usr/bin/env python doesn't work.
 
Old 10-02-2016, 08:23 PM   #7
AwesomeMachine
Senior Member
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora
Posts: 1,841

Rep: Reputation: 260Reputation: 260Reputation: 260
Usually
Code:
$ py command
works.
 
Old 10-03-2016, 01:51 AM   #8
ondoho
Senior Member
 
Registered: Dec 2013
Posts: 4,176

Rep: Reputation: 837Reputation: 837Reputation: 837Reputation: 837Reputation: 837Reputation: 837Reputation: 837
Quote:
Originally Posted by RandomTroll View Post
#!/usr/bin/env python doesn't work.
that's not a command, that's something you have to put at the top of your python script.

anyhow, you've been here long enough to know that "doesn't work" isn't an acceptable explanation.
 
Old 10-03-2016, 07:00 AM   #9
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 2,184

Rep: Reputation: Disabled
Python Basic Syntax
 
Old 10-03-2016, 07:04 AM   #10
hazel
Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: Debian, Crux, LFS, AntiX, NuTyX
Posts: 529

Rep: Reputation: 230Reputation: 230Reputation: 230
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.
 
Old 10-03-2016, 07:34 AM   #11
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 2,184

Rep: Reputation: Disabled
Quote:
Originally Posted by hazel View Post
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?
 
Old 10-03-2016, 08:02 AM   #12
hazel
Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: Debian, Crux, LFS, AntiX, NuTyX
Posts: 529

Rep: Reputation: 230Reputation: 230Reputation: 230
Quote:
Originally Posted by BW-userx View Post
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.
 
Old 10-03-2016, 08:18 AM   #13
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 2,184

Rep: Reputation: Disabled
Quote:
Originally Posted by hazel View Post
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 ...

Last edited by BW-userx; 10-03-2016 at 08:24 AM.
 
Old 10-03-2016, 11:21 AM   #14
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,260

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
Quote:
Originally Posted by BW-userx View Post
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
Code:
file /usr/bin/solaar
 
1 members found this post helpful.
Old 10-03-2016, 02:02 PM   #15
RandomTroll
Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 477

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by ondoho View Post
that's not a command, that's something you have to put at the top of your python script.
Of course I replaced the first line of /usr/bin/solaar with the #!/usr/bin/env solaar - I've been here long enough to know that.

'Doesn't work' means that it behaves exactly as it did before.
 
  


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
python PyQt4 scripts won't run iFunction Programming 3 04-06-2016 06:24 AM
Script to run all the python scripts from particular directory mandar.nandale Linux - General 4 05-08-2012 02:29 AM
Is it possible to use python scripts to run a dzen2 status bar? daweefolk Linux - General 0 02-08-2011 04:38 PM
How to run shell scripts wrapped in Python. narnie Programming 20 07-15-2010 01:41 AM
How do I make python programs run without entering the command python? trist007 Programming 5 03-22-2009 09:21 PM


All times are GMT -5. The time now is 01:17 PM.

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