PyCharm running Python file always opens a new console - python

I initially started learning Python in Spyder, but decided to switch to PyCharm recently, hence I'm learning PyCharm with a Spyder-like mentality.
I'm interested in running a file in the Python console, but every time I rerun this file, it will run under a newly opened Python console. This can become annoying after a while, as there will be multiple Python consoles open which basically all do the same thing but with slight variations.
I would prefer to just have one single Python console and run an entire file within that single console. Would anybody know how to change this? Perhaps the mindset I'm using isn't very PyCharmic?

There is a specific option in PyCharm 2018.2+: Settings | Build, Execution, Deployment | Console | Use existing console for "Run with Python console".
Run with Python console is an option you have enabled in the Run Configuration. Disable it if you don't need a Python console after a script execution:

Hi: If you are looking for re running the code again in the same python console everytime then you have to check the respective box in the Project settings as shown in image below.

To allow only one instance to run, go to "Run" in the top bar, then "Edit Configurations...". Finally, check "Single instance only" at the right side. This will run only one instance and restart every time you run.

One console is one instance of Python being run on your system. If you want to run different variations of code within the same Python kernel, you can highlight the code you want to run and then choose the run option (Alt+Shift+F10 default).

You have an option to Rerun the program.
Simply open and navigate to currently running app with:
Alt+4 (Windows)
⌘+4 (Mac)
And then rerun it with:
Ctrl+R (Windows)
⌘+R (Mac)
Another option:
Show actions popup:
Ctrl+Shift+A (Windows)
⇧+⌘+A (Mac)
And type Rerun ..., IDE then hint you with desired action, and call it.

I think that what you are looking for is the last option in this window; check it and it should work.
Settings -> Build, Execution, Deployment -> Console

Related

VScode run code selection

I just made the transition from Spyder to VScode for my python endeavours. Is there a way to run individual lines of code? That's how I used to do my on-the-spot debugging, but I can't find an option for it in VScode and really don't want to keep setting and removing breakpoints.
Thanks.
If you highlight some code, you can right-click or run the command, Run Selection/Line in Python Terminal.
We are also planning on implementing Ctrl-Enter to do the same thing and looking at Ctr-Enter executing the current line.
You can:
open a terminal at Terminal>New Terminal
Highlight the code you want to run
Hit Terminal>Run Selected Text
As for R you can hit CTRL Enter to execute the highlighted code. For python there's apparently no default shortcut (see below), but I am quite sure you can add yours.
In my ver of VSCode (1.25), shift+enter will run selection. Note that you will want to have your integrated terminal running python.
One way you can do it is through the Integrated Terminal. Here is the guide to open/use it: https://code.visualstudio.com/docs/editor/integrated-terminal
After that, type python3 or python since it is depending on what version you are using. Then, copy and paste the fraction of code you want to run into the terminal. It now has the same functionality as the console in Spyder. Hope this helps.
I'm still trying to figure out how to make vscode do what I need (interactive python plots), but I can offer a more complete answer to the question at hand than what has been given so far:
1- Evaluate current selection in debug terminal is an option that is not enabled by default, so you may want to bind the 'editor.debug.action.selectionToRepl' action to whatever keyboard shortcut you choose (I'm using F9). As of today, there still appears to be no option to evaluate current line while debugging, only current selection.
2- Evaluate current line or selection in python terminal is enabled by default, but I'm on Windows where this isn't doing what I would expect - it evaluates in a new runtime, which does no good if you're trying to debug an existing runtime. So I can't say much about how useful this option is, or even if it is necessary since anytime you'd want to evaluate line-by-line, you'll be in debug mode anyway and sending to debug console as in 1 above. The Windows issue might have something to do with the settings.json entry
"terminal.integrated.inheritEnv": true,
not having an affect in Windows as of yet, per vscode documentation.

In vscode using Python, ctrl+F5 always asks for "select environment"

I installed VSCode, downloaded official Python 3.6.4.
VSCode detected and set the environment right - I do see "python.pythonPath" user setting set correctly.
But, when using VS Code using Ctrl+F5to run a Python file, I am always getting asked for "select environment" and it shows me two options
- Python
- Python Experimental
What is this "Python Experimental"? How can I get rid of getting interrupted every time I try to run a script?
Run > Add Configuration... => Choose one of the two options.
After doing this it will no longer prompt you.
Ctrl+F5 is a shortcut to Start without Debugging What you want is just running a file according to your post. To run a python file in the integrated terminal, right-click your editor window and Run Python File in Terminal. It should open up a terminal window within VS Code and run as normal. It also takes into account the environment you are currently using whenever you run it.
To answer your other question about Python Experimental, it is an option to use the experimental debugger, an alternate version of a Python Debugger. You can read the instructions here

How to run same application in new run tool window? Pycharm Community 2017

I am working on a Python 3.6 project in Pycharm Community Edition 2017.2.3.
I have multiple run configurations in my project. Each different run configuration will open a different 'Run tool window'.
I want to look through the output of the previous run of my application while re-running it in another tool window. However, each new run of the same configuration will overwrite the console output of the previous run. How do I run the same application/configuration in a new tool window?
It's probably possible to just duplicate the configuration, but I'm looking for a better way. Maybe something similar to opening new terminals?
Go to build configurations and enable show command line afterwards.
It simply opens the python interpreter in the console after running your code.
Now When you run the code again. It will open a new tab in the run window, instead of overwriting the old one.
You can pin first tab by right click then run another script
I do not think if it is possible. But you can do it using terminal.
It's still in Run/Debug configurations but now it's called, "Run with Python console".

Running a module from the pycharm console

I'm new to python and pycharm and I'd like to run a module from the pycharm console in the same way as you can from IDLE, if it's possible.
The idea is to create simple functions and test them "live" using the console.
...how do you do that in pycharm?
Running python scripts using pycharm is pretty straightforward, quote from docs:
To run a script with a temporary run/debug configuration Open the
desired script in the editor, or select it in the Project tool window.
Choose Run on the context menu, or press Ctrl+Shift+F10. So
doing, a temporary run/debug configuration is created on-the-fly.
Besides there is a "Python Console" available in pycharm: see documentation.
UPD:
Here's an example.
Imagine you have a python module called test_module.py:
def a(*args, **kwargs):
print "I'm function a"
def b(*args, **kwargs):
print "I'm function b"
Then, in pycharm's "Python Console" you can do this:
>>> from test_module import *
>>> a()
I'm function a
>>> b()
I'm function b
If you need to execute a part of an existing code, you can use the Execute Selection in Console feature: select the code snippet -> right click -> "Execute Selection in Console".
For anyone still having this problem: Go to the Run/Debug menu, choose Edit Configuration, check the box 'Show command line' this will enable you to enter parameters in the console at the >>> prompt and test your function.
Edit: To make this change apply to all your .py files (as this check box only applies to the current file you're working on) go to: Edit configuration, in the pop up you will see a menu tree on the left, select Defaults, then Python, then check the 'Show command line' box, this will make it the default setting whenever you open a .py file, (this feature should really be on by default!)
Right Click --> Run File In Console
Done!
Looks like in version 2018.3, this option is now Run with Python console in Run/Debug Configurations:
What you're looking for is the feature called Execute Selection in Console which is described in section Loading Code from Editor Into Console of PyCharm's online help.
Select the script lines that you want to execute and press Shift+Alt+E
You can run the Find Action shortcut (Ctrl+Shift+A or ⌘+⇧+A on mac), then type run file, and choose the option Run file in Console.
In pycharm do:
Run>Edit Configuration>Show command line afterwards
Assuming your code is in file MySimpleCode.py you can simply say
run MySimpleCode
in the PyCharm console. This assumes that you have set your working directory properly; e.g. if MySimpleCode.py is located in d:\work on a Windows system you must execute
cd d:\work
first. In my opinion the other solutions miss what the post really wants: simply executing a file like from a DOS or Unix shell, or a .m script in MATLAB. No messing with imports, projects and so on. If you use CTRL SHIFT F10 your code gets executed, sure, but in a different environment, so you have no access to variables created in your code. I assume the question means that you want to do further work with the results of the script.
Explanation for people with MATLAB background: In most Python IDEs you have to configure an interpreter first in some kind of project. The MATLAB equivalent would be a master IDE where you can choose your MATLAB version for each project. This makes it possible to run your Python code on the CPU, your GPU or even an external NVIDIA board with different settings (after several days in the installation hell). For the beginner this is very confusing, because for simple code samples any "default" interpreter should suffice. Unfortunately this is not the case for Python (2 or 3? 2.x or 2.y? which package version?), and it will get worse as you progress (which 32 or 64 bit version of TensorFlow is available for Python 3.x? and so on).

Python curses Redirection is not supported

I am trying to use Curses in PyDev in Eclipse in Win7.
I have installed Python 3.2 (64bit) and curses-2.2.win-amd64-py3.2. When I input the following testing codes into PyDev:
import curses
myscreen = curses.initscr()
myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()
curses.endwin()
It did not show any syntax error, so I think the curses was installed correctly.
However, when I ran it as Python Run, the output showed: Redirection is not supported. I do not know where this problem comes from. I googled a lot but can't find related information.
Recent PyCharm versions (I am currently running 2017.2, not sure when this option was added, or if it has been there the entire time) have the option "Emulate terminal in output console". Curses works with this option checked.
You cannot expect to use curses with a non-terminal.
Probably you get this because you are running the script from inside an IDE, like PyCharm or any other.
All IDEs do provide consoles that are not terminals, so that's where the problem comes from.
For a Pycharm user the solution given by codeape works fine :
Snapshot
You can't use any IDE to run python files with the curses package. I used to run in pycharm and naturally couldn't run.
Change to the command line to run:
for testing follow my following steps
on desktop open notepad and copy paste the code and save it as filename.py
open command line change directory to desktop use below command cd Desktop and hit enter type python example.py and hit enter, your program will definitely run
My workaround is to create a Run Configuration that calls a curses script. The little overhead is worth not having to switch to the terminal and manually run the script hundreds of times a session. I use Intellij but I imagine the process should be similar in PyCharm.
The desired result is the convenience of a button to run the script:
First create a script that calls the entry script, for instance:
ptyhon name-of-script.py
Then, to create a configuration for each script:
Go to Edit configuration.
Click the plus button and add a Shell Script.
Enter the path to a shell script.
Here is a picture of a directory with a couple of sample scripts.
I use this process to view my progress. My curses scripts are very modest so fortunately I can live without a debugger.

Categories