0

I have a Django project called ubm-app with two apps, main and ubm-app. I've created a couple of classes, URLAdder and URLModifier in the views.py file in the main app. Im trying to import these views into my urls.py file by using the line from . import views but VS Code shows me the error unresolved import 'c:\Users\Hari\Desktop\django\UBM-App\main.'.

This is my urls.py file.

from django.urls import path, include
from . import views

urlpatterns = [
    path('',views.home,name='home'),
    path('api/add/', views.URLAdder.as_view()),
    path('api/modify/',views.URLModifier.as_view()),
]

This is the settings.json file found when I use the Configure Language Specific Settings... option.

{
    "C_Cpp.updateChannel": "Insiders",
    "[python]": {



    }
}

This is the settings.json file in the .vscode folder in my Django project.

{
    "python.pythonPath": "env\\Scripts\\python.exe"
}

Can anyone please suggest a fix to this with an explanation? I seem to be having these unresolved import errors time and again. Forgive me if the error is a basic one.

supersaiyajin87
  • 151
  • 1
  • 8
  • Did you install pylint? That is often the issue when it comes to false import error messages in VSCode – Hybrid Apr 24 '20 at 16:57
  • @Hybrid yes, I have installed pylint-django. The version is 2.0.15 and the pylint version is 2.4.4, but I dont quite understand its necessity here because importing has worked without pylint before and I dont see the people around me doing this to be able to import peacefully. – supersaiyajin87 Apr 24 '20 at 21:14
  • Can you provide the entire traceback? – Brett Cannon Apr 27 '20 at 20:47

2 Answers2

0

I had the same problem with wrong unresolved import errors in vscode. I suggest that you see the answers to this question as one of the answers might solve your problem as well.

Winston
  • 601
  • 1
  • 9
  • 29
  • The right answer seems to be to use following setting in the workspace settings `.vscode/settings.json` : ``` "python.autoComplete.extraPaths": ["./path-to-your-code"], ``` Though, is it necessary to do this everytime I initialize a project in Django? Because importing has worked in the past a time or two without this. Thanks so far. – supersaiyajin87 Apr 24 '20 at 21:15
  • Well, it seems that this approach solves the problem for this specific project, not the others. Though in my case, [this answer](https://stackoverflow.com/a/56464813/6287044) solved the problem. So, check this one too. – Winston Apr 24 '20 at 22:10
0

This is my Solution: On your Vscode/setting.json simply place the "Python.autoComplete.extraPaths" to correspond to your Project File so that when you import a Module on your script, Your pyDev server can detect it and show no error to your Code. e.g "python.autoComplete.extraPaths": ["./path-to-your-script-code"],

Remember to inlude your Project folder along the path to your actual code file

enter image description here

Grayrigel
  • 3,474
  • 5
  • 14
  • 32