Unable to import module when debugging Django in VSCode

Asked 2 years ago, Updated 2 years ago, 102 views

What do you want to solve
When you start debugging with visual studio code,
ffrom.forms import........ <
on views.py The forms.py file in is not loaded and stops, and the following error appears:

Error Contents

ModuleNotFoundError: No module named 'main.forms'; 'main' is not a package

Supplemental
There are no errors when you try python manage.py running server from the terminal.

problem probability

Is it because the debugging function cannot read the relative path?
I have looked into various things, but the settings are not working.
I would appreciate it if you could let me know.

Development Environment
pyenv1.2.4-8-g43235c2
virtualenv --version 16.0.0
(I don't know if I'm using pyenv or virtualenv)
You are in a virtual environment with the command source myvenv/bin/activate, moving to the desktop at the terminal.
(myvenv is a folder on the desktop.)
Django 2.0.2
python 3.6.5
visual studio code 1.25.1 (1.25.1)

python django vscode

2022-09-30 18:05

2 Answers

I have not received an answer, so I will write it from my experience.

1. Open the target Django project directory in File - > Open Folder in the menu.
2. Change the debug environment selection list to Python:Django

Why don't you try running debugs now?


2022-09-30 18:05

_init__.py does not exist in the same directory as forms.py or views.py, so it is presumed that it is loaded as a package.
The contents are empty, so I think you can solve this problem by creating a new _init__.py in the same directory as the python module you want to call.

If this does not work, I would appreciate it if you could add the structure of the directory to your question.


2022-09-30 18:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.