pycharm код стал запускаться в консоли заместо run
Python console
Python console enables executing Python commands and scripts line by line, similar to your experience with Python Shell.
Actions available in the Python console
In an interactive console, you can:
Type commands in the lower pane of the console, and press Enter to execute them. Results are displayed in the upper pane.
Use basic code completion Ctrl+Space and tab completion.
Use Up and Down arrow keys to scroll through the history of commands, and execute the required ones.
Load source code from the editor into console.
Use context menu of the upper pane to copy all output to the clipboard, compare with the current contents of the clipboard, or remove all output from the console.
Use the toolbar buttons to control your session in the console.
Configure color scheme of the console to meet your preferences. Refer to the section Configure color schemes for consoles for details.
Working with Python console
The main reason for using the Python console within PyCharm is to benefit from the main IDE features, such as code completion, code analysis, and quick fixes.
The console is available for all types of Python interpreters and virtual environments, both local and remote.
Preview a variable as an array
When your variables are numpy arrays or dataframes, you can preview them as an array in a separate window. To try it, do one of the following:
Click the link View as Array / View as DataFrame :
From the context menu of a variable, choose View as Array / View as DataFrame :
The variable will be opened in the Data tab of the SciView window.
Run source code from the editor in console
Open file in the editor, and select a fragment of code to be executed.
Watch the code selection execution:
By default, the Python console executes Python commands using the Python interpreter defined for the project. However, you can assign an alternative Python interpreter.
Configure Python console settings
Select any available interpreter from the Python interpreter list. Note that you cannot introduce a new interpreter here. If you want to come up with the new interpreter, you need to create it first.
In needed, click the Configure Interpreters link to inspect the list of the installed packages and add new ones.
Mind the code in the Starting script area. It contains the script that will be executed after you open the Python console. Use it to pre-code some required Python commands.
When working on several Python scripts, you might want to execute each in a separate Python console.
Run several Python consoles
Toolbar
Item | Tooltip and shortcut | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Click this button to terminate the current process and launch the new one. | |||||||||||
Item | Description |
---|---|
Script path/Module name | Click the list to select a type of target to run. Then, in the corresponding field, specify the path to the Python script or the module name to be executed. |
Parameters | |
Run with Python console | Enables running your script or module with the Python console. |
Redirect input from | Enables redirecting data from a text file to standard input. Use this option if your script requires some input and you want to automatically submit the values instead of typing them in the Run console. To enable redirecting, select the checkbox and specify the path to the target text file. |
Docker container settings This field only appears when a Docker-based remote interpreter is selected for a project.. Run options : Use this field to specify the Docker command-line options. This field only appears when a Docker Compose-based remote interpreter is selected. You can use the following commands of the Docker Compose Command-Line Interface: You can expand this field to preview the complete command string. Example: if you enter the following combination in the Commands and options field: the preview output should looks as follows: Code Running AssistancePrerequisitesYou have already created a Python project and populated it with the following code: You have Python interpreter already configured. Note that for the current project your Python interpreter version should be 3.0 or later. First run
The script runs. Enter values for a, b, and c to review the output in the Run tool window:
Let’s explore in detail what we’ve done and what we see. Each script or test you wish to run or debug from within PyCharm, needs a special profile that specifies the script name, working directory, and other important data required for running or debugging. PyCharm comes with a number of such pre-defined profiles, or run/debug configurations, that serve patterns, against which you can create any number of run/debug configurations of your own. Every time you click the Run or Debug buttons (or choose Run or Debug commands from the context menu), you actually launch the current run/debug configuration in the run or debug mode. If you look at the very first image, you will notice that in the combobox there is no run/debug configuration at all; on the second image it appears, marked with the green circle. It means that the Solver run/debug configuration has been created automatically by PyCharm, when you’ve chosen Run ‘Solver’ from the context menu. Now, as this run/debug configuration is marked with the green circle, it is current. You also see that its icon is shown semi-transparent. What does it mean? It means that the Solver run/debug configuration is temporary- PyCharm has created it automatically. OK, now click the down arrow to reveal the available commands and, below the separator line, the list of existing run/debug configurations:
Should you have more run/debug configurations, the list of existing ones will become broader. If you click one of the run/debug configurations in this list, it will become current. Save run/debug configurationEdit run/debug configuration
Here you can see the Solver configuration and its default settings. The default configuration options are derived from the predefined Python template. Click the Edit configuration templates link, to preview and modify the patterns.
If you change anything in one of the existing run/debug configurations, then only this particular run/debug configuration will be affected. Redirect standard input from a text fileYou can use the Run/Debug Configuration dialog to automatically submit input values from a text file instead of typing them in the Run tool window. Create the in.txt file with the following values of a, b, and c: To enable redirecting, select the Redirect input from checkbox and specify the path to the in.txt file.
Save the Run/Debug configuration and run it. Preview the execution results in the Run tool window.
Pass parameters to the running scriptWhen running your Python scripts, you can pass various values as command-line arguments. Use the Parameter field in the Python run/debug configuration to add a parameter or to insert a macro. Modify the code sample so that it can use a command-line argument value instead of «4» in the calculations. In the Run/Debug Configurations dialog, select Solver from the list of the Python run/debug configurations. Then, click + in the Parameters field and select ClipboadContent from the list of the available macros.
In this example, the clipboard contains the value 76 and it is shown in the Macro preview area. Click Insert to add the selected macro. Run the Solver run/debug configuration. Note that the command line now contains «76» as an argument. Copy any numeric value into the clipboard and rerun the configuration to evaluate the macro. Similarly, you can include other helpful macros: $FilePath$ : for the file path of the open file $ModuleSdkPath$ : for the project interpreter path $Prompt$ : for a string input dialog on running the configuration Regular runYou’ve already executed the Solver script in one of the most straight-forward ways. Let us now explore the other ways to run a script. As you have already learnt, running a script means in fact launching the current run/debug configuration. So, to run the Solver script, follow this procedure: On the main toolbar, click the run/debug configuration combobox, and make sure that ‘Solver’ run/debug configuration is the current one. Do one of the following: Now you can observe results in the Run tool window. Test runSelecting a test runner
In this case, this is Unittests. Apply changes and close the dialog. Creating a test
Click the suggested action, and PyCharm will show the following dialog:
Click OK, and see the test class opened in the editor:
PyCharm has produced a test class for us. However, this is but a stub, and lacks the actual testing functionality. So, we’ll import the class to be tested, and add a test method. The resulting code might be as follows: if you have a project directory structure, you will need to reflect that in the auto-completion of your import statement ( from Solver import Solver ). Running a testLaunch it and observe results in the Test Runner tab of the Run tool window:
Debug runFirst of all, why do we need debugging? Suppose, you hit a runtime error. How to find out its origin? This is where debugging is necessary. With PyCharm, you can debug your applications without leaving the IDE. The only thing you need to do beforehand, is to place breakpoints in the required places. Let’s explore this in details. A breakpoint is a line of the source code, where PyCharm will suspend, when this line is reached. PyCharm discerns several types of breakpoints, each one denoted with its own icon. Here we’ll use the Python line breakpoints. Setting breakpoints
Note that each breakpoint is denoted also with a red stripe over the entire line. This color corresponds to a breakpoint that has not yet been reached. Later we’ll see how the line at breakpoint changes its color. Debugging sessionSo, we are now ready for debugging. Let’s start. PyCharm starts, and then suspends execution at the first breakpoint. The line at breakpoint becomes blue. It means that PyCharm has reached the line with the breakpoint, but has not yet executed it. Next to the executed lines in the editor, the values of the variables appear. The Debug tool window appears. This tool window shows all the important information related to debugging, and allows managing the debugging process. Refer to the product documentation for details. Working in the Debugger tabOK, we’ve paused at the first breakpoint. What’s next? Refer to the section Debugger tab for details. Working in the Console tabWhy do we need it at all? For example, you’d like to see the error messages, or perform some calculations not related to the current application. With PyCharm this is not a problem. The Python prompt appears, and the console becomes interactive. Try to execute Python commands in this interactive console:
Note that interactive console provides code completion Ctrl+Space and history (Up/Down arrow keys). Refer to the page Using Debug Console for more information. You can always invoke the debug console by using the command Tools | Open Debug Command Line from the main menu. Finally, if you are used to working with an Python console, you can also do that right from within PyCharm. To launch the console, choose Tools | Python Console. from the main menu:
You can handle the script execution in the interactive mode:
Python console enables executing Python commands and scripts line by line, similar to your experience with Python Shell. Working with Python consoleThe main reason for using the Python console within PyCharm is to benefit from the main IDE features, such as code completion, code analysis, and quick fixes.
The console is available for all types of Python interpreters and virtual environments, both local and remote. Preview a variable as an arrayWhen your variables are numpy arrays or dataframes, you can preview them as an array in a separate window. To try it, do one of the following: Click the link View as Array / View as DataFrame : From the context menu of a variable, choose View as Array / View as DataFrame : The variable will be opened in the Data tab of the SciView window.
Run source code from the editor in consoleOpen file in the editor, and select a fragment of code to be executed.
Watch the code selection execution: By default, the Python console executes Python commands using the Python interpreter defined for the project. However, you can assign an alternative Python interpreter. Configure Python console settingsSelect any available interpreter from the Python interpreter list. Note that you cannot introduce a new interpreter here. If you want to come up with the new interpreter, you need to create it first. In needed, click the Configure Interpreters link to inspect the list of the installed packages and add new ones. Mind the code in the Starting script area. It contains the script that will be executed after you open the Python console. Use it to pre-code some required Python commands. When working on several Python scripts, you might want to execute each in a separate Python console. Run several Python consoles
|