I'm trying to run sh-script from python file
my_script.sh
#!/usr/bin/python
rm category.xml
python file
import subprocess
subprocess.call(["../my_script.sh"])
And I get
File "../my_scrypt.sh", line 3
rm category.xml
^
SyntaxError: invalid syntax
How to fix this?
You used a shebang line of #!/usr/bin/python on a file that isn't Python. Change the shebang line.
Better yet, don't call out to shell scripts when you can call Python functions to do the same thing:
import os
os.remove("category.xml")
Look at your shell code. You using python interpreter #!/usr/bin/python and feeding it with bash commands rm category.xml.
Fixed shell script:
#!/bin/bash
rm category.xml
if you're using python 2x
use commands module:
import commands
print commands.getoutput('sh my_script.sh')
if using python 3x
use subprocess module:
import subprocess
print(subprocess.getoutput('sh my_script.sh'))
Try this,
my_script.sh
#!/usr/bin/sh
rm category.xml
Trivial approach:
>>> import subprocess
>>> subprocess.call(['./my_script.sh'])
>>>
Related
So example I have got an effects.py file and to run on the command prompt, I am doing
gimp-console-2.10.exe -idf --batch-interpreter python-fu-eval -b "import effects; effects.data()" and this works.
However I don't really want to run it in this way. Is there a way I could run it like python effects.py on the command prompt instead?
#!/usr/bin/env python
from gimpfu import *
def data():
pass
If you want to avoid the import, you can install your python code as a regular plugin, and then you should be able to skip the import and call your plugin directly:
gimp-console-2.10.exe -idf --batch-interpreter python-fu-eval -b "pdb.python_fu_your_registration_atom(...)"
(not tested)
I have a bash script which changes the path on my command line,
This one,
#!/usr/bin/env python
cd /mnt/vvc/username/deployment/
I have a python script which i wish to run after the path changes to the desired path,
The script,
#!/usr/bin/env python
import subprocess
import os
subprocess.call(['/home/username/new_file.sh'])
for folder in os.listdir(''):
print ('deploy_predict'+' '+folder)
I get this
File "/home/username/new_file.sh", line 2
cd /mnt/vvc/username/deployment/
^
SyntaxError: invalid syntax
Any suggestions on how can i fix this?thanks in advance
You need to explicitly tell subprocess which shell to run the sh file with. Probably one of the following:
subprocess.call(['sh', '/home/username/new_file.sh'])
subprocess.call(['bash', '/home/username/new_file.sh'])
However, this will not change the python program's working directory as the command is run in a separate context.
You want to do this to change the python program's working directory as it runs:
os.chdir('/mnt/vvc/username/deployment/')
But that's not really great practice. Probably better to just pass the path into os.listdir, and not change working directories:
os.listdir('/mnt/vvc/username/deployment/')
I'm trying to call a python script from a bash script. I get import errors only if I try to run the .py from the bash script. If I run with python myscript.py everything is fine. This is my bash script:
while true; do
python script.py
echo "Restarting...";
sleep 3;
done
The error I get:
Traceback (most recent call last):
File "script.py", line 39, in <module>
from pokemongo_bot import logger
File "/Users/Paolo/Downloads/folder/t/__init__.py", line 4, in <module>
import googlemaps
ImportError: No module named googlemaps
There is more to this story that isn't in your question.
Your PYTHONPATH variable is getting confused somewhere along the way.
Insert a couple quick test lines:
in bash:
echo $PYTHONPATH
in your python:
import os
print os.environ["PYTHONPATH"]
At some point, the path to googlemaps got lost.
Your problem is in the script itself, your bash code is OK!. If you don't have problem running python scrip.py from bash directly, you should test if you use the same interpreter for both calls. You can check the shebang line in the python script, it is the first line in the file for example #!/usr/bin/env python or #!/usr/bin/python and compare it to the output of which python command if the output is different try to change or add the shebang line in to the file. If you call directly file in bash ./some_script.py bash reads the first line and if it is shebang line he wil execute the specific command for the file. My point is that if you use two diferent interpreters for calling file directly with python script.py and indirectly ./script.py one of them may not have the proper python modules.
Howto code:
$ which python
/usr/local/bin/python
So the second line is the path for your interpreter to build a shebang from it write in the first line of your script file this.
#!/usr/local/bin/python
So here's an example of the terminal line I'm trying to run after importing the OS into a Python Script of mine:
user$ echo variable | thecommand
Even though OS imports have been working for me lately, the fact that the variable is in the MIDDLE of the imported OS command is not allowing my code to run:
#! /bin/python
import os
variable = 'thevariable'
os.system ("echo "+variable +" | thecommand")
the above is what I have tried in a few different syntax's with no success. Is there a way to accomplish what I'm looking to do using the os.system method?
Don't use os.system(). it is deprecated.
Instead try
import subprocess
variable = 'thevariable'
subprocess.call("echo "+variable +" | thecommand", shell=True)
the shell=True means that the command will be run in a bash process so that echo and the pipe would work.
Often when I am using the IDLE shell I import the pickle module. Is it possible to make it automatically import pickle when I start it?
You can use the -c or -r argument:
From idle -h:
-c cmd run the command in a shell, or
-r file run script from file
For example:
idle -c 'import pickle, sys'
Or:
idle -r ~/my_startup.py
Where my_startup.py might contain:
import pickle, sys
You can either create a shell alias to always use this, or create a separate script; the procedure for this differs depending on your OS and shell.