How to run python script with arguments in python 3 interpreter? - python-3.x

I am using PyZo(with python3.5) and dont know how to run a script with arguments from PyZo's python interpreter - or from python interpreter in general. I found following working example here for python3 but dont know how to pass arguments (e.g. csv file input_data.csv) to the script
>>> exec(open("./script.py").read())
This is working in iPython:
In [1]: run script.py input_data.csv
What is the python 3 equivalent of the iPython command above ?
Thanks
Note 1
When running a script with arguments from an OS command line you type this:
$ python script.py input_data.csv
What I would expect when using python interpreter is being able to run a python script e.g. like this:
>>> script.py input_data.csv
i.e. without calling python executable, or using 'exec(open("./script.py").read())', etc.
For me running a script with arguments is very fundamental thing to do but apparently not for majority of users.

>>> import subprocess
>>> subprocess.run('python script.py input_data.csv', shell=True)

Related

How to force line buffering for python 3.6.7?

I use "stdbuf -oL" to force line buffering when running commands on Ubuntu. This works for Python 2.7, but does not seem to work for Python 3.6.7:
# test_buf.py
import time
for i in range(10):
print('a')
time.sleep(1)
I run the command: stdbuf -oL python test_buf.py > output.txt
When running with Python 3.6.7 the file output.txt updated only after the interpreter exists. When running with Python 2.7 output.txt is updated with every line written.
1. Any idea why stdbuf does not work with Python 3.6.7?
2. Any other alternative to force line buffering (not from within the Python process)?

executing perl script using python

i have a perl script 'ProcessServerData.pl' in 'C:\Users\Gurpreet Singh\Desktop\Amar_AG\KA03MM7155' and wish to run it through python program present in 'C:\Users\Gurpreet Singh\Desktop\Amar_AG'. What should my python program be?

python3: How can I run a python script when python starts?

I have a shortcut which runs python3 in a terminal window. I would like to add some import commands to a python script which is to be run when python starts.
How can I do this?
eg; I have xfce4-terminal -e python3 which starts a graphical terminal session with python3 running. I want to add something to this to make python3 execute a script, however I do not want python to exit at the end of the script, which is the default behaviour if a filename is given immediatly following the python3 command.
See python --help. It mentions an environment variable called PYTHONSTARTUP which looks like it could help you get where you want.

Python shebang line with Python3

I have the following python file:
vagrant#vagrant-ubuntu-trusty-64:~/my_app$ cat version_py3.py
#!/usr/bin/env python3
print "Python 2 print statement"
and I thought python version_py3.py will enforce the use of python3. But the code ran fine. and python3 version_py3.py gives expected error.
vagrant#vagrant-ubuntu-trusty-64:~/my_app$ python3 version_py3.py
File "version_py3.py", line 3
print "Python 2 print statement"
^
Also, there is no /usr/bin/env folder on my linux. Am I missing something here with the shebang right, but Python 2 and Python 3 are both installed on my computer.
vagrant#vagrant-ubuntu-trusty-64:~/my_app$ python -V
Python 2.7.6
vagrant#vagrant-ubuntu-trusty-64:~/my_app$ python3 -V
Python 3.4.3
vagrant#vagrant-ubuntu-trusty-64:~/my_app$
When you execute a script and you want have the shebang line respected, you may not prepend "python" in the command. A binary executable as the head of a command, which could work in she-bang as well, any given she-bang is ignored because the command executable is always preferred by the kernel.
Example: So let's assume you have a little python script, but with cat in the shebang. What happens when you set the executable bit of the script and call it as the command head is that the system first reads the first line, strips off the initial two bytes and tries to execute the (mandatorily binary) executable and feed it the script file.
$ /tmp/getver
#!/bin/cat
import sys
print( sys.version )
And now try and see yourself prepending python, python2 and/or python3.
Trivia: The interpreter may choose to process the shebang another time, on its own. E.g. perl used to respect any flags like -w given, whether it is given on the shebang or on the command, no matter if perl is the command head.
Try to located the python installation with locate for example, and set it in your shebang, like #!/usr/bin/python3.6 . I bet it's here.
I am not sure if you have found the solution. In python3, the syntax for print is different. The message has to be inside the parenthesis.
Try print("Python 2 print statement")

Running a bash job within a python script

I was hoping for some advice on using the subprocess module.
I'm trying to run a bash job within a python script so my bash command (in the right directory) is: ./program myjob.inp
This is just running the executable "program" with myjob.inp being the input file (and my python script constantly updates myjob.inp).
I know that if I just wanted to run "program", I could do something like:
with open("tmp.dat","w") as fstore_tmp:
subprocess.call(["./program"], stdout = fstore_tmp)
However, I can't figure out how to run the job taking in the input file myjob.inp such that it's doing the equivalent of ./program myjob.inp. I tried:
with open("tmp.dat","w") as fstore_tmp:
subprocess.call(["./program", "myjob.inp"], stdout = fstore_tmp)
However, that doesn't seem to be working. Does anyone have any suggestions? Thanks!

Resources