When the function below is entered into a notebook cell, the output will have an input box to enter the details of the prompt.
def a():
val = raw_input("abc")
print "Entered value: %s" % val
I'm trying to achieve the same thing by running the script, but it doesn't work. I've copied the same code for the function above into a script sample.py as below.
# cat sample.py
def a():
val = raw_input("abc")
print "Entered value: %s" % val
a()
Attempts to achieve the same behavior of getting a notebook prompt while running the script from notebook:
1. Using the '!' prefix
This command just keeps running without giving any prompt:
[*] !python sample.py
abc
2. Using %%bash
Gives the following error:
%%bash
python sample.py
EOFError: EOF when reading a line
What is the reason for this to fail and is there a workaround to get it working as expected?
The two command you've used are used for shell command and bash script respectively.
To run Python script as a program you can use Magic command run.
Related
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
I have a simple python script
name = input("Enter your name: ")
print ("Hello, " + name)
saved to test.py
I have a simple powershell script
python test.py
saved to Untitled2.ps1
Running Untitled2.ps1 from either the Powershell IDE or the command line cause no printing of the input request and no output. It hangs and has to be cancelled out. Entering data and pressing Return also cause no output.
Python 3.8.0, Windows 10, Powershell 5.1
Running on Windows system, I run bash.exe using subprocess.call().
Following is the code
def predict():
os.system('notepad cmnd.txt')
subprocess.call(['C:/Windows/System32/bash.exe'])
print(file_contents)
label = Label(master, text=file_contents)
#subprocess.call(['c:/users/hp/open.py'])
label.pack()
The handle passes to bash,thus not executing a couple of commands.
cd commands that runs on actually entering values return Missing Directory error.
ls command returns 'cannot run binary file' error.
What should I do?
I'm not really sure what you want here, but if you want to run bash commands in a Windows enviorment, you can try using subprocess.check_output():
from subprocess import check_output
bash_commands = ['ls', 'pwd']
for command in bash_commands:
output = check_output(['bash', '-c', command]).decode()
print(output)
Which in this example, lists all files in the current directory and prints out the parent working directory.
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 . . .
consider the simple python program below which runs a powershell script. It works fine if I start the python program from the command line, even though I feel it's odd it's printing a blank line for the return value instead of 0. However, if I run this python program from pydev, the script hangs after calling subprocess.call(). I'm using Pydev 2.7.0 under Eclipse SDK 4.2.1 under Windows 7. Python version is 2.7.3.
import subprocess
def run_powershell_script(script):
cmd = ['powershell',
'-ExecutionPolicy',
'RemoteSigned',
'-File',
script]
returncode = subprocess.call(cmd)
print "Done"
return returncode
if __name__ == "__main__":
print run_powershell_script("testscript.ps1")
The powershell script I'm testing with is very simple: it prints the path and then returns 0:
Write-Host "$env:Path"
exit 0
So to recap I guess I have two questions, the most important being why subprocess.call() hangs when I run this program under pydev and the other question is why when I print the returncode I get a blank line instead of 0.
I'm not 100% here, but doesn't exit just kill the script? To return a value when you exit to script, use return.. so Try this PS script instead:
Write-Host "$env:Path"
return 0