Reputation: 745
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.
Upvotes: 6
Views: 10477
Reputation: 341
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,
Select a Python 3 interpreter by opening the Command Palette (Ctrl+Shift+P).
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)
Select Python Environment option showing venv path (in my case, i.e. ~/.virtualenvs/djangodev/bin/python)
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.
Re-run debug steps.
(Many thanks to Boregore for providing solution, this is just a re-elaboration of his comment to the actual question)
Upvotes: 5
Reputation: 745
The issue was that I used the "python" command instead of the "python3" command when creating the virtual environment for my project. This was causing the debugger to execute the wrong command when trying run the local server. I was able to create a new virtual environment using the command ...
python3 -m venv env
... that the Visual Studio Code debugger was able to successfully recognize when debugging using the "Python: Django" drop down configuration.
Upvotes: 1
Reputation: 205
Use the command virtualenv -p python3 venv
(or replace "venv" with your virtual environment name) in the terminal to create the virtual environment with python3 as the default when "python" is used in the terminal (e.g. python manage.py ...
).
The -p
is used to specify a specific version of python.
Upvotes: 2