Run a .sh file with Python 3 Idle - python

I am trying run a sh file with python 3. My .sh file will work on Terminal. My operating system is Raspbian. I try that code :
import time
import os
import subprocess
# STEP 1
text='sudo somecode'
savefile=open('step1.sh','w')
savefile.write(text)
savefile.close()
time.sleep(2)
shellscript=subprocess.Popen(['./step1.sh'], stdin=subprocess.PIPE)
but it not works...

The is undoubtedly a permissions issue. In order to be able to "directly" execute a file (a la "dot slash" - ./yourfile), the file in question needs the "execute bit" set. Try using ls -l to see the file you've just created with your script. I'll bet the file does not have the execute bit:
$ ls -l ./step.sh
-rw-r--r-- 1 furkan furkan 0 Nov 13 20:51 step.sh
Note the lack of x in that first column. You might chmod to add the execute bit:
$ chmod +x ./step.sh
$ ls -l ./step.sh
-rwxr-xr-x 1 furkan furkan 0 Nov 13 20:51 step.sh
With the execute bit set, you could then use a "dot slash" construct.
However, I doubt you want to execute chmod from within your script, so instead tell your script the actual program you want to run step.sh, namely, sh:
shellscript=subprocess.Popen(['sh', './step1.sh'], stdin=subprocess.PIPE)
Or in the simple case of your example, simply go directly to sudo:
shellscript=subprocess.Popen(['sudo', 'yourexecutable'], stdin=subprocess.PIPE)
Note that if you're being robust, I might consider adding some absolute paths, or ensuring your PATH variable is set. However the key to your problem is the misunderstanding of what 'executable' means.

Related

script.py in path but can't be executed without providing full path

I have a script I have downloaded and installed from GitHub that I am trying to add to my path. Although the script is in the path already somehow I can't run it unless I provide the full path.
echo $PATH
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Users/me/bin/SCRIPT_FOLDER/
/Library/TeX/texbin
/Users/me/bin/SCRIPT_FOLDER/script.py
Since I am working on Catalina 10.15.7, I saw in other answers that I need to add the script to the .zshrc file, so I did so.
code .zshrc
export PATH=/Users/me/bin/SCRIPT_FOLDER/:$PATH
export PATH=$PATH:/Users/me/bin/SCRIPT_FOLDER/script.py
Despite this the script cannot be run except if I type the entire path
python /Users/me/bin/SCRIPT_FOLDER/script.py --h
What can I try to solve this?
script.py must be executable and the first line must be as shown below.
? echo $PATH
.:./bin:...
? ls
bin
? ls -l bin/script.py
-rwxr-xr-x. 1 x x 40 Mar 9 10:54 bin/script.py
? cat bin/script.py
#!/usr/bin/python
print('Hello world')
? script.py
Hello world
?
If the script is not executable use the chmod command like so:
chmod 755 bin/script.py
Like most command-line programs, Python doesn't consult your PATH for the arguments (e.g., script.py, given the command python script.py). Your PATH only matters when deciding how to resolve the command name (in this case, python). If script.py is in one of your PATH directories (like /Users/me/bin/SCRIPT_FOLDER/), it has the appropriate executable bit set, and it begins with an appropriate shebang line such as #!/usr/bin/env python, then you can run it with the command script.py.

Starting python script on Google Cloud Compute instance startup

I've been trying to find the best way to start a python script on startup of my cloud compute instance. So far, I haven't gotten it to run. The script does run when manually executed on the instance. I did make the file executable.
This is what I have tried so far:
1)
Add script directly to metadata with key "startup-script". Script starts with:
#! /usr/bin/python3
Followed by the script contents.
Result:
Won't run, doesn't show up in log.
2)
Try to execute local script from metadata with key "startup-script":
#! /usr/bin/bash"
/home/dir/scripts/script.py.
Result:
Won't run, doesn't show up in log.
3)
Point to file located in storage bucket with "startup-script-url".
gs://project.appspot.com/folder/script.py
Result:
Won't run, but shows "Found startup script" in log.
I hope anyone has some insights.
This worked for me:
#! /bin/bash
cat <<EOF > /var/myScriptStackOverflow.py
with open("/var/python_was_here_stack_overflow.txt", "w") as file:
file.write("Looks that the script is executed")
EOF
python3 /var/myScriptStackOverflow.py
The script above is explicit in respect to the paths, but this also works:
#! /usr/bin/python3
with open("/var/python_was_here_stack_overflow.txt", "w") as file:
file.write("Looks that the script is executed this way as well...")
Edit the instance, paste the script above in the Custom metadata with the key startup-script:
Reset the instance
ssh inside the instance to check the results:
ls -la /var | grep -i py
-rw-r--r-- 1 root root 119 Aug 3 17:33 myScriptStackOverflow.py
-rw-r--r-- 1 root root 33 Aug 3 17:33 python_was_here_stack_overflow.txt
cat /var/myScriptStackOverflow.py
with open("/var/python_was_here_stack_overflow.txt", "w") as file:
file.write("Looks that the script is executed")
cat /var/python_was_here_stack_overflow.txt
Looks that the script is executed

