Reputation: 181
I want to debug some Python code in PyCharm but the repository I got the code from uses a shell script of the form
python main.py arg1 ... argN
which is run from the command line. The main.py module in turn has a if __name__ == '__main__':
part which uses argparse to parse the arguments passed via the script.
How can I write a Python script which also calls the main of main.py without touching main.py?
Upvotes: 0
Views: 394
Reputation: 19414
You don't need a script for that. You can Edit Run Configurations in Pycharm to emulate command line arguments passed to the script.
Follow these steps:
Open Edit Configurations...
:
Make sure you edit the configuration of your main.py
:
Then, simply add the command line arguments, just as you would in the command line, under the parameters
field:
Press OK
Run regularly using the green arrow (or right-click and Run)
If you actually have that shell script, you can run it directly by adding a new configuration. On the Configurations
window press the + on the top-left corner and choose Shell Script
. Indicate the path and any option you want to pass and run.
Upvotes: 2
Reputation: 176
For example: define new def main():
function and move all your if __name__ == '__main__':
stuff there, then rewrite main.py
as:
def main():
<original "if __name__ == '__main__'" stuff>
if __name__ == '__main__':
main()
, and then call main() function from an external python script.
Upvotes: 0