5

I have been all over the internet and spent hours trying to fix this. It may be something pretty straight forward so please cut me some slack. This is my first time using Visual Studio Code. I have already tried the solutions presented in the links below:

  1. VS Code - pylinter cannot find module
  2. vscode import error for python module
  3. Can't get VSCode/Python debugger to find my project modules
  4. https://code.visualstudio.com/docs/python/environments

My folder structure looks like this:

I am trying to run the urls.py file which tries to import the views file using-

from . import views

But I get the following error:

Traceback (most recent call last): File "c:/Users/abc/projects/telusko/calc/urls.py", line 7, in from . import views ImportError: cannot import name 'views'

I have tried all possible combinations, and currently my launch.json file looks like this:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }],
    "env": {"PYTHONPATH": "c:/Users/abc/projects/telusko/"},
    "python.pythonPath": "c:/Users/abc/Envs/test/Scripts/python.exe" 
}

I am using a virtual env to run this project, the env is located at - "c:/Users/abc/projects/telusko/test/"

And my project directory is located at -

"c:/Users/abc/projects/telusko/"

Kiedi7
  • 51
  • 1
  • 8

3 Answers3

1

You need to make sure that the folder where VS Code is open is the same folder as the file you have open. You have TELUSKO open and have access to all folders/files in that dir. But urls.py is in the calc dir.

It looks like your VS Code directory is TELUSKO which contains the folders and files:

  • .idea/
  • .vscode/
  • calc/
  • telusko/
  • db.sqlite3
  • manage.py

Thus, ANY files you open will only have access to these folders/files. Even if you open a file in a different directory.

To test this, type import manage and you will see it works even though there is no manage.py file in the calc/ dir.

You have 2 solutions:

  1. Open a new VS Code window in the calc/ dir. Then you can run import views or import apps or import test... all of the files in calc/ are now available to you!
  2. Do an absolute import. Your current dir is TELUSKO/, so do from calc import views.
codeananda
  • 939
  • 1
  • 10
  • 16
0

I create a project with the same folder structure as yours and the venv is called env under the folder test.

enter image description here the urls.py and views.py are the same level modules, so the import sentence should be

from views import *

and the method os.getcwd() is to show your current working directory.

Molly Wang-MSFT
  • 7,943
  • 2
  • 9
  • 22
  • Hi Molly, Are you saying that I should move my env folder to the project folder? Because currently they are in different locations? Could you please elaborate on how you got the env (venv) folder/file. – Kiedi7 Aug 18 '20 at 10:54
  • Hi Molly, I tried following your instructions - moving my venv under the project root. But am still getting the same error. – Kiedi7 Aug 18 '20 at 13:38
  • Nope, in your question, you say the the env is located at c:/users/abc/projects/telusko/test, so i put my venv under the telusko/test to mostly copy and recreate your error. let env alone first, let me check, you want to import views.py in calc/urls.py, right? please comment all other code in calc/urls.py, and run import os print (os.getcwd()) and show me the output – Molly Wang-MSFT Aug 18 '20 at 14:28
  • In calc/urls.py I have the following code: --- from django.urls import path import os print(os.getcwd()) from views import * urlpatterns = [ path('', views.home, name='home') ] ---- The output i get is this ___ (test) C:\Users\abc\projects\telusko>c:/Users/abc/Envs/test/Scripts/python.exe c:/Users/abc/projects/telusko/calc/urls.py C:\Users\abc\projects\telusko Traceback (most recent call last): File "c:/Users/abc/projects/telusko/calc/urls.py", line 7, in path('', views.home, name='home') NameError: name 'views' is not defined – Kiedi7 Aug 18 '20 at 14:45
  • try to run the code in my answer's screenshot, if still error, try to use from calc.views import * – Molly Wang-MSFT Aug 18 '20 at 14:54
  • from calc.views import * ModuleNotFoundError: No module named 'calc' – Kiedi7 Aug 18 '20 at 15:04
  • ok, have you tried import sys sys.path.append('../') from views import * – Molly Wang-MSFT Aug 18 '20 at 15:10
  • Tried this but getting another error - NameError: name 'views' is not defined – Kiedi7 Aug 18 '20 at 15:17
  • For reference, I am exactly following this tutorial on my windows machine - https://www.youtube.com/watch?v=OTmQOjsl0eg .........................................................................stuck at around 28:00 mins into the video. – Kiedi7 Aug 18 '20 at 15:25
  • i've wathed the part you learned and there are some things i need to know: 1. the output about just run import os print(os.getcwd()) 2.try this:when you open the project in vscode, type python -m venv .venv in terminal to create a new environment and select it as you current project's interpreter, then press ctrl+shift+` to refresh the terminal, try to run the import code again. I'll do some research regarding this error and update u later. – Molly Wang-MSFT Aug 19 '20 at 02:04
  • Thanks, will try this out and let you know – Kiedi7 Aug 21 '20 at 08:38
-2

My answer may have been poorly phrased so let me be more clear.

I'm suggesting VIM as a replacement for your entire process.
You don't need VScode or any other IDE, You don't need Pylint, and you don't need to create any virtual environment.

Trying to simplify whatever problem it is your having in creating this "virtual environment" that's causing the error "Unable to find/ import .py file in the same directory Visual Studio Code", by replacing it ALL with VIM.

When I say "This will require additional work and research on your part" it's because Idk if VIM can be configured to do all the things Pylint can do because I don't know what Pylint can do.

You can check this out: https://realpython.com/vim-and-python-a-match-made-in-heaven/

And if you're interested: Edit: Link replaced with direct GitHub repository https://github.com/vim/vim-win32-installer/releases

This is probably something you should be aware of and consider as well: VIM was made for LINUX so future issues you may have could result in it being harder for you to find "Windows" solutions.

Armagon
  • 58
  • 10
  • @kiedi-If you tried that previous install plz discard and use GitHub repository instead – Armagon Aug 18 '20 at 17:17
  • Thanks @armagon, I shall try this out – Kiedi7 Aug 21 '20 at 08:38
  • I don't think that moving to the VI editor is a good advise. It is about the worst editor on the planet IMHO. I am not interested in moving from an IDE like VS Code to an esoteric hard to use editor like vim or vi. I would rather give up coding. – BrownPony May 05 '21 at 22:31