Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

PyCharm

PyCharm

PyCharm Intro

  • IDE - Integrated Development Environment
  • Introspection (understands Python)
  • Running, Debugging
  • Refactoring

PyCharm configure interpreter

  • Mac: PyCharm / Preferences / Project: (name) / Project Interpreter
  • Windows/Linux: File / Settings / Project Interpreter

PyCharm install modules

  • Same place where we set the interpreter

PyCharm Project

  • At the opening create a new project (directory + Python version)
  • File/New Project

PyCharm Files

  • New file
  • Open file
  • Ctrl-Shift-N

PyCharm - run code

  • Run/Run
  • Set command line parameters
  • Set environment variables
import sys
import datetime
import random

def main():
    limit = get_limit()
    print(limit)

    date = datetime.datetime.now()

    rnd = random.randrange(2, 6)
    print(rnd)

    count(limit)
    print("after count")

def get_limit():
    limit = 10
    if len(sys.argv) == 2:
        limit = int(sys.argv[1])
    return limit

def count(limit):
    for ix in range(limit):
        div = ix - 12
        show(ix, ix / div)

def show(number, result):
    print(number, result)


if __name__ == '__main__':
    main()

PyCharm - debugging code

  • Set fixed Breakpoints (click on line next to row-number to have a red circle)
  • Run/Debug
  • Inspect variables
  • Conditional breakpoint
  • Step in function
  • Step out of function
  • Step over function

PyCharm Terminal

  • Bottom "Terminal"

PyCharm Python console at the bottom left

  • Bottom "Python Console"
 2 + 3
 x = 2
 print(x)
 def f(x, y):
    return x+y

 f(4, 5)

Refactoring example with PyCharm

  • Change variable name (in scope only)
def add(x, y):
    z = x + y
    return z

def multiply(x, y):
    z = x * y
    return z

x = 2
y = 3
z = add(x, y)
print(z)

z = multiply(x, y)
print(z)
  • Extract method