I've got python script (ala #! /usr/bin/python) and I want to debug it with pdb. How can I pass arguments to the script?
I have a python script and would like to debug it with pdb. Is there a way that I can pass arguments to the scripts?
python -m pdb myscript.py arg1 arg2 ...
This invokes pdb as a script to debug another script. You can pass command-line arguments after the script name. See the pdb doc page for more details.
usually I use ipython
-i
If running code from the command line, become interactive afterwards.
It is often useful to follow this with `--` to treat remaining flags as
script arguments.
ipython --pdb -i -- test.py -a
python3 -m pdb myscript.py -a val if using argparse with flag "a" and value "val"
If, like me, you prefer the more graphical pudb debugger, you can pass the arguments of your script directly by doing:
pudb myscript.py arg1 arg2 ...
Indeed, invoking:
python -m pudb myscript.py arg1 arg2 ...
won't work will return with the following error:
No module named pudb.__main__; 'pudb' is a package and cannot be directly executed
Related
I have a shell script that calls various python scripts and passes an argument it got onto the python scripts. However, the python scripts are currently not getting the argument. when i go to sys.argv it instead shows something completely different. I have the code and sys.argv below:
Shell Script (called run_everything.sh) :
#!/bin/bash
source venv/bin/activate
python3 eval_otare_on_otare.py $1
then i have a line in eval_otare_on_otare.pt that prints the arguments passed:
print(sys.argv)
And I get the following list:
['eval_care_on_otare.py', 'run_everything.sh']
what can I do because sys.argv is clearly not getting what I want, I want it to return
['eval_care_on_otare.py', $1]
where $1 is the argument passed
If your activate script messes up the value of $1, save it first.
#!/bin/bash
arg=$1
source ./venv/bin/activate
python3 eval_otare_on_otare.py "$arg"
Tangentially note also When to wrap quotes around a shell variable?
I am using pipenv for managing my packages. I want to write a python script that calls another python script that uses a different Virtual Environment(VE).
How can I run python script 1 that uses VE1 and call another python script (script2 that uses VE2).
I found this code for the cases where there is no need for changing the virtual environment.
import os
os.system("python myOtherScript.py arg1 arg2 arg3")
The only idea that I had was simply navigating to the target project and activate shell:
os.system("cd /home/mmoradi2/pgrastertime/")
os.system("pipenv shell")
os.system("python test.py")
but it says:
Shell for /home/..........-GdKCBK2j already activated.
No action taken to avoid nested environments.
What should I do now?
in fact my own code needs VE1 and the subprocess (second script) needs VE2. How can I call the second script inside my code?
In addition, the second script is used as a command line tool that accepts the inputs with flags:
python3 pgrastertime.py -s ./sql/postprocess.sql -t brasdor_c_07_0150
-p xml -f -r ../data/brasdor_c_07_0150.object.xml
How can I call it using the solution of #tzaman
Each virtualenv has its own python executable which you can use directly to execute the script.
Using subprocess (more versatile than os.system):
import subprocess
venv_python = '/path/to/other/venv/bin/python'
args = [venv_python, 'my_script.py', 'arg1', 'arg2', 'arg3']
subprocess.run(args)
When running a python with pdb, like
python -m pdb myscript.py some_arg another_arg
I don't want it to ask me what to do. I want it to "continue" immediately. Is there a way to do that? Is there a way to do that without editing the global pdb configuration in ~/.pdbrc?
python -m pdb -c continue myscript.py some_argument another_argument
From pdb docs:
pdb.py accepts a -c option that executes commands as if given in a .pdbrc file, see Debugger Commands.
A normal python script that does not need extra arguments from the command line can be executed passing -i to enter python upon completion. I was wondering what to do when my script reads in multiple other arguments using sys.argv if I want to enter python upon completion as well? Because at the moment it returns error of the arguments being read. Thanks!
this works fine:
python -i myscript.py arg1 arg2 arg3
Example:
nosklo#stackoverflow:~$ touch foobar.py # create empty file
nosklo#stackoverflow:~$ python -i foobar.py arg1 arg2
>>> import sys
>>> sys.argv
['foobar.py', 'arg1', 'arg2']
I am attempting to run a script through the django shell that uses command line arguments. My script looks like the following:
#...
import sys
arguments = sys.argv
# do stuff based on the arguments
so I tried to run the following in the command line:
python manage.py shell >> my_script.py arg1 arg2 ...
which gave me CommandError: Command doesn't accept any arguments
I would like to avoid using standard input, although I understand that it is a fall back plan. So is there a way to have a python script that requires command line arguments to run through the django shell?