user15368062
user15368062

Reputation:

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.

Upvotes: 4

Views: 5137

Answers (1)

Anya
Anya

Reputation: 1398

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").

Image displaying the correct virtualenv interpreter

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. :)

Upvotes: 10

Related Questions