简体   繁体   English

pylint vscode无法导入自己的模块

[英]pylint vscode unable to import own module

Context语境

I have a project in vscode with following file structure:我在 vscode 中有一个项目,文件结构如下:

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: settings.json文件的写法如下:

{
    "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: .env文件是这样写的:

PYTHONPATH=./src

Problem问题

In test_code1.py , I'm trying to import code1 this way:test_code1.py ,我试图以这种方式导入 code1:

import my_package.code1

And I get an error: Unable to import 'my_package.code1' pylint(import-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 .您需要提供python.envFile的绝对路径。 It should be:它应该是:

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

And in the .env file, you can add this:.env文件中,您可以添加以下内容:

PYTHONPATH=src

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 VSCode 在同一文件夹中导入模块时出现“pylint(导入错误)” - "pylint (import error)" while import a module in the same folder with VSCode VSCode无法导入“示例” pylint(导入错误) - VSCode Unable to import 'example' pylint(import-error) 无法使用 pylint(vscode、virtualenv)导入“app.xxx” - Unable to import 'app.xxx' with pylint (vscode, virtualenv) Pylint-无法导入从其他程序运行的模块 - Pylint - Unable to import a module running from a different program 安装模块时,VSCode / pylint将模块win32event报告为未解决的导入 - VSCode/pylint reports module win32event as unresolved import when module is installed Python窥视定义在vscode中有效,但是pylint抱怨无法导入 - Python peek definition works in vscode, but pylint complains about being unable to import VS Code pylint(import-error)“无法导入”自定义目录中的子模块 - VS Code pylint(import-error) "Unable to import" subsub-module from custom directory 无法导入“numpy”pylint(导入错误) - Unable to import 'numpy'pylint(import-error) 无法导入 BeautifulSoup(pylint 导入错误) - Unable to import BeautifulSoup(pylint import error) PyLint 在 Django 项目中的应用程序模块文件中显示“无法导入”错误 - PyLint shows “Unable to import” error with app's module file in Django project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM