profiling a python module from the command line - python

I have a python project where I execute the app as a module using the -m flag. So something like:
python -m apps.validate -i input.mp4
Now, I want to profile it using the command line. So the inline examples suggest invoking cProfile itself a module. However, I cannot do something like:
python -m cProfile apps.validate -i input.mp4
However, this results in the error "No such file or directory". I cannot just go to the apps directory and launch validate.py due to relative imports.
Is there a way to profile a module on the command line?

Instead of running cProfile in shell, maybe you can use cProfile in your python script by adding some code in apps.validate or creating a new script and import apps.validate like this. Maybe some typo below :)
import cProfile
import sys
def run_validate(args):
# run your apps.validate code with shell arguments args here
pass
if __name__ == '__main__':
pr = cProfile.Profile()
pr.enable()
run_validate(*sys.argv)
pr.disable()
pr.print_stats()
then just run the original script: python -m apps.validate -i input.mp4

Related

How to use a python module in combination with a command from shell

I would like to use a python module from shell (to be exact: indirect from gnuplot). I do not want to write for every call an extra script or implement some I/O logic.
Let's say as a minimal working example, I have a python module module_foo.py with
#!/usr/bin/python
def bar():
print(1,2,3)
My question is:
Why isn't it possible to use a python module combining module loading and command execution like here?:
$ python -m module_foo -c 'bar()'
When executed, nothing happens. But what does work, is using only a command call like this
$ python -c 'import module_foo; module_foo.bar()'
1 2 3
or this
$ python -c 'from module_foo import *; bar()'
1 2 3
As soon as I load a module before, even a syntactically errorneous command is “accepted” – not executed, I suppose (the bracked of the call to bar isn't closed):
$ python -m module_foo -c 'bar('
$
It is, however, possible to use the -m module option using a python unit test (from the python docs):
python -m unittest test_module1 test_module2
The python manpage says for both options:
-c command
Specify the command to execute (see next section). This terminates the option
list (following options are passed as arguments to the command).
-m module-name
Searches sys.path for the named module and runs the corresponding .py file as
a script.
So I'd expect to be able to use path options in this -m ... -c ..., but not in reverse order -c ... -m ...'. Am I missing something obvious?
If you want your Python module to be executable and to call function bar(), you should add this to the end of the python file:
if __name__ == "__main__": # this checks that the file is "executed", rather than "imported"
bar() # call the function you want to call
Then call:
python module_foo.py
If you want more control, you can pass arguments to the script and access them from sys.argv.
For even more flexibility in arguments passed to the script, see argparse module.

Running a script with arguments from within another python script

In Jupyter, this can be achieved with the %run line magic. Like this:
%run -i somescript.py -f -b
But that magic does not work from within a python script file. I tried this:
os.system("python3 -i somescript.py -f -b")
A further complication is that I want to use a variable, like this:
%run -i somescript.py -f $LINE -b
Have you tried using subprocess ?
That is probably not the best answer, but you can call bash from python script, which calls another python script
import subprocess
subprocess.check_call(["echo", "Hello world!"])

How can I write python code in a shell script such that it imports a module and runs a test on it?

How can I run python code in a shell script that imports a python module and tests that specific module?
For example:
from _some_path_ import File_We_Want_To_Test
if(File_We_Want_To_Test.Function_We_Want_To_Test(a, b) == True):
print("great!")
else:
print("bad!")
You can use Python in your shell like this:
python -c command
Where command is a small Python program (a quoted string).
Example:
python -c "import my_module; my_module.function()"
See the page Command line and environment

How to run module from command line?

Suppose there is a module somewhere, which I can import with
from sound.effects import echo
How can I run echo directly from command line?
Command
python sound/effects/echo.py
does not work since the relative path is generally incorrect
If the module has top-level code executing on import, you can use the -m switch to run it from the command line (using Python attribute notation):
python -m sound.effect.echo
The module is then executed as a script, so a if __name__ == '__main__': guard will pass. See for example the timeit module, which executes the timeit.main() function when run from the command line like this.

Error running Python script from shell

I am trying to pipe output from a command written in the terminal to a Python script.
For example:
ls | ./foo.py
I wrote a Python script to do the same:
#foo.py
import fileinput
with fileinput.input() as f_input :
for line in f_input :
print(line,end='')
But this does not seem to work,
when I run the following command:
$ ls | sudo ./foo.py
I get an error that says:
$ ./foo.py: command not found
I have checked the working directory and I can see the foo.py when I use the ls command, so what am I doing wrong here?
It seems like you forgot the Shebang:
#!/usr/bin/env python3
import fileinput
with fileinput.input() as f_input :
for line in f_input :
print(line,end='')
Also remember make it as executable via command:
chmod +x foo.py
Then run your command again.
You have to pipe it to the Python executable, not to the name of a file. As the error says, that filename doesn't represent a command it knows.
ls | py ./foo.py
Use py or python or however you run the Python interpreter on your particular system.

Categories