简体   繁体   English

Visual Studio代码无法从调试器启动Django项目

[英]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. 我使用Visual Studio Code作为使用Python的Django Web开发框架构建Web应用程序的IDE。 I am developing on a 2018 MacBook Pro. 我正在开发2018 MacBook Pro。 I am able to launch my web applications by launching them in the terminal using: 我可以使用以下命令在终端中启动Web应用程序:

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. 要尝试执行此操作,我导航到debug部分,创建了launch.json文件,然后在下拉列表中将我的配置更改为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. 发生异常:ImportError无法导入Django。 Are you sure it's installed and available on your PYTHONPATH environment variable? 您确定已在PYTHONPATH环境变量中安装并使用该变量吗? Did you forget to activate a virtual environment? 您是否忘记了激活虚拟环境? File "/Users/justinoconnor/Desktop/Rapid Prototyping/Projects/hello_django/manage.py", line 14, in ) from exc 来自“ exc”的文件“ / Users / justinoconnor / Desktop / Rapid Prototyping / Projects / hello_django / manage.py”,第14行

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. 使用此配置启动VS Code调试器应与运行python manage.py runserver --noreload --nothreading相同,但无法正常工作。 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. 我在想是因为在MacBook上我必须使用“ python3”命令而不是“ python”,但是我在文档中没有看到任何允许我在launch.json配置文件中指定它的内容。

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. 我不明白为什么当我可以在终端中输入python3 manage.py runserver并执行就可以了,为什么这不起作用。

The issue was that I used the "python" command instead of the "python3" command when creating the virtual environment for my project. 问题是在为我的项目创建虚拟环境时,我使用了“ python”命令而不是“ python3”命令。 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. ……使用“ Python:Django”下拉配置进行调试时,Visual Studio Code调试器能够成功识别。

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 (eg python manage.py ... ). 在终端中使用命令virtualenv -p python3 venv (或用您的虚拟环境名称替换“ venv”),以在终端中使用“ python”(例如python manage.py ... )。

The -p is used to specify a specific version of python. -p用于指定特定版本的python。

Same issue caused to my VS Code environment even launching VS Code after activating venv (Python virtual environment). 甚至在激活venv(Python虚拟环境)后启动VS Code的情况下,我的VS Code环境也遇到了同样的问题。

The VS Code also displayed the Python Environment option "Python 3.7.3 64 bit" on the Status Bar. VS代码还在状态栏上显示了Python环境选项“ Python 3.7.3 64 bit”。 On first sight, this python environment option looks correct. 乍一看,此python环境选项看起来正确。

But, my issue resolved after applying Boregore's comment. 但是,在应用Boregore的评论后,我的问题解决了。 Python Environment option related with venv python needs to be selected as an interpreter. 需要选择与venv python相关的Python Environment选项作为解释器。

I selected the correct Python Environment option related with venv (in my case ie ~/.virtualenvs/djangodev/bin/python), by applying following steps, 通过执行以下步骤,我选择了与venv相关的正确的Python环境选项(在我的情况下,即〜/ .virtualenvs / djangodev / bin / python),

  1. Select a Python 3 interpreter by opening the Command Palette (Ctrl+Shift+P). 通过打开命令面板(Ctrl + Shift + P)选择一个Python 3解释器。

  2. Start typing the Python: Select Interpreter command to search, then select the command. 开始键入Python:选择“解释器”命令进行搜索,然后选择该命令。 You can also use the Select Python Environment option on the Status Bar if available (it may already show a selected interpreter, too) 您还可以使用状态栏上的“选择Python环境”选项(如果可能)(它可能已经显示了选定的解释器)

  3. Select Python Environment option showing venv path (in my case, ie ~/.virtualenvs/djangodev/bin/python) 选择显示venv路径的Python Environment选项(在我的情况下,即〜/ .virtualenvs / djangodev / bin / python)

  4. Now, VS Code displays the Python Environment option related with venv, (in my case, ie "Python 3.7.3 64 bit ('djangodev': venv) " on the Status Bar. 现在,VS Code在状态栏上显示与venv相关的Python Environment选项(在我的情况下,即“ Python 3.7.3 64 bit ('djangodev':venv)

  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) (非常感谢Boregore提供的解决方案,这只是他对实际问题的评论的重新阐述)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM