Unable to find/ import .py file in the same directory Visual Studio Code

Viewed 5714

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/"

3 Answers

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.

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.

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.

Related