LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 11:49 AM   #1
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,953

Rep: Reputation: 270Reputation: 270Reputation: 270
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, 11:54 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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, 12:07 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
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 12:09 PM.
 
Old 09-29-2016, 12:55 PM   #4
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 417

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

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
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, 03:57 PM   #6
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,953

Original Poster
Rep: Reputation: 270Reputation: 270Reputation: 270
'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, 07:23 PM   #7
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Usually
Code:
$ py command
works.
 
Old 10-03-2016, 12:51 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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, 06:00 AM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Python Basic Syntax
 
Old 10-03-2016, 06:04 AM   #10
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,563
Blog Entries: 19

Rep: Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445
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, 06:34 AM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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, 07:02 AM   #12
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,563
Blog Entries: 19

Rep: Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445Reputation: 4445
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, 07:18 AM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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 07:24 AM.
 
Old 10-03-2016, 10:21 AM   #14
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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, 01:02 PM   #15
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,953

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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:08 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
Open Source Consulting | Domain Registration