0

I'm new to VS Code for python development on Windows and my pylint cannot find a package. This is my project directory structure.

workspace/    <- This is VS Code workspace (E:\workspace)
  .vscode/
    launch.json
    settings.json    
  project1/
    mypackge/
      __init__.py          <- In here, I wrote: `import mypackage.first_sub_pkg`
      first_sub_pkg/
        __init__.py        <- In here, I wrote: `from .second_sub_pkg.mymodule import MyClass`
        second_sub_pkg/
          __init__.py      <- In here, I wrote: `from .mymodule import MyClass`
          mymodule.py    <- This module has class: `MyClass`
    test_script/
      mytest.py
  project2/
  etc.../

And I wrote the mytest.py script code like:

from mypackge.first_sub_package import MyClass

I'm using C:/Anaconda3/python.exe for python interpreter

When I click the button on the upper side ▷ (Run Python File in Terminal) on the upper right side of VS Code, I get this error message

PS E:\workspace> & c:/Anaconda3/python.exe e:/workspace/project1/test_script/mytest.py
Traceback (most recent call last):
  File "e:/workspace/project1/test_script/mytest.py", line 1, in <module>
    from first_sub_pkg.second_sub_pkg import MyClass
ModuleNotFoundError: No module named 'first_sub_pkg'

Also, I added workspace/.vscode/launch.json like:

{
    // 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",
            "pythonPath": "${command:python.interpreterPath}",
            "env": {
                "PYTHONPATH": "${workspaceFolder};E:/workspace/project1"
            }
        }
    ]
}

And workspace/.vscode/settings.json like:

{
    "python.autoComplete.extraPaths": [
        "E:/workspace",
        "E:/workspace/project1",
        "E:/workspace/project1/first_sub_pkg",
    ],
    "python.pythonPath": "c:/Anaconda3/python.exe",
    "terminal.integrated.shell.windows": "C:/windows/System32/WindowsPowerShell/v1.0/powershell.exe",
    "python.linter": "pyLint",
    "python.linting.pylintPath": "pylint"
}

And my user settings.json file is like:

{
    "python.autoComplete.extraPaths": [
        "E:/workspace",
        "E:/workspace/project1",
        "E:/workspace/project1/first_sub_pkg",
    ]
}

I already ran this test script in Eclipse + pydev environment, and there was no problem running it. But somehow VSC cannot import my modules.

I seems like system path problem since it works well when I run python and append 'E:/workspace/project1' to system path (import sys; sys.path.append('E:/workspace/project1');), but I cannot find out how to solve the problem. (Adding system variables in Windows settings did not worked neither).

What did I miss? Somebody please help me. I searched for 2 days but got nowhere.

N Lee
  • 21
  • 1
  • 6
  • running the script from the terminal (as you're doing currently) should not have anything to do with vscode – Chase Jul 15 '20 at 11:38
  • I found the exact solution that I needed. https://stackoverflow.com/questions/53653083/how-to-correctly-set-pythonpath-for-visual-studio-code – N Lee Nov 11 '21 at 04:44

3 Answers3

0

first_sub_pkg is not in the same directory as the mytest.py file. You first have to move up one level to project1/ then into mypackage/ then continue with the rest of imports. So the imports you do in mytest.py should be like so:

from ..mypakage.first_sub_pkg.second_sub_pkg.third_sub_pkg.mymodule import MyClass

Why you have so many sub directories I don't know, but your directory structure will get really confusing really fast. Keep the zen of python in mind when coding.

keystroke33
  • 159
  • 3
  • 11
  • I only described the directories which I need to explain for this problem, so all the directories have meanings. Also, in the eclipseo or pycharm ide, there was NO PROBLEM running the SAME code. I guess eclipse or pycharm somehow import my projects to system paths, but VS Code don't. I just want to know how I should change the VS Code settings to fix this problem. – N Lee Jul 16 '20 at 00:29
0

Solutions:

One:

change this statement :"from first_sub_pkg.second_sub_pkg import MyClass" in mytest.py

to "from mypackage.first_sub_pkg.second_sub_pkg.third_sub_pkg.mymodule import MyClass".

Two:

change 'env' in lanuch.json from "PYTHONPATH": "${workspaceFolder};E:/workspace/project1"

to "PYTHONPATH": "${workspaceFolder};${workspaceFolder}/project1/mypackge".

Explain:

The Python only can search the paths in PYTHONPATH. If the module nested in the paths you need to use '.' to connect the folder until points to the module file.

Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13
  • Thanks for the kind answer and my bad. I do not have third_sub_pkg, but the problem I am facing is the same. **The 1st solution**: I already used that to-be statement (`from mypackage...`) **The 2nd solution**: I tried to change the PYTHONPATH in launch.json file as you told, but nothing changed. My test script still do not working. And could you be more specific about using '.' in the last sentence? – N Lee Jul 22 '20 at 06:57
  • The solutions are separated, you just need to take one solution(just in case). If you want to import a module, you got to let python interpreter know which paths it should search, and only the package under these paths can be treated as a package. As you tried "PYTHONPATH": "${workspaceFolder};E:/workspace/project1", the python interpreter will know to search these two paths to, this means it can treat mypackage as a package, but it don't know what's meaning of first_sub_pkg, you got to tell it, it's under the package of mypackage, through 'mypackage.first_sub_pkg'. – Steven-MSFT Jul 22 '20 at 09:39
0

This sulotion is not for mac.

  1. Open vs code press ctrl+shift+p

enter image description here

  1. Enter: Python:Select Interpreter

  2. Choose your environment and run the code again. (rjz is my environment name) enter image description here

  3. If this doesn't fix, you need to use CMD for install packages with conda or pip. In my case install packages with VS code terminal doesn't fix the problem.

  • For some packages, you need to install with vs code terminal expect CMD.
reza jafari
  • 1,228
  • 13
  • 14