简体   繁体   中英

vscode django debugging error: Couldn't import Django

I tried to debug a django project using vscode. But the next one came.

 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/cubest_june/hj-django/english/manage.py", line 11, in main
    from django.core.management import execute_from_command_line

The above exception was the direct cause of the following exception:

  File "/Users/cubest_june/hj-django/english/manage.py", line 13, in main
    raise ImportError(
  File "/Users/cubest_june/hj-django/english/manage.py", line 22, in <module>
    main()

Even when I just ran python manage.py runserver, it ran without any problems, and both Django and Python are installed.

(django-envs) ➜ english git:(main) ✗ django-admin --version
3.2.5
(django-envs) ➜ english git:(main) ✗ python --version
Python 3.9.0

What is the cause of this problem and how to fix it?

This is my first time doing something like this, so I don't know what kind of information I need. Let me know and I'll edit it.

I don't know if you have already solved it, but I give a solution as reference.

The problem is happening because your VS Code is not automatically using your virtualenv as interpreter of the file. You need to access "Python: Select Interpreter" on your VS Code settings (ctrl+shift+p or clicking on the Python version on the status bar) and change the interpreter to the one located inside your virtualenv. Be sure you select the one that points out to your virtualenv folder ("django-envs" for you. In my case (see picture) it is just "env").

显示正确的 virtualenv 解释器的图像

Please also make sure that you have set "Python: Django" in your debug mode, as this is necessary to run and debug correctly your Django app. And this should allow your debugger to work correctly. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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