Access Denied when launching a Shell file

Post your Gambas programming questions here.
Post Reply
User avatar
sarpomira
Posts: 20
Joined: Monday 28th December 2020 4:15pm

Access Denied when launching a Shell file

Post by sarpomira »

Hi All,

When the "Launch" button is pressed, this code would launch an external Python script.
It worked just fine for some time. Now whenever I try to get Gambas3 to run an external Python file (any file),
I get the error message shown below referring to : Permission denied.

Does anyone have an idea why this happens and what may be the solution?

public sub cmd_Launch()

  'Path to the external Python Script  
  'Specify the path to run the Python script
  Dim FileAppPath As String
  FileAppPath = "/home/veggie/Desktop/CallOptionCalc/python3/DoAnalysis.py"
  
  'Launch the external application
   Shell FileAppPath

End


ERROR MESSAGE in the debug window....
/bin/sh: 1: /home/veggie/Desktop/CallOptionCalc/python3/DoAnalysis: Permission denied
User avatar
thatbruce
Posts: 168
Joined: Saturday 4th September 2021 11:29pm

Re: Access Denied when launching a Shell file

Post by thatbruce »

What happens if you run that exact string in a terminal?
Have you ever noticed that software is never advertised using the adjective "spreadable".
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Access Denied when launching a Shell file

Post by BruceSteers »

sarpomira wrote: Thursday 28th December 2023 2:32am Hi All,

When the "Launch" button is pressed, this code would launch an external Python script.
It worked just fine for some time. Now whenever I try to get Gambas3 to run an external Python file (any file),
I get the error message shown below referring to : Permission denied.

Does anyone have an idea why this happens and what may be the solution?

public sub cmd_Launch()

  'Path to the external Python Script  
  'Specify the path to run the Python script
  Dim FileAppPath As String
  FileAppPath = "/home/veggie/Desktop/CallOptionCalc/python3/DoAnalysis.py"
  
  'Launch the external application
   Shell FileAppPath

End


ERROR MESSAGE in the debug window....
/bin/sh: 1: /home/veggie/Desktop/CallOptionCalc/python3/DoAnalysis: Permission denied
is the files executable flag set?
If not you'll have to use
Shell "python " & FileAppPath
or Exec ["python", FileAppPath]

or make it executable for it's shebang line to work as expected.
chmod +x /home/veggie/Desktop/CallOptionCalc/python3/DoAnalysis.py
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Access Denied when launching a Shell file

Post by BruceSteers »

PS. is your Shell using bash or sh? by default it uses sh but it can be better with bash

Change the Shell your app uses to bash like this..
System.Shell = System.Find("bash")



but often when an archive is unpacked somewhere all the +x flags get removed from scripts. (it's probably just that) using the python command to launch them or making the scripts executable should fix it for you.


' check if file is executable and set x flag if not...
If not InStr(Stat(FileAppPath).Perm.User, "x") Then Exec ["chmod", "+x", FileAppPath] Wait

' Now try it
Shell FileAppPath
If at first you don't succeed , try doing something differently.
BruceS
User avatar
sarpomira
Posts: 20
Joined: Monday 28th December 2020 4:15pm

Re: Access Denied when launching a Shell file

Post by sarpomira »

I simplified the troubleshooting as much as possible and added two test lines as suggested by Bruce.
I created a "Hello World" script called Hello.py and placed it on the desktop.( It runs fine in the linux shell)
When I call the file from Gambas using "Shell" I get the following error.

/home/veggie/Desktop/Hello.py: 1: Syntax error: word unexpected (expecting ")")
I don't know if this is a Gambas error or an OS error.?

Public Sub cmdRun_Click()

   'Path to the external Python Script  
   Dim FileAppPath As String
    
  'Specify the command to run the Python script
   FileAppPath = "/home/veggie/Desktop/Hello.py"
  
  'check if file is executable and set x flag if not...
  'if Not InStr(Stat(FileAppPath).Perm.User, "x") Then Exec ["chmod", "+x", FileAppPath] Wait

  'Launch the external application
   Shell FileAppPath
   
  'Shell "python" & FileAppPath

End


PS: I ran the program with the various "chmod" and Shell lines shown above but the result is still "access denied" or expecting ")"[/b]
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Access Denied when launching a Shell file

Post by BruceSteers »

I'm only getting access denied if the executable flag is not set.
I just made a quick helloworld.py in $HOME

#!/usr/bin/env python
print ("hello world")



Terminal went like this..

Code: Select all

bonus:~$ ~/helloworld.py
bash: /home/bonus/helloworld.py: Permission denied
bonus:~$ chmod +x ~/helloworld.py
bonus:~$ ~/helloworld.py
hello world
And running Shell "~/helloworld.py" in gambas printed "hello world" to console as expected.

Does the file in question have a shebang line as it's first line?
#!/usr/bin/env python

If it is something normally launched by something else the something else may know it is a python script and a shebang line was never added.
if not it will need to be run explicitly with python.

Ps. your commented Shell command
'Shell "python" & FileAppPath

Should be
'Shell "python " & FileAppPath
(a space after python)

You could maybe do some manual handling..

 Select LCase(File.Ext(FileAppPath))
   Case "py"
     Exec ["python", FileAppPath] Wait
   Case "gbs"
     Exec ["gbs3", FileAppPath] Wait
   Case Else
     Exec [FileAppPath] Wait
 End Select

 If Process.LastValue Then
   Message.Error(FileAppPath & "\nReturned error code " & Process.LastValue)
 Endif


Something like that ?
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Access Denied when launching a Shell file

Post by BruceSteers »

Did your python hello world use parentheses?

that'd be a "you" error ;)

print "hello world"
that's wrong with python

print ("hello world")
that's correct.
If at first you don't succeed , try doing something differently.
BruceS
Post Reply