Как открыть консоль в pycharm
Перейти к содержимому

Как открыть консоль в pycharm

  • автор:

Python console

Python console enables executing Python commands and scripts line by line, similar to your experience with Python Shell.

Working with Python console

The console appears as a tool window every time you choose the corresponding command on the Tools menu. You can assign a shortcut to open Python console: press Control+Alt+S , navigate to Keymap , specify a shortcut for Main menu | Tools | Python or Debug 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.

Code completion in the Python console

You can use up and down arrow keys to browse through the history of executed commands, and repeat the desired ones. To preview the variable values calculated in the course of the execution, click and check the Special Variables list.

Show variables in the Python console

The console is available for all types of Python interpreters and virtual environments, both local and remote.

View a variable as an array or a data frame

Viewing data frames

  • When viewing variables in the Python Console , you can click View as DataFrame or View as Series links to display the data in a separate editor tab.

Run source code from the editor in console

  1. Open file in the editor, and select a fragment of code to be executed.
  2. From the context menu of the selection, choose Execute Selection in Python Console , or press Alt+Shift+E : Context menu for executing the code selectionWith no selection, the command changes to Execute line in console . Choose this command from the context menu, or press Alt+Shift+E . The line at caret loads into the Python console, and runs.
  3. Watch the code selection execution: Execution of the code selection in the console

By default, the Python console executes Python commands using the Python interpreter defined for the project. However, you can assign an alternative Python interpreter.

Run asyncio coroutines

  1. In the editor, select a fragment of code which contains the definition of an asyncio coroutine.
  2. From the context menu, select Execute Selection in Python Console , or press Alt+Shift+E : Selecting and executing asyncio coroutines in the Python console
  3. After the code is executed on the Python console, run the coroutine by using the await keyword: Execution of the asyncio coroutine in the console

Configure Python console settings

Search an alternative Python interpreter

  1. In the Settings dialog ( Control+Alt+S ), select Build, Execution, Deployment | Console | Python Console .
  2. 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.
  3. 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

Rename the Python console

  1. Click to add a new Python console.
  2. By default, each console has the name Python Console with an index. To make a console reflect the script you’re running, right-click the console tab, select Rename Console , and enter any meaningful name.

All the commands you’re running in the Python console are executed one by one. If the commands require substantial time to get executed, you might want to preview and manage the execution queue.

Manage the command execution queue

Console command queue

  1. Go to Settings | Build, Execution, Deployment | Console and enable the Command queue for Python Console checkbox.
  2. Click on the console toolbar to open the queue.
  3. In the Python Console Command Queue dialog, review the list of commands. If needed, click to delete the command from the queue.

Note, once the command is executed, it disappears from the queue. To preview all previously executed commands browse the console history ().

Terminal emulator

PyCharm includes an embedded terminal emulator for working with your command-line shell from inside the IDE. Use it to run Git commands, set file permissions, and perform other command-line tasks without switching to a dedicated terminal application.

Enable the Terminal plugin

This functionality relies on the Terminal plugin, which is bundled and enabled in PyCharm by default. If the relevant features aren’t available, make sure that you didn’t disable the plugin.

  1. Press Control+Alt+S to open the IDE settings and then select Plugins .
  2. Open the Installed tab, find the Terminal plugin, and select the checkbox next to the plugin name.

Initially, the terminal emulator runs with your default system shell, but it supports many other shells, such as Windows PowerShell, Command Prompt cmd.exe , sh , bash , zsh , csh , and so on. For more information about changing the shell, refer to Terminal settings.

Open the Terminal tool window

  • Go to View | Tool Windows | Terminal or press Alt+F12 .

Right-click a project item in the Project tool window and choose Open in terminal from the context menu. This way the terminal will start with the directory corresponding to the selected item.

You can open the terminal as an editor tab: right-click the Terminal tool window header and select Move to Editor .

Start a new local session

New session in the terminal

  • To start a new session in a separate tab, click on the toolbar or press Control+Shift+T .

To run multiple sessions inside a tab, right-click the tab and select Split Right or Split Down in the context menu.

The Terminal saves tabs and sessions when you close the project or PyCharm. It preserves tab names, the current working directory, and even the shell history.

To close a tab, click on the Terminal toolbar or press Control+F4 .

Press Alt+ArrowRight and Alt+ArrowLeft to switch between active tabs. Alternatively, you can press Alt+ArrowDown to see the list of all terminal tabs.

Configure shortcuts for the terminal actions in the IDE settings ( Control+Alt+S ), under Keymap | Plugins | Terminal .

Start a new SSH session

Start a new SSH session

  1. On the toolbar, click .
  2. Click New SSH Session , enter the address of a host to which you want to connect, and provide authentication data. Or, if you have configured SSH configurations, you can select one of them from the list.

To terminate the connection, click in the terminal tab.

Rename terminal tab

  • Right-click the tab and select Rename Session from the context menu.

Just like with system terminal tabs, you can rename PyCharm Terminal tabs programmatically. For example, use title MyTitle if your interpreter is Windows Command Prompt or echo -en «\033]0;MyTitle\a» for bash and zsh. This feature is available if the Show application title checkbox is selected in Advanced Settings.

Search in terminal

  • To search for a certain string in a Terminal session, press Control+F . This searches all text in the session: the prompt, commands, and output.

By default, the search is not case-sensitive. You can click Match case in the search field to make it case-sensitive.

Configure the terminal emulator

  • Press Control+Alt+S to open the IDE settings and then select Tools | Terminal . For more information, refer to Terminal settings.

For advanced settings, select Advanced Settings and scroll down to the Terminal group. For more information, refer to Advanced Settings: Terminal.

Query consoles

This functionality relies on the Database Tools and SQL plugin, which is bundled and enabled in PyCharm by default. If the relevant features aren’t available, make sure that you didn’t disable the plugin.

The Database Tools and SQL plugin is available only in PyCharm Professional.

  1. Press Control+Alt+S to open the IDE settings and then select Plugins .
  2. Open the Installed tab, find the Database Tools and SQL plugin, and select the checkbox next to the plugin name.

Overview

Query or database consoles are SQL files that are attached to a data source. You can write and execute SQL statements in query consoles the same way as you do it in terminal.

When you create a data source, the data source’s default query console is created automatically. If necessary, you can create additional query consoles.

Query console

Each console creates a new connection session. If you do not want to create new connections, enable single session mode.

All the created query consoles are located in the internal Database Consoles directory. To open this folder, open the Project tool window ( Alt+1 ) and navigate to Scratches and Consoles | Database Consoles . On your machine, the query console files are stored in the consoles subdirectory of the IDE configuration directory.

For more information about working with query results in query consoles, refer to Query results.

Database connection session

For each console, a database connection session is created automatically. You can create new connection sessions and attach your console to existing ones in the console toolbar.

In the following example, the PostgreSQL console is associated with the guest.public schema of a data source and connected to it via the console connection session. You can change the console session to actor or create a new session and attach the console to it.

Change the connection session

Code editor

The code editor is where you compose your SQL statements using the resolve modes and coding assistance features, and execute them against the associated data source.

Find the code editor toolbar controls in Code editor controls. Read more about the editor in Editor basics.

SQL statement execution

When you execute a statement, the Services tool window opens. The Services tool window displays available connection sessions, Output and Result tabs. For more information about the Services tool windows, refer to Services tool window.

  • If the executed statement retrieves data (for example, SELECT ), results are displayed in the Result tab that has a title of a qualified table name. For more information about creating custom titles for result tabs, refer to Use custom titles for tabs with results.
  • If the executed statement does not retrieve data, results are displayed in the Output tab.

Query console with an active Result tab of Services tool window

  1. Query console tab toolbar.
  2. Code editor.
  3. Services tool window.
  4. Output and Result tabs. Result tab is active.
  5. Result tab toolbar.

Query console with an active Output tab of Services tool window

  1. Query console tab toolbar.
  2. Code editor.
  3. Services tool window.
  4. Output and Result tabs. Output tab is active.
  5. Right toolbar of the Output tab.

Work with console files

Create a query console

To create a console, use one of the following actions in the Database tool window ( View | Tool Windows | Database ).

  • Click a data source and select File | New | Query Console .
  • Right-click a data source and select New | Query Console .
  • Click a data source, press Alt+Insert , and select Query Console .
  • Click a data source, press Control+Shift+F10 , and select New Query Console .
  • Click a data source and press Control+Shift+Q .

New console means a new connection to a data source. You can work with just one connection for the data source and have all consoles use that same one connection. Read more about the single session mode in Single session mode.

Open a console

When a console is open, you can write SQL here, run it and get the result.

Open a console

  • In the Project tool window ( View | Tool Windows | Project ) , navigate to Scratches and Consoles | Database Consoles | , double-click a console file.
  • In the Database tool window ( View | Tool Windows | Database ) , click the Jump to Query Console… button () and select a console that you want to open or create a new one.
  • Click a data source, press F4 , and select the console that you need.

Rename a query console

To rename a query console file, do one of the following:

Rename a query console file

  • Right-click a console tab, and select Rename File .
  • Open the Project tool window ( View | Tool Windows | Project ) and navigate to Scratches and Consoles | Database Consoles . Expand a data source group that includes your console, right-click the console file, and select Refactor | Rename ( Shift+F6 ).

Group consoles under the data source directory

Group consoles under the data source directory

  1. In the Project tool window ( View | Tool Windows | Project ) , navigate to Scratches and Consoles | Database Consoles .
  2. Right-click a data source group where you want to create a new directory, select New | Directory .
  3. Specify a name of the directory.
  4. Select and drag console files to the directory.
  5. In the Move dialog, click Refactor .

Delete a query console

  1. In the Project tool window ( View | Tool Windows | Project ) , navigate to Scratches and Consoles | Database Consoles .
  2. Expand a data source group that includes your console.
  3. Right-click a console file and select Delete .

Copy a console file to a new location

Copy a console file to a new location

  1. In the Project tool window ( View | Tool Windows | Project ) , navigate to Scratches and Consoles | Database Consoles .
  2. Expand a data source group that includes your console.
  3. Right-click a console file and select Refactor | Copy File… .
  4. In the To directory field, specify a new location for the file.

Open a console file in the file browser

To open a console file in your file browser, do one of the following:

Open a console file in the file browser

  • Right-click a console tab and navigate to Open In | .
  • Open the Project tool window ( View | Tool Windows | Project ) and navigate to Scratches and Consoles | Database Consoles . Expand a data source group that includes your console, right-click the console file, and select Open In | .

Configure console settings

Change a schema

Change a schema

  • In the query console toolbar, open the list of databases and schemas by clicking the button and select the one that you want to run queries against. For more information about schemas, refer to Schemas.

Modify console settings

  • Open a console and click the Open Query Execution Settings. button ().

Configure console dialects

Change the SQL dialect in the code editor

For query consoles, SQL dialect is defined by a data source that is attached to the console. It can only be changed for the Oracle consoles.

  • Right-click the editing area and select Change Dialect () to . In the Change SQL Dialect window, select a dialect.

Productivity tips

View history of a query console

  • In a console, right-click any area and select Local History | Show History .

Apply a code style in the editor

Apply a code style in the editor

  • Right-click any area or a selection of code in the editor and select Reformat Code . Alternatively, press Control+Alt+L .

View the file path of a query console

  • To see the details of a file, hover over the console tab.

Navigate to a console file from the Select In window

  • Press Alt+F1 , select Project View and press Enter .

Edit data in INSERT statements as a table

Edit data in INSERT statements as a table

  1. Select INSERT statements that you want to edit.
  2. Right-click the selection and click Edit as Table .
  3. In the table editor that opens, make necessary changes in the table cell. Press Enter to confirm.

Context menu

In the code editor context menu, use the following actions to increase your productivity:

