running bash script from python file - python

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/')

Related

python gives ImportError: No module named "" when triggered in a perl program

In my perl program which runs the python script
I have provided the PYTHONPATH env param with the path for the lib and i have run the python script. I am getting
ImportError: No module named "....."
my $script = "/path/pythonscript.py";
$ENV{'PYTHONPATH'} = "/path/lib";
system("python $script");
Whereas when i run the same python script on command line in the same directory where the script executes in my perl program, it is working.
Can anyone give me some pointers on why this is happening.
Try printing the contents of sys.path and compare the difference e.g. change your python script to
import sys
print(sys.path)
Most likely there is a difference here and this is causing the module to not be found.
I once had a similar problem. I solved it by creating an executable script (chmod) and making that script run instead of the python script. The script simply contained a cd to the directory and a python3 program. py

Why does Numpy.loadtxt work when called from IDLE and gives an IOError when called from shell?

I try to do the following:
1 VBA script calls shell using
RetVal = Shell(fullpythonexepath & fullscriptpath)
2 Shell get follwing command
fullpythonexepath & fullscriptpath
3 Python Script
import numpy as np
thefilepath = "input.txt" # is in the same folder as the script
theoutputpath = "output.txt" # is in the same folder as the script
ssc=np.loadtxt(thefilepath, delimiter='\t', skiprows=0, usecols=range(2)) #error line
#some other code to fit input data
np.savetxt(theoutputpath, sscnew, header=top, fmt='%1.2f\t%1.2f')
When I run this the output file doesn't get printed, which means the script doesn't run properly.
Now to the funny thing: When I run the python script from IDLE it runs fine. When I run it from the shell using the command:
fullpythonexepath & fullscriptpath
it says :
I tried inputting the fullpath to the input.txt and output.txt. When I do this and run it in the IDLE it does not find the file anymore. When called from the shell it doesn't work either.
Python obviousely does not find the path to the input.txt
the issue is, to my understanding, not related to VBA. The error occures also when using shell commands
Solution was to put :
import os
os.chdir(r"fullpath")
into my script this changes the current working directory to my path and then input.txt gets found.
When you start the script from shell the working directory for the script will be the directory from which it is called upon.
Most IDLE define their own working directory.
To check I suggest doing:
os.getcwd()
in both cases and look what directory is used in both cases

python: how to run a program with a command line call (that takes a user's keystroke as input) from within another program?

I can run one program by typing: python enable_robot.py -e in the command line, but I want to run it from within another program.
In the other program, I imported subprocess and had subprocess.Popen(['enable_robot', 'baxter_tools/scripts/enable_robot.py','-e']), but I get an error message saying something about a callback.
If I comment out this line, the rest of my program works perfectly fine.
Any suggestions on how I could change this line to get my code to work or if I shouldn't be using subprocess at all?
If enable_robot.py requires user input, probably it wasn't meant to run from another python script. you might want to import it as a module: import enable_robot and run the functions you want to use from there.
If you want to stick to the subprocess, you can pass input with communicate:
p = subprocess.Popen(['enable_robot', 'baxter_tools/scripts/enable_robot.py','-e'])
p.communicate(input=b'whatever string\nnext line')
communicate documentation, example.
Your program enable_robot.py should meet the following requirements:
The first line is a path indicating what program is used to interpret
the script. In this case, it is the python path.
Your script should be executable
A very simple example. We have two python scripts: called.py and caller.py
Usage: caller.py will execute called.py using subprocess.Popen()
File /tmp/called.py
#!/usr/bin/python
print("OK")
File /tmp/caller.py
#!/usr/bin/python
import subprocess
proc = subprocess.Popen(['/tmp/called.py'])
Make both executable:
chmod +x /tmp/caller.py
chmod +x /tmp/called.py
caller.py output:
$ /tmp/caller.py
$ OK

How can I run my python script from the terminal in Mac OS X without having to type the full path?

I'm on Mac OS 10.6 Snow Leopard and I'm trying to add a directory to my PATH variable so I can run a tiny script I wrote by just typing: python alarm.py at the terminal prompt.
I put the path in my .profile file and it seems to show up when I echo $PATH, but python still can't find script the that I've put in that directory.
Here's the contents of my .profile file in my home directory:
~ toby$ vim .profile
export PATH=/Users/tobylieven/Documents/my_scripts:$PATH
Here's the output of echo $PATH, where all seems well:
~ toby$ echo $PATH
/Users/tobylieven/Documents/my_scripts:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
Here's the script I'm trying to run:
~ toby$ ls /Users/tobylieven/Documents/my_scripts
-rwxrwxrwx# 1 tobylieven staff 276 17 Jan 21:17 alarm.py
Here's the command I'm trying to use to run the script and the fail message I'm getting instead:
~ toby$ python alarm.py
python: can't open file 'alarm.py': [Errno 2] No such file or directory
If anyone has an idea what I might be doing wrong, that'd be great.
Thanks a lot.
PATH is only for executables, not for python scripts. Add the following to the beginning of your Python script:
#!/usr/bin/env python
and run
sudo chmod a+x /Users/tobylieven/Documents/my_scripts/alarm.py
Then, you can type just alarm.py to execute your program.
Which python are you targeting?
Did you install it with brew? It uses a different path.
which python3 or which python
Choose the one you want
Copy that output
Paste it at the top of your python file
add a #! in front of that path so it looks something like
#!/usr/local/bin/python3
Make sure to change the file permissions
chmod +x filename
Put that file in a folder that is in your path
Not sure if your folder is in your path?
echo $path
How to add that folder to your path?
Find your path first
echo $HOME
If you are using bash or zsh you might have something like this
In ~/.bash_profile or ~/.bashrc or ~/.zshrc at the bottom of your file
export PYTHON_UTILS="$HOME/code/python/utils"
export PATH="$PYTHON_UTILS:$PATH"
Consider removing the .py from your file bc it is not needed in this case
Close and open your terminal, which is sourcing your file by its path
And now you should be able to treat your python file similar to a bash command
You don't need to use python3 filename.py to run the file, you can just use filename
From anywhere on your filesystem!
change alarm.py to include:
#!/bin/python
as the very first line in the file.
(or /usr/bin/python, depending on where you python interpreter is located. You can figure this out by typing: which python in the terminal.)
You can then just run alarm.py instead of python alarm.py.
e.g.:
~ toby$ alarm.py
And phihag who beat me by a few seconds is right, you need to add execute permissions (via chmod) to alarm.py.
You need to modify the Python specific path variable: PYTHONPATH.
So:
export PYTHONPATH=/Users/tobylieven/Documents/my_scripts
should get you working.
See: Python Module Search path
Something of interest that I really struggled with on OS X coming from Window, is that you its very hard to get the directory of your current script.
I found this.
#! /bin/zsh
cd "${0:h}"
Now you could execute a python file relative to the executed script instead of having to know the exact path where your python file is.
This might or might not help but I use this a lot to make my scripts and .command files work better.

Change working directory in shell with a python script

I want to implement a userland command that will take one of its arguments (path) and change the directory to that dir. After the program completion I would like the shell to be in that directory. So I want to implement cd command, but with external program.
Can it be done in a python script or I have to write bash wrapper?
Example:
tdi#bayes:/home/$>python cd.py tdi
tdi#bayes:/home/tdi$>
Others have pointed out that you can't change the working directory of a parent from a child.
But there is a way you can achieve your goal -- if you cd from a shell function, it can change the working dir. Add this to your ~/.bashrc:
go() {
cd "$(python /path/to/cd.py "$1")"
}
Your script should print the path to the directory that you want to change to. For example, this could be your cd.py:
#!/usr/bin/python
import sys, os.path
if sys.argv[1] == 'tdi': print(os.path.expanduser('~/long/tedious/path/to/tdi'))
elif sys.argv[1] == 'xyz': print(os.path.expanduser('~/long/tedious/path/to/xyz'))
Then you can do:
tdi#bayes:/home/$> go tdi
tdi#bayes:/home/tdi$> go tdi
That is not going to be possible.
Your script runs in a sub-shell spawned by the parent shell where the command was issued.
Any cding done in the sub-shell does not affect the parent shell.
cd is exclusively(?) implemented as a shell internal command, because any external program cannot change parent shell's CWD.
As codaddict writes, what happens in your sub-shell does not affect the parent shell. However, if your goal is to present the user with a shell in a different directory, you could always have Python use os.chdir to change the sub-shell's working directory and then launch a new shell from Python. This will not change the working directory of the original shell, but will leave the user with one in a different directory.
As explained by mrdiskodave
in Equivalent of shell 'cd' command to change the working directory?
there is a hack to achieve the desired behavior in pure Python.
I made some modifications to the answer from mrdiskodave to make it work in Python 3:
The pipes.quote() function has moved to shlex.quote().
To mitigate the issue of user input during execution, you can delete any previous user input with the backspace character "\x08".
So my adaption looks like the following:
import fcntl
import shlex
import termios
from pathlib import Path
def change_directory(path: Path):
quoted_path = shlex.quote(str(path))
# Remove up to 32 characters entered by the user.
backspace = "\x08" * 32
cmd = f"{backspace}cd {quoted_path}\n"
for c in cmd:
fcntl.ioctl(1, termios.TIOCSTI, c)
I shall try to show how to set a Bash terminal's working directory to whatever path a Python program wants in a fairly easy way.
Only Bash can set its working directory, so routines are needed for Python and Bash. The Python program has a routine defined as:
fob=open(somefile,"w")
fob.write(dd)
fob.close()
"Somefile" could for convenience be a RAM disk file. Bash "mount" would show tmpfs mounted somewhere like "/run/user/1000", so somefile might be "/run/user/1000/pythonwkdir". "dd" is the full directory path name desired.
The Bash file would look like:
#!/bin/bash
#pysync ---Command ". pysync" will set bash dir to what Python recorded
cd `cat /run/user/1000/pythonwkdr`

Categories

Resources