Reputation: 45
I am trying to configure PyCharm: I need to write my script in the main editing window and then I want to check in the Python console window whether the results (variables, lists) work as expected.
Nonetheless, as I start using the Python console after running the script, the console has not collected any data from the running process.
Upvotes: 1
Views: 1891
Reputation: 315
The "Python Console" is a separate environment from the one you run code in when you press "Run...".
First of all: Use the excellent debugging facilities provided by PyCharm to step through your code and see whether things work as they should in situ.
If that for some reason doesn't do it for you:
You can from <yourfile> import *
in the console, if running pure python. That will get you your variables. If you do import <yourfile>
, the script will be run, but your variables will only be available as <yourfile>.<var>
. Of course, anything you have under if __name__ == '__main__'
won't be executed.
If this won't do, I refer you to https://stackoverflow.com/a/437857/3745323:
with open("yourfile.py") as f:
code = compile(f.read(), "yourfile.py", 'exec')
exec(code, global_vars, local_vars)
You can definen that as a function in a separate file and import it. Alternatively, just type it into the interpreter.
If you have IPython installed, recent PyCharm editions default to using it in the console. In this case you can use the %run
magic to run your script as if it was typed in line-by-line into the interpreter.
In[2]: %run yourfile
Upvotes: 3