azureai
azureai

Reputation: 181

shell script which opens python with arguments to python script

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

Answers (2)

Tomerikoo
Tomerikoo

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:

  1. Open Edit Configurations...:

    An image of the Pycharm window, highlighting the option "Edit configurations" that is shown after clicking the drop-down menu next to the run symbol at the top-right corner

  2. Make sure you edit the configuration of your main.py:

    An image showing the "Edit configurations" window, with the name of the current file highlighted at the left pane

  3. Then, simply add the command line arguments, just as you would in the command line, under the parameters field:

    An image showing the "Edit configurations" window, with the "Parameters" input field highlighted

  4. Press OK

  5. 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

Sasha Khapyorsky
Sasha Khapyorsky

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

Related Questions