Notes on PyCharm

This page is managed by StJohn Piano.

Notes on PyCharm

If you have any questions, comments, corrections, or suggestions - please contact StJohn Piano on Tela:‌‌tela.app/id/stjohn_piano/7c51a6


Background

  • A "caret line" is a line on which there is an active cursor.

Cursors

  • To add another cursor, use Opt-Click.
  • Press Opt twice and hold, then press down arrow = create multiline cursor at the same column location.

Search Everywhere

Tap Shift twice i.e. Shift+Shift (⇧⇧ on macOS) to open the Search Everywhere dialogue.

Include stdout in pytest summary

Run / Edit Configurations / Python tests / [pytest config] / Additional Arguments

Set to "--capture=no"

Include warnings in pytest output

Settings / Advanced Settings

  • Tick: [Pytest: do not add "--no-header --no-summary -q"]

Note: When the same warning occurs multiple times, only the first occurrence is displayed in the warning summary.

Show all tabs in main window

Settings / Editor / General / Editor Tabs / Show tabs in

Select "Multiple rows"

Switch to previous / next tab

Command + Shift + Left / Right

Activate license

  • Purchase license.
  • In PyCharm, log in with your JetBrains account.
  • Help / Register / Activate New License
  • Get license from: JetBrains Account
  • Click "Refresh license list"

Trim whitespace on Save

Settings / Editor / General / On Save

  • Remove trailing spaces on / All lines
  • Keep trailing spaces on caret line
  • Remove trailing blank lines at the end of saved files
  • Ensure every saved file ends with a line break

Move cursor to beginning / end of file

Mac: Cmd + Home, Cmd + End

Close current tab

Mac: Cmd + W

Reopen last closed tab

Right-click any tab and select Reopen Closed Tab from the context menu.

Soft wrap Python files automatically

  • Settings / Editor / General / Soft Wraps
  • Soft-wrap these files: *.md; *.txt; *.rst; *.adoc; *.py; *.yml‌‌
    Note: Use * and ? as wildcards and ; to separate patterns.

Soft wrap output in Run window

On the right-hand side of the Run window, below the up and down arrow buttons, there is a Soft-Wrap toggle button.

Set default Python interpreter for new projects

File / New Projects Setup / Settings for New Projects...

  • Python Interpreter (on the left navbar)
  • If you have used pyenv to install a new version, you can click Add Interpreter / Add Local Interpreter and set the base interpreter to e.g. ~/.pyenv/versions/3.10.13/bin/python3.10

Create a virtualenv for a project

Pycharm / Settings

  • Project: [name] / Python Interpreter
  • Select base interpreter, and click Add Interpreter / Add Local Interpreter

If your requirements.txt includes Github repos with GITHUB_TOKEN in the URL, you'll need to install via pip install -r requirements in the cli with the virtualenv activated.

Join lines

Highlight a group of lines.

Mac: Ctrl-Shift-J

Change case

To toggle between upper case and lower case:

  1. Select text fragment, or just place the caret at the line you want to change case in.
  2. Press ⌘Сmd ⇧Shift U or choose Edit | Toggle Case in the main menu.
  • CamelCase names are converted to the lower case.
  • Lowercase names are converted to the uppercase format.
  • Uppercase names are converted to the lower case.

CSV files

  • Open a .csv file. In the bottom left-hand corner, there are two toggles called "Text" and "Data". The "Text" view is the default and is the normal file editor. The "Data" view will show you the CSV in an editable table format.

Python imports

  • Option-Ctrl-o = Clean up Python imports
  • If the caret is on an variable, which needs to be imported but is not, then press Opt-Enter and select "import".

Sources

https://www.jetbrains.com/help/pycharm/using-code-editor.html‌‌

https://www.jetbrains.com/help/pycharm/multicursor.html

https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html

https://www.jetbrains.com/help/pycharm/working-with-source-code.html

https://www.jetbrains.com/help/rider/Toggling_Case.html

https://www.jetbrains.com/help/pycharm/creating-and-optimizing-imports.html#optimize-all