In INSERT statements, opens the editor for working with the data in a table format.

Change the SQL dialect. Select a dialect from the list.

  • Explain Plan : show an execution plan (or explain plan) for the current statement. The result is shown in a mixed tree/table format on a dedicated Plan tab. To build a diagram of the execution plan, click the Show Diagram icon () on the left of the Plan tab, or press Control+Alt+Shift+U . To build a flame graph, click the Flame Graph button ().
  • Explain Plan (Raw) : show an execution plan (or explain plan) for the current statement. The result is shown in table format. Technically, EXPLAIN or similar statement is executed.

Execute the current statement or the sequence of selected statements.

Execute to File

Execute the current statement and save results in a text file. Select the output format and specify the file location and name.

Run ‘console [data_source]’

Execute all the statements in the query console.

Query console reference

Use the code editor to compose and execute your SQL statements as well as to perform other associated tasks.

Read more about the editor in Editor basics.

Toolbar controls

Action and shortcut

Execute selected SQL statement or statements.

If nothing is selected, the current statement is executed.

Browse Query History

Open a dialog that shows all the statements that you have run for the corresponding data source.

Open or close the Parameters dialog.

Open Query Execution Settings…

Open the Tools | Database | Query Execution section of the Settings dialog to view or edit the query execution settings.

For more information about these settings, refer to Query Execution.

Select the isolation level for database transactions and the way the transactions are committed.

  • Auto : the current transaction is committed automatically when you submit your local changes to the database server.
  • Manual : the changes submitted to the database server are accumulated in a transaction that can either be committed or rolled back explicitly by means of the Submit and Commit () or Roll Back () buttons on the toolbar.

For more information about database transaction modes and isolation, refer to Submit changes to a database.

(For the Manual transaction mode.) Commit the current transaction. This button is available only for the manual transaction mode. See also, transaction mode and isolation.

(For the Manual transaction mode.) Roll back changes. This button is available only for the manual transaction mode. See also, transaction modes and isolation.

Cancel Running Statements

Terminate execution of the current statement or statements.

File Resolve Mode

Select the resolve mode to manage the context that database objects in your code are resolved to.

  • Playground : default mode for query consoles. All the database objects are resolved to the same context.
  • Script : default mode for local SQL files. The USE SQL statement ( SET search_path TO for PostgreSQL) overrides the default context that the database objects are resolved to.

For more information about the resolve modes, refer to Resolve modes.

In the following video, the public schema is selected as default for the query console. The schema contains the actor table, whereas the information_schema schema does not contain it. When the resolve mode is Playground , the database object is resolved to default schema in both SELECT statements, because all the statements are considered unrelated.

Once the resolve mode is changed to Script , the second SELECT statement is considered related to the SET search_path TO information_schema one. The actor table is no longer resolved in that statement.

Toggle the display of query result within the code editor of query console.

For more information about the in-editor results, refer to In-Editor Results.

Switch current schema

Select the default schema or database. For PostgreSQL, Amazon Redshift, and Greenplum use the list to form the schema search path.

Select a search path for PostgreSQL

For more information about schemas, refer to Schemas.

Switch attached session

Select the database connection session.

For more information about the database connection sessions, refer to Managing connection sessions

Как на PyCharm открыть консоль сверху на правой стороне?

Author24 — интернет-сервис помощи студентам

Как называется область видимости в правой стороне VS 2017?
Что бы мельком весь код видеть? Как она называется и где ее можно активировать?

Как выравнить текст в combobox не по левой а по правой стороне
Здравствуйте. Вопрос, что уж говорить, приведен выше. Вставление пробелов не предлагать, это не.

Как открыть консоль или отправить в консоль другого приложения какой то текст
Мне нужно отправить в консоль только что открытого приложения какой то текст как это сделать.

Background по правой стороне
Весь сайт смещен немного на левую сторону (так задумано). Чтобы не оставлять просто пустое место.

Как открыть несколько проектов в pycharm
Доброго времени суток! Есть необходимость открыть сразу несколько проектов, разложенных по папкам.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *