相同的导入,不同的错误 [英] Same import, different errors

查看:26
本文介绍了相同的导入,不同的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对我的代码进行测试,但我遇到了一个奇怪的导入错误.

注意事项:

  • 我使用的是 VSCode 版本:1.46.1
  • PYTHONPATH 设置为项目的根目录

文件夹结构是这样的:

-app.py-楷模---语言.py---category.py-my_tests- -单元-  -  - 楷模---------category_test.py---------language_test.py

在 category_test 中,我确实导入了Category";使用类:

from models.category import Category

在 language_test 中,我确实导入了Language";使用类:

from models.language 导入语言

在 category_test 案例中,一切正常.
在语言测试案例中,我遇到了一个导入错误".
它不是像搜索 category_test 文件那样在项目的根目录中搜索,而是在 my_test/unit/models 文件夹中搜索.

当我打印 sys.path 时,结果如预期:

<预><代码>['/home/ste3/Documents/Projects/SteFunBot/my_tests/unit/models',->当前文件夹'/home/ste3/Documents/Projects/SteFunBot', ->路径'/usr/lib/python38.zip', '/usr/lib/python3.8','/usr/lib/python3.8/lib-dynload','/home/ste3/Documents/Projects/SteFunBot/venv/lib/python3.8/site-packages']

我该如何解决这个问题?

编辑
我认为这是一个短绒问题.是真的吗?如果我尝试通过从根目录 (step1) 指定路径来导入模块,然后我恢复导入并从导入路径 (step2) 中删除根目录,它会起作用:
第 1 步

from SteFunBot.models.language 导入语言

第 2 步

from models.language 导入语言

解决方案

导入其他文件夹中的模块(文件)时,VSCode默认从文件的父文件夹开始.因此,我们可以通过以下方式帮助 VSCode 找到模块路径:

  1. 使用绝对路径.设置VSCode搜索路径的方式是从当前项目开始.VSCode 在执行代码之前先更改项目文件夹路径.

    例如:使用

    <块引用>

    环境":{PYTHONPATH":${workspaceFolder}",}

    launch.json"中添加项目的绝对路径:

  2. 使用相对路径.根据文件的相对路径(使用导入的模块文件)找到项目路径,然后VSCode将从项目文件夹开始搜索.

    例如:在文件开头使用以下代码查找项目文件夹路径:

I am making tests for my code, but I'm facing a strange import error.

Notes:

  • I am using VSCode version: 1.46.1
  • PYTHONPATH is set to project's root

Folder structure is this:

-app.py
-models
---language.py
---category.py
-my_tests
---unit
------models
---------category_test.py
---------language_test.py

In category_test, I do import "Category" class by using:

from models.category import Category

In language_test, I do import "Language" class by using:

from models.language import Language

In category_test case, all works fine.
In language_test case, I encounter one "Import error".
Instead of searching at the project's root as it does for category_test file, it searches on my_test/unit/models folder.

When i print sys.path, result is as expected:

[
 '/home/ste3/Documents/Projects/SteFunBot/my_tests/unit/models', -> current folder
 '/home/ste3/Documents/Projects/SteFunBot', -> PYTHONPATH
 '/usr/lib/python38.zip', '/usr/lib/python3.8', 
 '/usr/lib/python3.8/lib-dynload', 
 '/home/ste3/Documents/Projects/SteFunBot/venv/lib/python3.8/site-packages'
]

How can I fix this?

EDIT
I think it is a linter issue. Can it be? If I try to import the moudules by specifying the path from root (step1) and then I recover the import removing the root from the import path (step2), it works:
Step 1

from SteFunBot.models.language import Language

Step 2

from models.language import Language

解决方案

When importing modules (files) in other folders, VSCode starts from the parent folder of the file by default. Therefore, we can help VSCode find the module path in the following ways:

  1. Use absolute paths. The way to set the VSCode search path is to start from the current project. VSCode first changes to the project folder path before executing the code.

    For example: Use

    "env": {
                    "PYTHONPATH": "${workspaceFolder}",
                }
    

    in "launch.json" to add the absolute path of the project:

  2. Use relative paths. Find the project path based on the relative path of the file (using the imported module file), then VSCode will start searching from the project folder.

    For example: Use the following code at the beginning of the file to find the project folder path:

这篇关于相同的导入,不同的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