PyCharm tips

Posted on 2019-03-10 in Trucs et astuces

View print output immediately

Add PYTHONUNBUFFERED=1 in run config or .env (only use .env if you use the plugin). This is meant to immediately view the output of the print function without the need for flush=True.

Auto-load .env file

  • Install the EnvFile and .env file support plugins. Then configure the Django server to run with the custom command runserver_plus (optional) and in the EnvFile tab, tick Enable EnvFile and in the box below, add you .env file. Do the same for pytest.

  • Use [direnv]( to autoload the .env. To do this, create a .envrc file with the content below and run direnv allow . to allow the .envrc file to be loaded:

    # Auto load venv.
    # Auto load .env file
  • Add this to the activate script:

if [[ "${TERMINAL_EMULATOR}" == "JetBrains-JediTerm" && -f .env ]]; then
    load-dotenv .env

Use shell_plus in the integrated console

Use this starting script in Build, Execution, Deployment > Console > Django console:

# Load env var
import environ

import sys; print('Python %s on %s' % (sys.version, sys.platform))
import django; print('Django %s' % django.get_version())
if 'setup' in dir(django): django.setup()
import django_manage_shell;
from import shells
from import color_style

# Default settings for shell_plus
shell_plus_default_settings = {
    'bpython':         False,
    'connection_file': None,
    'dont_load':       [],
    'ipython':         True,
    'kernel':          False,
    'no_browser':      False,
    'no_color':        False,
    'notebook':        False,
    'plain':           False,
    'print_sql':       False,
    'ptipython':       False,
    'ptpython':        False,
    'pythonpath':      None,
    'quiet_load':      False,
    'settings':        None,
    'traceback':       False,
    'use_pythonrc':    False,
    'verbosity':       1,
    'vi_mode':         False,
    'dont_load':       [],

g = globals()
objects_to_import = shells.import_objects(shell_plus_default_settings, color_style())

Make sure commit hooks run in a venv

You can use this script:

#!/usr/bin/env bash

set -eu

readonly INSIDE_DOCKER=$(grep -q docker /proc/self/cgroup && echo true)
readonly command="$1"
# To be sure $@ will only contain the parameters of the command, not the command itself.

# If we are already in a venv or if the are in docker, run directly.
if [[ -v VIRTUAL_ENV || "${INSIDE_DOCKER}" == "true" ]]; then
    echo "Already in venv"
    ${command} "$@"

    exit $?
    # Load bashrc to be sure PATH is correctly set. We can't do this before this it could mess up with the enabled venv.
    # We don't want to fail if the bashrc file contains unbound variables.
    set +u
    source ~/.bashrc || echo "Bash RC file not found"
    set -u
    # If not and pipenv is installed and we have a Pipfile, run with pipenv.
    if command -v pipenv > /dev/null && [[ -f Pipfile ]]; then
        echo "Running in venv with pipenv"
        pipenv run "${command}" "$@"

        exit $?
    # If poetry is installed and we have a pyproject.toml, run with poetry.
    elif command -v poetry > /dev/null && [[ -f pyproject.toml ]]; then
    echo "Running in venv with poetry"
    poetry run "${command}" "$@"
        echo "Not in an virtual env and don't know how to run in one." >&2
        exit 1

Then, if you use pre-commit, you can do something like this:

- id: flake8
  name: flake8
  entry: ./bin/
  exclude: '/snapshots/.*\.py$'
  args: [flake8]
  language: system
  types: [python]