ModuleNotFound错误在Python中出现,但目录已在sys.path上。

huangapple go评论76阅读模式
英文:

ModuleNotFound error in Python but directory is on sys.path

问题

好的,以下是翻译好的部分:

从我的项目根目录开始 - 位于 /user/user4769/development/python-utils

从根目录运行:

python3.8 /user/user4769/development/python-utils/company/team/user_utils/main.py 或者带有 -m 标志的相同命令。正如预期的那样,当我打印 sys.path 时,main.py 的当前目录被首先添加 - 所以我看到:

['/user/user4769/development/python-utils/company/team/user_utils', ....]

在我的 main.py 中,我有:

import company.team.user_utils.utils

但我收到 ModuleNotFound 错误。

如果我添加这行 sys.path.append(os.getcwd()),那么我的根文件夹 /user/user4769/development/python-utils/ 将被添加到路径中,然后导入就能正常工作。

我漏掉了什么?这是 Python3.8。

英文:

Ok, so here is my directory structure

company
└── team
    └── user_utils
        ├── __init__.py
        ├── test.py
        ├── utils.py 

from the root of my project - which is at /user/user4769/development/python-utils

From the root I run :

python3.8 /user/user4769/development/python-utils/company/team/user_utils/main.py or the same with -m flag. As expected, when I print sys.path the current directory of main.py is added first - so I do see :

['/user/user4769/development/python-utils/company/team/user_utils', ....]

In my main.py I do have :

import company.team.user_utils.utils

But I do get ModuleNotFound

If I add this sys.path.append(os.getcwd()) then /user/user4769/development/python-utils/ which is my root folder gets added onto the path, and then import works.

What am I missing? This is Python3.8

答案1

得分: 1

你要翻译的内容如下:

你尝试导入的顶级模块是 company,所以为了使其工作,你需要将 /user/user4769/development/python-utils/ 添加到你的路径中,正如你也已经发现的那样。我相信 companyteam 目录中还需要有 __init__.py 文件。如果你想将 user_utils 目录视为要导入模块的位置,正如你所指出的在你的路径中,一个简单的 import utils 就可以。

点符号仅用于从具有子模块的分层模块中导入子模块和元素。所以如果 company 是模块,并且有一个子模块 .team.user_utils.utils

**注意:**考虑到你的导入在你的路径中包含 .../python-utils 时有效,我敢打赌 company 确实是一个分层模块。如果是这种情况,utils.py 可能会使用 company 模块的其他部分的元素,因此直接导入它而不是作为子模块可能完全行不通。

英文:

The (top) module you are trying to import is company so for this to work you need to have /user/user4769/development/python-utils/ in your path, as you have also found out. And I believe there also needs to be __init__.py files in the company and team directories. If you want to treat the user_utils directory as a location to import modules from, as implied by having pointing out it is in your path, a simple import utils will do.

The dot notation is only for importing submodules and elements from a hierarchical module with submodules. So if company was the module, with (one) submodule being .team.user_utils.utils.

Note: given that your import works with .../python-utils in your path, I'd wager that company is indeed a hierarchical module. If that is the case, utils.py may utilise elements from other parts of the company module, in which case importing it directly and not as a submodule may very well not work.

huangapple
  • 本文由 发表于 2023年6月6日 06:21:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定