Python script with executable permission says "Command Not Found"

I have a python script abc.py. Inside a shell script file, I call it ./abc.py
Inside abc.py, at the top, I have #!/usr/bin/python
test -x abc.py && echo true || echo false return true
On executing the shell script, it says ./abc.py: Command not found.
It works if I call it as python ./abc.py instead of just ./abc.py
The issue is only with this abc.py file that I created today. The older python scripts in the same directory with different purpose and names work without any issue.
I have referred the correct path of the file BTW. What could be the issue?
In tcsh, this happens when the interpreter is invalid:
$ cat foo
#!/invalid
$ tcsh -c './foo'
./foo: Command not found.
$ bash -c './foo'
bash: ./foo: /invalid: bad interpreter: No such file or directory
This could be for several reasons. Perhaps your path is wrong for your system:
% type python
python is /usr/local/bin/python
in this case the script needs to start with #!/usr/local/bin/python instead.
Alternatively, the script could have Windows line endings (carriage returns):
$ cat -v foo
#!/usr/bin/python^M
...^M
In this case, save it with Unix line endings instead.
Trying to replicate your setup as follows:
File: asdf.py
#!/usr/bin/python
print("Hello World")
File: asdf.sh
#!/bin/bash
if [ -x asdf.py ]; then ./asdf.py ; else echo "Arrrrgh. File does not exist or does not have executable permisions"; fi
Now I save the files in the same directory, e.g. the Desktop, make them executable and run the shell script from the terminal.
usr#cmptr $ chmod +x asdf.{py,sh}
usr#cmptr $ ls -la asdf.{py,sh}
-rwxr-xr-x 1 usr usr 613 Mar 15 22:22 asdf.py
-rwxr-xr-x 1 usr usr 56 Mar 15 22:14 asdf.sh
usr#cmptr $ ./asdf.sh
Hello World
Now, maybe I have misunderstood your setup but this should work just fine.

How to correctly execute a python script on the server?

I'm trying to execute my python script on the server. I use a hosting-company that allows me to do cronjobs. when i try to make the server execute the file i always get this error:
command /homez.208/thomasdewh/www/denshi/denshi.py must be executable
-- 2014-10-13 10:28:02.687788 exitcode: 255
So i found out that i have to use a sheban to tell the server this file can be executed.
So i added:
#!/www/python/python
import zipfile
import os.path
import os
import sys
...rest of script...
I uploaded this to the file and added all the files that are in C:\Users\my-computer\python27\ to the folder /www/python/python on my webserver via FTP.
But this still dont seem to work.. anybody has an idea what i'm doing wrong?
On a unix system, in order to make a script able to run when invoked, there are 2 things to consider:
the shebang #!... at the top of the file, this just tells the shell it is executed from, which interpreter to be used during the execution
ensure that the file has the executable permission for whoever needs to run it: if is is the current user, then chmod u+x , if it has to be for any member of a given group: chmod g+x , if anobody: chmod a+x
When you do: ls -l, you file must probably be like this:
-rw-r--r-- 1 apero staff 0 Oct 14 00:04 script.py
If you set u+x (what you must probably do in your case) it becomes:
-rwxr--r-- 1 apero staff 0 Oct 14 00:04 script.py
if you set g+x it becomes:
-rw-r-xr-- 1 apero staff 0 Oct 14 00:04 script.py
if you set a+x:
-rwxr-xr-x 1 apero staff 0 Oct 14 00:04 script.py
The same can be done for write permissions (w instead of x), and to remove some permission, do the same but with - instead of +.
However, try to never use "a" or "0" as it grants everybody permissions to the file (in read, or write or execute). Btw, whoever can write, can delete, so beware.

Altering my python path: helloworld.py returns command not found—

Massive apologies for this embarrassing question—
I'm using my MacBook Pro, running snow leopard, and using Python 2.7.1. Trying to run my first script and all the first pages of all my tutorials are laughing at me:
Let me preface with:
$ whereis python
/usr/bin/python
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
(Is this my issue?)
I wrote helloworld.py to /users/charles in vim:
$ vim helloworld.py
#!/usr/bin/python
# Hello World Python Program
print "Hello World!";
When trying to run it from terminal:
$ helloworld.py
-bash: helloworld.py: command not found
When trying to run it from python:
$ python
>>> helloworld.py
Traceback (most recent call last):
File :<stdin>", line 1, in <module>
NameError: name 'helloworld' is not defined
From Dive Into Python (not sure if this is pertinent):
$ python
>>> import sys,os
>>> print 'sys.argv[0] =',sys.argv[0]
sys.argv[0]=
>>> pathname=os.path.dirname(sys.argv[0])
>>> print 'path=',pathname
path=
>>> print 'full path=',os.path.abspath(pathname)
full path= /Users/charles
I'm befuddled! Do I need to alter one of my paths so it finds my script?
I'm absolutely new to programming, I actually just found out that terminal was something you could use.
Thanks!
Let's start with the first error you received. Understanding error messages is important.
-bash: helloworld.py: command not found
This indicates that helloworld.py is not a command that can be executed. To run the file, you then have two options:
Run it using the python interpreter. python helloworld.py
Make the file executable and then run it directly. ./helloworld.py
To make files executable in a *nix environment, you have to change their mode to allow execution. In order to do this, you use the chmod command (man chmod for more info).
chmod +x helloworld.py
This assumes that you are in the directory containing the helloworld.py file. If not, cd there first or use the full path.
The ./ is necessary because it tells the shell to run the file located here, not by looking in $PATH. $PATH is a list of possible executable locations. When you try to run helloworld.py directly, the shell tries to look for it in $PATH. You want to run the local file, so you have to prefix it with ./, which means "from here".
As an aside, note the first line of your python script:
#!/usr/bin/python
This is called a shebang line and tells system to use the /usr/bin/python executable to load the file. Internally, that means that the program loader will be doing /user/bin/python helloworld.py.
Finally, when you called python with no arguments, you were dropped into an interactive Python interpreter session. >>> helloworld.py in this environment is not referencing the file of that name, it's just interpreted as python code. Invalid python code. This is why you get your second error, NameError: name 'helloworld' is not defined.
To turn a Python module or script into a standalone program on a UNIX system you have to do two things:
1.) Make sure you have the "shebang" in the top of your script:
#!/usr/bin/python
2.) Make sure the script file is executable. This is done using the chmod command:
chmod +x /path/to/helloworld.py
/path/to/ being the fully qualified file path to your script. If it's in the current directory, then you can omit the path.
% ls -l
total 0
drwxr-xr-x 2 jathan jathan 60 2011-04-13 15:28 ./
drwxrwxrwt 12 root root 6.5K 2011-04-13 15:28 ../
-rw-r--r-- 1 jathan jathan 0 2011-04-13 15:28 helloworld.py
It's in my current directory, so let's make it executable!
% chmod +x helloworld.py
% ls -l
drwxr-xr-x 2 jathan jathan 60 2011-04-13 15:28 ./
drwxrwxrwt 12 root root 6.5K 2011-04-13 15:28 ../
-rwxr-xr-x 1 jathan jathan 0 2011-04-13 15:28 helloworld.py*
See the "x"s in the permission bits on the left? You've done it! Now we can run it:
% ./helloworld.py
Hello World!
Lastly, never use semicolons as line-endings in Python. It's not required and it's ugly!
Wanted to add my 2 cents: Apart from permissions and path answers above, there is one more situation where you may still face the same error.
In-spite of correct permissions and the shebang header, you may still get the same "Command not found" error if you've originally written the file in Windows and copied it over to Linux. Due to differing line-ending characters, there will be extra '\r' characters on the lines.
This happens because there are non-printable characters in the file. Examing it by doing:
cat -v <filename>:
#!/usr/intel/bin/python^M
The extra "^M" is the problem. Use 'dos2unix' to convert the file and then it'll run fine.
as others said you should chmod +x your file to make it executable and if you don't want to put "./" in your coomand line you should add your current place as system path:
export PATH=$PATH:.
If you're already within python, the syntax to load your script is not helloworld.py :
import helloworld
or
from helloworld import *
you only use the extension .py when you're running python with a script as a command line argument.
No need to apologize, have to start somewhere, and the error messages can be cryptic when you're having basic syntax problems.
Make sure your terminal's current working directory is where your .py file is.
EDITED:
try doing /usr/bin/python helloworld.py on commmand line

Categories

Resources