Run arbitrary Python after 'py' command - python

Suppose I have the following Fowershell function:
function test {
py
print('hello world')
}
when run, it opens Python in Powershell, but it doesn't execute the code following the 'py' command. how can I make it do that WITHOUT creating a file?
Edit: after quitting python, it outputs Unable to initialize device PRN. I think it's executing print() after py closes

You can use Python's -c command line flag:
python -c "print('hello world');print('Second line')"
>> hello world
>> Second line

Related

How to send keys to a python pdb shell from bash script?

I am trying to create a bash script that runs a python3 script with pdb trace set. As such, in my bash script I have the following lines:
python3 path/to/my_script.py
n
What I want to happen is for bash to run the python script, which will then open the python debugger. Then, the bash script will send the key 'n' to the pdb shell so that pdb executes the first line in the python script.
The script does not work as expected and bash waits until the python script has completed (or exited) to execute 'n' in the command line which just opens node.
I thought this might be a problem unique to pdb shells so I tried executing the following in bash:
python3
print("Hello")
However, again, we observe that the script creates a python3 shell and then waits for the shell to exit before executing print("Hello") in the terminal. I understand that I could use python3 -c for this case, but that does not address the case of passing commands to the pdb shell in the context of the running script.
Is there any way to send the 'n' command to the pdb shell that the python script generates?
Your code will try to run two commands. First, it will run your python script, then it will try to run a command called n. Assuming your script needs you read from stdin you can do one of the following:
Use a herestring:
python3 path/to/my_script.py <<< "n"
Use a pipeline:
One example echo "n" | python3 path/to/my_script.py
Echo is not the only command you can use. You can also use printf or even yes for this use case.
You can use a coprocess to send and receive from pdb.
#! /bin/bash
send() {
echo "$1" >&${PDB[1]}
}
recv() {
IFS= read -r -u ${PDB[0]} line
echo $line
}
coproc PDB { /path/to/my_script.py; }
send 'n'
recv
#...
send 'n'
recv

python subprocess.call() doesn't work with multiline shell commands

I would like to run this multiline shell commands:
echo 'a=?'
read a
echo "a=$a"
from a python script, using the subprocess.call() method.
I wrote this, in test.py file:
import shlex, subprocess
args = ["echo", 'a=?',"read", "a", "echo", "a=$a"]
subprocess.call(args)
and when I execute it, I have in terminal this report:
Armonicus#MyMacs-iMac MyNewFolder % python test.py
a=? read a echo a=$a
which is not at least close to what I expect.
Can I have some support from anyone, please?
There are a couple of issues with your approach here.
First, if what you're trying to do is prompt the user for input from the command line, then you can use Python builtins instead of a subprocess:
a = input('a=?')
print(a)
If you do want to call a subprocess with multiple commands, you need to either make separate calls for each command, or invoke a shell and execute the commands within it. For example:
subprocess.call("echo 'a=?'; read a; echo $a", shell=True)

Unable to open my source code a second time with `python -i`

When I first start bash I can open my code like so:
$ python -i index.py
That file is open, but when I try again this happens:
>>> python -i index.py
File "<stdin>", line 1
python -i index.py
^
SyntaxError: invalid syntax
If I close bash and start again it works. What am I doing wrong?
You can't run terminal commands from the Python REPL.
You can tell you're in the REPL when you see >>> as opposed to $. This means you can run Python code there, but not shell/terminal commands (like the python command).
To exit the REPL, use Ctrl + Z or type exit() and press enter. This will bring you back to the regular terminal.
In addition, I'd recommend running just python index.py rather than python -i index.py in most cases.
The added -i means that you'd like to stay in the REPL to inspect the results after running the index.py file. It allows you to continue running additional Python code after the index.py file has finished its execution.
It looks by the three >>> that you are in the python console not in bash itself. If you type exit() you should get back to bash, and then you can try the code again.

Read/write same variable in batch and python scripts

