导入的软件包搜索我的代码中的模块

发布于 2025-02-13 21:42:55 字数 2098 浏览 0 评论 0原文

有人可以向我解释这里发生了什么以及如何预防这种情况吗?

我有一个 main.py ,具有以下代码:

import utils
import torch


if __name__ == "__main__":
    # Foo
    print("Foo")

    # Bar
    utils.bar()

    model = torch.hub.load("ultralytics/yolov5", "yolov5s")

我将一些功能外包到一个名为 utils.pys.py 的模块中:

def bar():
    print("Bar")

当我运行此功能时,我会得到以下输出:

(venv) jan@xxxxx test % python main.py
Foo
Bar
Using cache found in /Users/jan/.cache/torch/hub/ultralytics_yolov5_master
Traceback (most recent call last):
File "/Users/jan/PycharmProjects/test/main.py", line 12, in <module>
    model = torch.hub.load("ultralytics/yolov5", "yolov5s")
File "/Users/jan/PycharmProjects/test/venv/lib/python3.10/site-packages/torch/hub.py", line 540, in load
    model = _load_local(repo_or_dir, model, *args, **kwargs)
File "/Users/jan/PycharmProjects/test/venv/lib/python3.10/site-packages/torch/hub.py", line 569, in _load_local
    model = entry(*args, **kwargs)
File "/Users/jan/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 81, in yolov5s
    return _create('yolov5s', pretrained, channels, classes, autoshape, _verbose, device)
File "/Users/jan/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 31, in _create
    from models.common import AutoShape, DetectMultiBackend
File "/Users/jan/.cache/torch/hub/ultralytics_yolov5_master/models/common.py", line 24, in <module>
    from utils.dataloaders import exif_transpose, letterbox
ModuleNotFoundError: No module named 'utils.dataloaders'; 'utils' is not a package

所以似乎就像我导入的火炬软件包一样,也具有 utils 资源(软件包),并搜索一个名为“ utils.dataloaders”的模块。好的。但是为什么它在我的Utils模块中搜索?如果我在我的代码中找不到匹配资源,为什么不继续在自己的软件包中搜索?最后但并非最不重要的一点:我该如何防止这种情况?

我将将导入UTILS将其更改为导入UT,并使用ut.bar()调用我的功能,但没有任何区别。

唯一有效的是重命名我的utils.py对其他东西,但这不能是解决方案...

感谢您的帮助。欢呼,

Can someone explain me what is going on here and how to prevent this?

I have a main.py with the following code:

import utils
import torch


if __name__ == "__main__":
    # Foo
    print("Foo")

    # Bar
    utils.bar()

    model = torch.hub.load("ultralytics/yolov5", "yolov5s")

I outsourced some functions into a module named utils.py:

def bar():
    print("Bar")

When I run this I get the following output:

(venv) jan@xxxxx test % python main.py
Foo
Bar
Using cache found in /Users/jan/.cache/torch/hub/ultralytics_yolov5_master
Traceback (most recent call last):
File "/Users/jan/PycharmProjects/test/main.py", line 12, in <module>
    model = torch.hub.load("ultralytics/yolov5", "yolov5s")
File "/Users/jan/PycharmProjects/test/venv/lib/python3.10/site-packages/torch/hub.py", line 540, in load
    model = _load_local(repo_or_dir, model, *args, **kwargs)
File "/Users/jan/PycharmProjects/test/venv/lib/python3.10/site-packages/torch/hub.py", line 569, in _load_local
    model = entry(*args, **kwargs)
File "/Users/jan/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 81, in yolov5s
    return _create('yolov5s', pretrained, channels, classes, autoshape, _verbose, device)
File "/Users/jan/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 31, in _create
    from models.common import AutoShape, DetectMultiBackend
File "/Users/jan/.cache/torch/hub/ultralytics_yolov5_master/models/common.py", line 24, in <module>
    from utils.dataloaders import exif_transpose, letterbox
ModuleNotFoundError: No module named 'utils.dataloaders'; 'utils' is not a package

So it seems like the torch package I imported has also a utils resource (package) and searches for a module named "utils.dataloaders". Okay. But why is it searching in my utils module? And why isn't it continuing searching in its own package if it doesn't find a matching resource in my code? And last but not least: How can I prevent this situation?

I changed import utils to import utils as ut and call my function with ut.bar() but it doesn't make any difference.

