Home » Python » pycharm – How can I run a .py file with its options in Python console?-Exceptionshub

pycharm – How can I run a .py file with its options in Python console?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I am trying to run this GitHub project in python, but I could only run it using the Terminal of Pycharm IDE.

According to the guide from the GitHub repository, I removed the $ sign from the beginning of $ python train.py RGCN PPI and could run it there. What does $ mean here and how can I run a file like this in Python Console (for example after >>> sign)?

How to&Answers:

The ‘$’ isn’t part of Python’s syntax, it’s a visual cue in the documentation representing the command prompt.

To answer the question from the title of this post, I’ll provide some
instructions first on how to load scripts into the Python console.
However, for your specific case, you don’t need this. Scroll down to
the part about debugging in PyCharm.

There’s two ways you can get your script into the console. One is to simply load it using the right version of the two lines I give right below, or you can load it as a module – even if it wasn’t intended to be one.

In general, to execute a script in the Python shell on Python 2 you can do

>>> execfile(r"<path to script here>")

On Python 3 it’s more verbose:

>>> exec(open(r"<path to script here>").read())

The effect this has is as if you cut-n-pasted the script into the console. The console’s global scope will get all the functions, classes, and variables that are leftmost indented in the file. Also it might not run your if __name__ == '__main__': block. But you could hack that.

If you want the vars/classes/etc to be put in another scope than your console’s global scope, then there are two additional parameters to the above commands. The first one is a dictionary for the globals , and one for the locals. You can get away with only supplying the globals parameter – it’s just an ordinary dictionary object you need.

If the file you want to load is a module, you could import it as you would any other module by appending its home folder to the Python module search path, and using the import directive. You can load your script this way even if it wasn’t intended to be module.

>>> import sys
>>> sys.path.append(r'/Users/todd/projects/mymodule_folder')
>>> import mymodule

If you make modifications to it and want to reload it:

>>> import importlib
>>> importlib.reload(mymodule)

Loading your script as a module avoids polluting your console’s global scope. After it loads, just prefix the names of your script’s functions and variables with the module name. The module name will be the name of the file without the .py extension.

If the script requires command line options, you could just hard code values for those into the script and disable lines of code that try and get values from the CLI. If it gets complicated, consider running it in an IDE as described in the next section.

So the above is how you can run your python scripts in whatever Python REPL console you want.

BUT loading your scripts into the Python console may not be at all
required for your purposes. You wanted to debug some scripts (train.py,
test.py) from this project:
https://github.com/microsoft/tf-gnn-samples).

Debugging Command Line Script With PyCharm

In many cases, a Python script is written to run from the OS shell and take command line options from the user. These kinds of script could be loaded into the Python console, but most require some minor hacks to run. However, if all you want to do is debug such a script, you don’t need to muck with the console.

PyCharm supports running these as is (as does Eclipse and other IDEs) like any other script. It’s just a matter of creating a run/debug configuration for the project. I just installed PyCharm and gave it a try in order to record the details. Easy task.

Just open the project in PyCharm, and above the editor pane, on the toolbar, there’s a menu option for Edit Configurations. Click that to open the Run/Debug Configurations dialog and click the + to add a configuration. A small dialog will appear with predefined templates – select Python as your template and accept.

Then in the main dialog, fill in Script path: with the path to train.py (or another script), then click the checkbox, [x] Emulate terminal in output console. Also, you can add command line options in the Parameters: text box (I put in the text: mymodel mytask just to satisfy the script’s need for two parameters). Click OK at the bottom to accept the configuration and shut the dialog.

Now you should see a green bug icon on the toolbar.Set a breakpoint in the __main__ block of the script and click the debug icon to start debugging the script. That should do it!