Pycharm notes
From raju
Note:- This page will be migrated to http://www.kamaraju.xyz/dk/pycharm_notes. Once that is done, it will be deleted.
Current configuration
my favorite configuration
if version == 2019.3.3:
- File -> Settings -> Appearance & Behavior -> Appearance -> Theme -> IntelliJ
else:
- File -> Settings -> Appearance & Behavior -> Appearance -> UI Options -> Theme -> IntelliJ
- File -> Settings -> Appearance & Behavior -> System Settings -> Project Opening -> select "Open project in new window"
- scrollbar -> right click -> Customize Highlighting Level -> drag it to Syntax
- File -> Settings -> Editor -> General -> Editor Tabs -> Tab Closing Policy -> Tab limit - 20 -> OK
useful shortcuts
go to the location of current file in the project tool window
Alt+F1 -> 1
view just the functions
Ctrl+F12
Ref:- https://www.jetbrains.com/help/pycharm/viewing-structure-of-a-source-file.html
tags | show code outline
To go back after control clicking
- Ctrl+Alt+Left arrow, Ctrl+Alt+Right arrow - to go back and forward
- Ctrl+Tab - Switch to the last (most recent) tab -- similar to how Alt+Tab works
Ref:- https://stackoverflow.com/questions/25103626/in-pycharm-how-to-go-back-to-last-location
tags | coming back after navigating to a function
Jump to next/previous function
- Open a file already opened in another tab - Ctrl+Tab
- Go to the right or to the left tab - Alt+Right or Alt+Left
- Go back and forth through the history of visited tabs - Ctrl+Alt+Left, Ctrl+Alt+Right
view function definition
- Ctrl+Shift+I
- Ctrl+left click
- https://www.jetbrains.com/help/pycharm/viewing-definition.html
folding
- Ctrl+NumPad Plus - Expand the current fold
- Ctrl+NumPad - - Close the current fold
- Ctrl+Shift+Plus - open closed folds
Ref: https://www.jetbrains.com/help/pycharm/code-folding.html
tags | expand import stanza by keyboard
editing
- duplicate current line - Ctrl+d . Documented in https://www.jetbrains.com/help/pycharm/adding-deleting-and-moving-code-elements.html#d1293e39
switch between open projects
- Window | Next Project Window Ctrl+Alt+]
- Window | Previous Project Window Ctrl+Alt+[
- Window | <ProjectNameOrLocation>
project tool window
formatting
- format the whole file - ctrl+alt+L
- format a selection - ctrl+alt+l
Ref:- https://www.jetbrains.com/help/pycharm/reformat-and-rearrange-code.html
tags | realign selected code
change indentation
- https://www.jetbrains.com/help/pycharm/changing-indentation.html - indent or deindent a block
- indent/deindent selected code - tab/shift+tab
multiple selections
To select multiple blocks of text
select first block normally press ALT key for all the subsequent blocks.
dummy
run git bash in the terminal
- File -> Settings -> Tools -> Terminal -> Shell path = "C:\Program Files\Git\bin\bash.exe" --login
The quotes are important
add new python interpreter
tags | python interpreter not available in the run configuration
Click the Project Interpreter widget in the lower right hand corner of pycharm window.
See https://www.jetbrains.com/help/pycharm/guided-tour-around-the-user-interface.html for a pic. It is part of the status bar which is at the bottom.
-> Add Interpreter
change python console interpreter
File -> Settings -> Build, Execution, Deployment -> Console -> Python Console -> select the Python interpreter from the dropdown.
Ref:- https://www.jetbrains.com/help/pycharm/console-python-console.html
go to a line
Use Ctrl+g
sort after
- search for whole words - \bfoo\b
Ref:- https://www.jetbrains.com/help/pycharm/regular-expression-syntax-reference.html
troubleshooting
- "tput: terminal attributes: No such device or address" - https://stackoverflow.com/questions/42836245/tput-terminal-attributes-no-such-device-or-address-in-windows-pycharm-debugger
Change PYTHONPATH
Run -> Edit configurations -> Environment variables -> click the three dots icon
Add something like
PYTHONPATH c:\path\to\dir1;d:\path\to\dir2
Send output from a run to a file
Edit Configurations -> Logs tab -> check "Save console output to file" -> provide the full path of the output file.
This will redirect both standard output and standard error. On subsequent runs, it overwrites the previous file.
missing features
- Pycharm does not correctly hightlight a multiline todo item inside the comments. It assumes that a todo item is always just a single line.
Ref:- https://stackoverflow.com/questions/48645942/how-do-i-format-a-multi-line-todo-comment-in-pycharm
- If you have a split screen, it is not possible to click on a function call and have its definition open in the other split screen.
tags | open function definition in a different split
functions are not clickable
tags | cannot find function, not able to find imported functions, python path files in another directory, sources root, control click, functions are not clickable
Add appropriate directory as a content root
File -> Settings -> Project: foo -> Project Structure -> Add Content Root
See also:
- https://www.jetbrains.com/help/pycharm/configuring-content-roots.html
- https://www.jetbrains.com/help/pycharm/content-root.html
- https://www.jetbrains.com/help/pycharm/accessing-project-structure.html
convert single quotes docstrings to double quoted docstrings
Code -> Inspect Code -> Whole project -> Python ... -> Convert docstring to the triple double-quoted string form
key words
- navigation bar
wrap lines
View -> Active Editor -> Soft-Wrap
Ref:- https://stackoverflow.com/questions/38585204/pycharm-to-toggle-soft-wrap-in-code-editor
useful videos
- https://www.youtube.com/watch?v=QYO5_riePOQ - Top 15 IntelliJ IDEA shortcuts (2020) by Trisha Gee.
Tips
block selection
- You can easily make column selection by dragging your mouse pointer while keeping the Alt key pressed.
How can I
show all unresolved references
Code -> Inspect Code