I have 2 scripts. One is .bat and other is python. The python script is triggered by the .bat file. While executing, first I will run .bat with command line arguments but then I need to read the argument into the python script.
What am I doing wrong?
I call the batch script like this:
C:>main.bat c:\temp\text1.txt
main.bat :
#ECHO off
set var1=%~1
call python_script.bat
echo "returned to main"
pause
python_script.bat :
python -x %0 %*
print var1 # Notworking
import sys
var1 = sys.argv ############ Also not working
with open(var1, 'r+') as f:
content = f.read()
f.seek(0)
f.trunca........
I don't know much about bat and windows but doesn't windows support calling a python script with a command line argument from a bat file? If so, wouldn't something like this work?
This works in Linux shell:
call_py.sh:
# do stuff with command line argument 1 here ($1) and pass it on to py
echo "I'm a shell script and i received this cmd line arg: " $1
# pass $1 on to python as a cmd line arg here
python some_script.py $1
echo "shell script still running after python script finished"
The other question I linked to showed us how to call python from bat (although I can't verify it). Couldn't you simply add you var1 after the name of the py script like I did in call_py.sh?
# syntax might be wrong
start C:\python27\python.exe D:\py\some_script.py var1
some_script.py then receives $1/var1 as sys.argv[1]
some_script.py:
import sys
print "I'm a python script called " + sys.argv[0]
print "I received this cmd line arg from shell: " + sys.argv[1]
Output:
$ sh call_py.sh "VARIABLE_GIVEN_TO_SHELL_AND_PASSED_TO_PY"
I'm a shell script and i received this cmd line arg: VARIABLE_GIVEN_TO_SHELL_AND_PASSED_TO_PY
I'm a python script called some_script.py
I received this cmd line arg from shell VARIABLE_GIVEN_TO_SHELL_AND_PASSED_TO_PY
shell script still running after python script finished
Does this work or is Windows weirder than I thought? :)
UPDATE:
I fired up the old malware magnet and tried passing arguments from command line -> batch script -> python. I didn't use your python -x %0 %* syntax that seems to allow running python code in a batch script but simply wrote a separate .py file and called that from the .bat file.
This worked on Windows 7:
call_py.bat:
#ECHO off
set var1=%~1
echo Bat script called with cmdline arg: "%var1%"
echo Passing cmdline arg to python script
call C:\Python27\python.exe C:\Users\bob\Desktop\print_arg1.py %var1%
echo Bat again - continuing...
pause
print_arg1.py:
import sys
try:
print "Python says hi and received this cmdline arg: " + sys.argv[1]
except IndexError:
print "Python says hi but didn't receive any cmdline args :("
Output:
C:\Users\bob\Desktop>call_py.bat I_AM_A_CMDLINE_ARG
Bat script called with cmdline arg: "I_AM_A_CMDLINE_ARG"
Passing cmdline arg to python script
Python says hi and received this cmdline arg: I_AM_A_CMDLINE_ARG
Bat again - continuing...
Press any key to continue . . .

python script to send commands to vim (mvim)

I wrote a very simple vim plugin and python script trying to test some communication between the two. My vim-script looks like this:
function! HelloWorld()
silent :!python helloworld.py
endf
nmap <C-P> :call HelloWorld()<CR>
then my python script looks like this:
import os;
os.system( 'mvim --servername VIM -u NONE -U NONE --remote-send \"<C-\\\\><C-N>:echo \'Hello World!\'<CR>\"' )
If I am in vim and press , use the ":call HelloWorld()" command, or just type ":!python helloworld.py" from the same or another mvim or vim instance, nothing happens. However, if I call the script from the command line separately, mvim responds appropriately: shows "Hello World!" along the bottom.
Does anyone have any idea why it is not working when called from vim?
Try replacing
silent :!python helloworld.py
with
silent :!(sleep 0.5s && python helloworld.py) &
redraw!
(the point is in returning to vim before remote command arrives). If it works, then the problem is in processing remote commands while receiving shell output. You can also try another workarounds:
call system('python helloworld.py')
,
call system('python helloworld.py &')
and
pyfile helloworld.py
(Note that the last one requires vim compiled with +python feature and also alters the state of python interpreter used by vim).
By the way, use system() call instead of ! when you don't want to see the script output. Also use redraw! after silent !.

Categories

Resources