How to make `python2 -V >> myfile` "work"? [duplicate] - python

This question already has answers here:
Redirect all output to file using Bash on Linux? [duplicate]
(6 answers)
Closed 6 years ago.
I want to automatically record my python version in a computerinfo file. However
python2 -V >> myfile
prints only to the console but not to myfile. How can I get the output into a file?
Curiously, for python3 -- python3 -V >> myfile -- this works as I expected.
I am on Ubuntu 16.04 and I use the bash shell.

There is a bug in python2 -V, that prints the version as stderr instead of stdout. Try to redirect the stderr like:
python2 -V 2>> myfile

Related

Python version output [duplicate]

This question already has answers here:
Which version of Python do I have installed?
(26 answers)
Closed 12 months ago.
Every time I type "python -v" in cmd or another terminal result same
here
You must type the v option uppercase python -V or with --version
According to the --help
-v is verbose mode: It traces all inputs
-V returns version.

How can I run inline Python code in a command-line shell? [duplicate]

This question already has answers here:
How to execute Python inline from a bash shell
(4 answers)
Closed 2 years ago.
In a shell, I can execute AppleScript command-line code like so:
osascript -e "tell application \"Finder\" to activate"
Is the same thing possible in Python, e.g.:
python --execute "print('hello world!')"
Something like this should work for you!
python -c 'print("Hi")'
Probably a duplicate of How to execute Python inline from a bash shell

Capture run time of python script executed inside shell script [duplicate]

This question already has answers here:
Get program execution time in the shell
(11 answers)
Closed 5 years ago.
I have bash shell script which is internally calling python script.I would like to know how long python is taking to execute.I am not allowed to do changes in python script.
Any leads would be helpful thanks in advance.
You can use the time command to get the runtime of the python script.
]$ cat time_test.bash
#!/bin/bash
# You can use: time python script.py
time python -c 'import os;os.getenv("HOME")'
Output will be something like this
]$ ./time_test.bash
real 0m0.010s
user 0m0.005s
sys 0m0.005s
Call the python script with /usr/bin/time script. This allows you to track CPU and wall-clock time of the script.

Store the output of command in variable in unix [duplicate]

This question already has answers here:
How to store standard error in a variable
(20 answers)
Closed 7 years ago.
Need to check the version of python so for that i am hitting this command in a script.
I am trying to store the output of the given command in a variable "x".So that I can use this x in further script.
But when I am trying to print x it is showing null value(no value).
[bin]$x=`/path/thirdparty/python/2.7/bin/python2.7 -V`
Python 2.7.8
[bin]$echo x
Please help me to store the value of the command in a variable.
python -V writes to the standard error, not to the standard output. So you have to redirect STDERR (2) to STDOUT (1).
$ x=$({python -V} 2>&1)
$ echo $x
Python 2.7.6
Hm... the output is possibly written to stderr, not stdout. Try this:
x=`/path/thirdparty/python/2.7/bin/python2.7 -V 2>&1`

Is there anything like "set -x" in python [duplicate]

This question already has answers here:
What is the Python equivalent of `set -x` in shell?
(4 answers)
Closed 5 years ago.
I'd like to run my python script in such a way that every line is printed as it is executed (for debugging purposes), like how you can add set -x to a bash script. Is this possible? If not, what other strategies are there for debugging python that don't involve adding log lines everywhere? I usually use pdb but this isn't always suitable for time sensitive applications eg networking.
Use the trace module.
$ python -m trace -t myscript.py | grep myscript.py

Categories