The only thing that worked is to rename my utils.py to something else but this cannot be the solution...

Thanks for your help. Cheers,

Jan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万劫不复 2025-02-20 21:42:55

其他utils软件包不属于火炬,它属于yolov5存储库:/users/jan/.cache/torch/torch/hub/ultralytics_yolov5_master/utils

现在,要解释错误:似乎Python会在导入utils时首先搜索sys.modules。如果您首先导入自己的utils,则在sys.modules中注册,Python将在您自己的utils中搜索Yolov5模型。如果您先创建模型并以utils导入 utils yolov5 repo的 utils ,则会发生同样的事情。 /code>,因此您将无法导入自己的utils)。

最简单的解决方案是将您的utils.py重命名,或将其放在文件夹中,例如,

your_project/
  main.py
  some_name_other_than_utils/
    utils.py

然后将其导入,然后将其导入

import some_name_other_than_utils.utils as utils

some_name_other_than_utils.utils.utils.utils中注册sys.modules,因此不会影响在yolov5中导入。

另一个解决方案是将Yolov5文件夹复制为项目的子文件夹。因此,Yolov5文件夹中的所有内容都在另一个名称空间下,而不会与您的文件冲突。但是您可能需要更改一些导入语句,然后用自己的模型加载功能替换torch.hub.load


在大多数情况下,在您自己的项目中具有utils不应与具有utils的第三方软件相抵触。如果您尝试导入火炬然后检查sys.modules,则可以看到torch.utlistorch.nn.utils等,而不仅仅是utils。以下是如何使用相对导入完成操作的示例:

your_project/
  main.py
  utils.py
  some_3rdparty_lib/
    __init__.py
    utils.py

in some_3rdparty_lib/__ init __. py

# some_3rdparty_lib/__init__.py
from . import utils

然后在main.py.py中:

# main.py
import utils   # your own utils
import some_3rdparty_lib.utils   # the one under some_3rdparty_lib

请注意,some_3rdparty_lib代码>不必在您的项目目录下。只要路径位于sys.path中,就可以将其放置在任何地方。导入的utils仍然属于命名空间some_3rdparty_lib

The other utils package does not belong to torch, it belongs to the yolov5 repository: /Users/jan/.cache/torch/hub/ultralytics_yolov5_master/utils.

Now, to explain the error: It seems that python would search for sys.modules first when you import utils. If you import your own utils first, it is registered in sys.modules, and python would search in your own utils for things needed for the yolov5 model. Same thing would happen if you create the model first and import your own utils later (at this time, utils of the yolov5 repo is registered in sys.modules, so you won't be able to import your own utils).

The easiest solution would be to rename your utils.py, or put it under a folder, e.g.,

your_project/
  main.py
  some_name_other_than_utils/
    utils.py

and import it as

import some_name_other_than_utils.utils as utils

Then, some_name_other_than_utils.utils is registered in sys.modules so it won't affect importing in yolov5.

Another solution is to copy the yolov5 folder as a subfolder of your project. So everything in the yolov5 folder is under another namespace, without conflicting with your files. But you may need to change some import statements, and replace torch.hub.load with your own model loading function.


In most cases, having utils in your own project shouldn't conflict with third-party software that also has utils. If you try to import torch and then check sys.modules, you can see torch.utlis, torch.nn.utils, etc., rather than just utils. The following is an example of how this can be done with relative imports:

your_project/
  main.py
  utils.py
  some_3rdparty_lib/
    __init__.py
    utils.py

In some_3rdparty_lib/__init__.py:

# some_3rdparty_lib/__init__.py
from . import utils

Then in main.py:

# main.py
import utils   # your own utils
import some_3rdparty_lib.utils   # the one under some_3rdparty_lib

Note that some_3rdparty_lib does not have to be under your project directory. You can put it anywhere as long as the path is in sys.path. The imported utils would still belong to the namespace some_3rdparty_lib.

就是爱搞怪 2025-02-20 21:42:55

您必须将文件的名称从utils更改为另一个名称,因为也许您之前安装了UTILS软件包,因此Python会自动获取软件包并忽略您的utils.py文件。

使您的文件名plapla.py

import plapla

也许这对您有用。

You have to change the name of the file from utils to another name because maybe you installed utils package before so the python automatically get the package and ignore your utils.py file.

make your file name plapla.py

import plapla

maybe this will work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文