vscode do not find my custom python package

4.3k Views Asked by At

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.

3

There are 3 best solutions below

1
On

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.

2
On

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.

0
On

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.