[英]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.