PyCharm tips

View print output immediatly

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.

Autoload .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](http://direnv.net/) 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.
    layout_pipenv
    # Auto load .env file
    dotenv
    
  • Add this to the activate script:

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

Use shell_plus in the integrated console

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

# Load env var
import environ
environ.Env.read_env('.env')

import sys; print('Python %s on %s' % (sys.version, sys.platform))
import django; print('Django %s' % django.get_version())
sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])
if 'setup' in dir(django): django.setup()
import django_manage_shell; django_manage_shell.run(PROJECT_ROOT)
from django_extensions.management import shells
from django.core.management.color 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())
g.update(objects_to_import)

Pages

blogroll

social