[Python] vscode do not find my custom python package

Discussão em 'Python' iniciado por Stack, Maio 16, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página