Beginner's questions on PyCharm
Answered
1) Is there a way to get PyCharm to run the currently open file? If I last ran file1.py, then open file2.py, PyCharm won't automatically run file2.py - I either have to right click on file2.py from the project tab on the right, which shows all the files, and click run from there, or, from the menu: Run -> Run 9Alt shift F10) -> choose the file I want to run. Why? It seems convoluted. is there a quicker way?
2) If I run file.py once, I see the output in a tab headed "file" at the bottom. If I run it again, another tab, called "file(2)" is created.
Is it possible not to create this additional tab?
Or is there a way to automatically close all the tabs from the previous runs?
Thanks!
Please sign in to leave a comment.
1) There is no such feature to automatically switch run/debug configuration, but you could use a shortcut to invoke the same action you currently do with menu or context menu.
2) Do you run it in Python Console? If yes, you could enable Use existing console for "Run with Python console" under Preferences | Build, Execution, Deployment | Console
Thanks. Is there a way to automatically clear all the variables from the console before running?
Also, and I fully realise this is very much a newbie question, can you recommend any online resource to clarify the differences between running a program with and without using ipython? You asked if I was running it in a Python console - what is the alternative?
>Thanks. Is there a way to automatically clear all the variables from the console before running?
No, since this is the same Python Console it preserves all your variables in variables view. You can use del to remove your objects from runtime, e.g.:
And then it will be also removed from variables view.
>Also, and I fully realise this is very much a newbie question, can you recommend any online resource to clarify the differences between running a program with and without using ipython? You asked if I was running it in a Python console - what is the alternative?
You mix different things a little. In PyCharm you can just run your python script as if you were running something like python <path_to_script> or you can run it in Python Console which lets you interact with it afterwards. If you enable Run with Python console in Run | Edit Configurations... then PyCharm will run it in Python Console (well, obviously).
IPython is a command shell for interactive computing in multiple programming languages. You can read more about it in the web.
In PyCharm, if ipython package is installed and Use IPython if available under Settings | Build, Execution, Deployment | Console is enabled, you will be able to use IPython in Python Console.
I know that for someone who is not very familiar with all this it's quite frustrating and confusing. This is why I would recommend to first disable Run with Python console, if you don't actually need it.
Here is our documentation on how to create and run your first project https://www.jetbrains.com/help/pycharm/creating-and-running-your-first-python-project.html