导入的软件包搜索我的代码中的模块
有人可以向我解释这里发生了什么以及如何预防这种情况吗?
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其他
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
重命名,或将其放在文件夹中,例如,然后将其导入,然后将其导入
,
some_name_other_than_utils.utils.utils.utils
在中注册sys.modules
,因此不会影响在yolov5中导入。另一个解决方案是将Yolov5文件夹复制为项目的子文件夹。因此,Yolov5文件夹中的所有内容都在另一个名称空间下,而不会与您的文件冲突。但是您可能需要更改一些导入语句,然后用自己的模型加载功能替换
torch.hub.load
。在大多数情况下,在您自己的项目中具有
utils
不应与具有utils
的第三方软件相抵触。如果您尝试导入火炬然后检查sys.modules,则可以看到torch.utlis
,torch.nn.utils
等,而不仅仅是utils
。以下是如何使用相对导入完成操作的示例:in
some_3rdparty_lib/__ init __. py
:然后在
main.py.py
中:请注意,
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 importutils
. If you import your ownutils
first, it is registered insys.modules
, and python would search in your ownutils
for things needed for the yolov5 model. Same thing would happen if you create the model first and import your ownutils
later (at this time,utils
of the yolov5 repo is registered insys.modules
, so you won't be able to import your ownutils
).The easiest solution would be to rename your
utils.py
, or put it under a folder, e.g.,and import it as
Then,
some_name_other_than_utils.utils
is registered insys.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 hasutils
. If you try to import torch and then check sys.modules, you can seetorch.utlis
,torch.nn.utils
, etc., rather than justutils
. The following is an example of how this can be done with relative imports:In
some_3rdparty_lib/__init__.py
:Then in
main.py
: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 insys.path
. The importedutils
would still belong to the namespacesome_3rdparty_lib
.您必须将文件的名称从utils更改为另一个名称,因为也许您之前安装了UTILS软件包,因此Python会自动获取软件包并忽略您的utils.py文件。
使您的文件名plapla.py
也许这对您有用。
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
maybe this will work for you.