Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code Is Not Able To Launch Django Project From Debugger

I am using Visual Studio Code as my IDE for building web applications using Python's Django web development framework. I am developing on a 2018 MacBook Pro. I am able to launch my web applications by launching them in the terminal using:

python3 manage.py runserver

However, I want to be able to launch my application through the debugger. To try and do this, I navigated to the debug section, created the launch.json file, and changed my configuration in the drop down to Python: Django. Here is are my configurations from the file.

    {
        "name": "Python: Django",
        "type": "python",
        "request": "launch",
        "program": "${workspaceFolder}/manage.py",
        "console": "integratedTerminal",
        "args": [
            "runserver",
            "--noreload",
            "--nothreading"
        ],
        "django": true
    },

When I try to run the debugger using the green play arrow, I get the following exception:

Exception has occurred: ImportError Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment? File "/Users/justinoconnor/Desktop/Rapid Prototyping/Projects/hello_django/manage.py", line 14, in ) from exc

Launching the VS Code debugger with this configuration should be the same as running python manage.py runserver --noreload --nothreading, but it is not working. I'm thinking it is because on the MacBook I have to use the "python3" command rather than "python", but I did not see anything in the documentation that would allow me to specify this in the launch.json configuration file.

Does anyone know how to resolve this so that when I run the debugger it automatically executes/saves my project? I don't understand why this is not working when I can type python3 manage.py runserver into the terminal and it will execute just fine.

like image 648
Justin Avatar asked Nov 21 '18 23:11

Justin


People also ask

How do I run an existing Django project in Visual Studio code?

In Visual Studio create a new project and select Python -> From Existing Python code . For Location choose the folder of your existing Django project. Clicking Ok will open the "Create New Project from Existing Python Code Wizard" screen. Your existing folder should already be entered, so click Next .

How do I enable Python debugger in Visual Studio code?

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.


1 Answers

Same issue caused to my VS Code environment even launching VS Code after activating venv (Python virtual environment).

The VS Code also displayed the Python Environment option "Python 3.7.3 64 bit" on the Status Bar. On first sight, this python environment option looks correct.

But, my issue resolved after applying Boregore's comment. Python Environment option related with venv python needs to be selected as an interpreter.

I selected the correct Python Environment option related with venv (in my case i.e. ~/.virtualenvs/djangodev/bin/python), by applying following steps,

  1. Select a Python 3 interpreter by opening the Command Palette (Ctrl+Shift+P).

  2. Start typing the Python: Select Interpreter command to search, then select the command. You can also use the Select Python Environment option on the Status Bar if available (it may already show a selected interpreter, too)

  3. Select Python Environment option showing venv path (in my case, i.e. ~/.virtualenvs/djangodev/bin/python)

  4. Now, VS Code displays the Python Environment option related with venv, (in my case, i.e. "Python 3.7.3 64 bit ('djangodev': venv)" on the Status Bar.

  5. Re-run debug steps.

(Many thanks to Boregore for providing solution, this is just a re-elaboration of his comment to the actual question)

like image 86
Divyarajsinh Jadeja Avatar answered Oct 08 '22 01:10

Divyarajsinh Jadeja