This question already has answers here:
Automatically execute commands on launching python shell
(2 answers)
Closed 9 years ago.
I often used python as a library and it could save me some time if I could use an alias to launch python and tell him to load the math module at the same time (ideally in the same way as from math import *) Is there a command line argument for that?
It doesn't look like much but that would save me from doing something really repetitive. Thank you.
One way of doing this on the command line is using the -c (execute command) and -i (run interactively after executing script/command) options.
$ python -i -c 'from math import *'
>>> sin(pi/2)
1.0
Related
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
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.
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.
This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 6 years ago.
How can I use command line tools like ls, grep from python.
This is done with subprocess
import subprocess
subprocess.call("ls", shell=True)
You can use the package getopt to use command line arguments with your python code.
There is also a more advanced module called argparse that is easier to code in and provides more help and helpful error messages.
I think if you import os and do os.system(command) that can execute commands.
import os
os.system("your command here")
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