简体   繁体   中英

pylint vscode unable to import own module

Context

I have a project in vscode with following file structure:

main_folder/
  .vscode/
    settings.json
  src/
    my_package/
      part1/
        __init__.py
        code1.py
      part2/
        __init__.py
        code2.py
    __init__.py
  test/
    __init__.py
    test_code1.py
.env

The settings.json file is written as follows:

{
    "python.pythonPath": "/usr/local/opt/python@3.9/bin/python3.9",
    "python.envFile": ".env",
    "python.autoComplete.extraPaths": ["./src"]
}

The .env file is written as follows:

PYTHONPATH=./src

Problem

In test_code1.py , I'm trying to import code1 this way:

import my_package.code1

And I get an error: Unable to import 'my_package.code1' pylint(import-error)

However, this works:

import src.my_package.code1

But I would like to go for the first option.

Any good solution for this?

You need to give an absolute path to python.envFile . It should be:

"python.envFile": "${workspaceFolder}/.env",

And in the .env file, you can add this:

PYTHONPATH=src

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM