Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JeancarloBarrios/92df21fd75ece704cc29ab4592ee5188 to your computer and use it in GitHub Desktop.
Save JeancarloBarrios/92df21fd75ece704cc29ab4592ee5188 to your computer and use it in GitHub Desktop.

Small Tutorial Setup for Python Debugger with Pycharm/VS Code

Prerequirements

  1. Create Virtual Enviorment
# Linux
sudo apt-get install python3-venv    # If needed
python3 -m venv .venv
source .venv/bin/activate

# macOS
python3 -m venv .venv
source .venv/bin/activate

# Windows
py -3 -m venv .venv
.venv\scripts\activate

Pycharm

Open Project in Pycharm

  1. Launch PyCharm.
  2. Click on File > Open....
  3. Navigate to your existing Django project folder and click OK.

PyCharm will load the Django project and index the files, which may take a few minutes for large projects.

Configure Python Interpreter

  1. Go to File > Settings > Project: your_project_name > Python Interpreter.
  2. Click on the gear icon and choose Add....
  3. In the left pane, select Existing environment.
  4. Click on the ... button to browse to your Python interpreter. It's usually located in the venv/bin/ folder inside your project folder.
  5. Click OK to close the dialogs.

This sets the Python interpreter for your Django project.

Configure Pycharms settings

  1. Go to File > Settings > Languages & Frameworks > Django.
  2. Check the Enable Django Support box.
  3. In the Django project root field, enter the directory which contains manage.py.
  4. In the Settings field, enter the location of your settings.py file, usually your_project_name/settings.py.
  5. Click OK to close the dialog.

This allows the IDE to find django setup files

Finally

After giving it a minut you will be able to run the debugger You should be able to use the debugger which is on the upper right corner

VS Code

Open and Confiugre interpreter

  1. Open the project folder in VS Code by running code ., or by running VS Code and using the File > Open Folder command.
  2. In VS Code, open the Command Palette (View > Command Palette or (⇧⌘P)). Then select the Python: Select Interpreter command:
  3. The command presents a list of available interpreters that VS Code can locate automatically (your list will vary; if you don't see the desired interpreter, see Configuring Python environments). From the list, select the virtual environment in your project folder select the enviorment
  4. Switch to Run view in VS Code (using the left-side activity bar or F5). You may see the message "To customize Run and Debug create a launch.json file". This means that you don't yet have a launch.json file containing debug configurations. VS Code can create that for you if you click on the create a launch.json file link
  5. Select the link and VS Code will prompt for a debug configuration. Select Django from the dropdown and VS Code will populate a new launch.json file with a Django run configuration. The launch.json file contains a number of debugging configurations, each of which is a separate JSON object within the configuration array.
  6. Choose the Django configuration
{
    "name": "Python: Django",
    "type": "python",
    "request": "launch",
    "program": "${workspaceFolder}/manage.py",
    "args": [
        "runserver",
    ],
    "django": true
},
  1. Test the configuration by selecting the Run > Start Debugging menu command, or selecting the green Start Debugging arrow next to the list (F5):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment