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

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

Related

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

Using pytest from a BAT file [duplicate]

This question already has answers here:
How to start Python virtual environment in .bat batch file?
(2 answers)
Closed 2 years ago.
I am trying to using pytest from a BAT file. I have tried something like this
..\venv\Scripts\activate.bat
pytest -v src/datasets_test.py::TestDatasetsOperations::test_dataset_add
But it seems to do nothing. How can I make it work?
EDIT: the question is similar, but no one will go to other question if he thinks the error is with pytest
In order to call another script from a batch script, you need to use call.
call ..\venv\Scripts\activate.bat
pytest -v src/datasets_test.py::TestDatasetsOperations::test_dataset_add

how to run command line python scripts within my code [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 2 years ago.
there are some scripts that you have to run them from command line like
python run.py -u alex__a -p 123 -v -f
I want to work with them inside my code I don't know whats the name of them so I wasn't able to google about my question . sorry if it't so simple
You are looking for sys.argv to access them. Here is a tutorial:
https://www.pythonforbeginners.com/system/python-sys-argv
If you want to get more serious and use a package to do this more elegantly, you should research the argparse package which is excellent and very intuitive to use.

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.

make #!/usr/bin/env python -u shebang portable [duplicate]

This question already has answers here:
Cannot pass an argument to python with "#!/usr/bin/env python"
(9 answers)
Closed 9 years ago.
I have this non-portable shebang:
#!/usr/bin/env python -u
It is non portable because python -u is fed as one single arg to env on my system.
Challenge: make this shebang portable changing the shebang only - that is to say a one-liner.
In other words, no solutions
from the question Disable output buffering
from the question Cannot pass an argument to python with "#!/usr/bin/env python"
I'd use the following:
#!/bin/sh
"""true"
exec python -u "$0" "$#"
"""
# python code goes here
The line """true" will be parsed by sh as true, because it consists of an empty "" string followed by "true". Since true is a no-op command, it will be effectively ignored, and the following line will execute the Python interpreter.
On the other hand, Python will parse the """true" line very differently, as the opening of a triple-quoted string which starts with true" and is closed two lines below. Since the string is not used for anything, the Python interpreter will effectively ignore the shell snippet that starts up Python. It is the difference in interpretation of """xxx" that allows Python and sh code coexist in the same script.
For a simple test, append something like:
import sys
print "hello!", sys.argv
Given a reasonable sh implementation (and taking into account the time to start Python), this should not be measurably slower than using env.

Categories