gpt4 book ai didi

Python项目结构,项目主文件导入助手

转载 作者:行者123 更新时间:2023-11-28 16:59:59 25 4
gpt4 key购买 nike

这是我的项目结构。

[~/Sandbox/pystructure]:$ tree
.
├── pystructure
│ ├── __init__.py
│   ├── pystructure.py
│   └── utils
│   ├── eggs
│   │   ├── base.py
│   │   └── __init__.py
│   ├── __init__.py
│   └── spam.py
├── README.md
└── requirements.txt

3 directories, 8 files

这些是文件的内容,

[~/Sandbox/pystructure]:$ cat pystructure/utils/spam.py 
def do():
print('hello world (from spam)!')

[~/Sandbox/pystructure]:$ cat pystructure/utils/eggs/base.py
def do():
print('hello world (from eggs)!')

[~/Sandbox/pystructure]:$ cat pystructure/utils/eggs/__init__.py
from .base import do

[~/Sandbox/pystructure]:$ cat pystructure/pystructure.py
#!/usr/bin/python3

from .utils import spam, eggs

def main():
spam.do()
eggs.do()

if __name__ == '__main__':
main()

但是,当我尝试像这样运行应用程序时,出现此错误,

[~/Sandbox/pystructure]:$ python3 pystructure/pystructure.py 
Traceback (most recent call last):
File "pystructure/pystructure.py", line 3, in <module>
from .utils import spam, eggs
ModuleNotFoundError: No module named '__main__.utils'; '__main__' is not a package

或者当我尝试从创建文件的目录中运行代码时(这不是我的愿望,因为我想将它作为服务运行或使用 cron),

[~/Sandbox/pystructure]:$ cd pystructure/

[~/Sandbox/pystructure/pystructure]:$ python3 pystructure.py
Traceback (most recent call last):
File "pystructure.py", line 3, in <module>
from .utils import spam, eggs
ModuleNotFoundError: No module named '__main__.utils'; '__main__' is not a package

但是如果我导入它,它确实有效(但是只能从基本目录...)

[~/Sandbox/pystructure/pystructure]:$ cd ..

[~/Sandbox/pystructure]:$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pystructure import pystructure
>>> pystructure.main()
hello world (from spam)!
hello world (from eggs)!
>>>

(如果我尝试从它所在的目录导入它,我会收到此错误),

[~/Sandbox/pystructure]:$ cd pystructure/

[~/Sandbox/pystructure/pystructure]:$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pystructure
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "~/Sandbox/pystructure/pystructure/pystructure.py", line 3, in <module>
from .utils import spam, eggs
ImportError: attempted relative import with no known parent package
>>>

我认为我的问题来自于对 PYTHONPATH 没有完全的理解,我尝试了谷歌搜索,但我还没有找到我的答案......请提供任何见解。

最佳答案

当您从包中导入时,您是从该包的 __init__.py 中导入....

所以在您的 utils 包中,您的 __init__.py 是空的。

尝试将此添加到您的 utils/__init__.py

print("utils/__init__.py")
from . import eggs
from . import spam

现在,当你说 from utils import eggs, spam 时,你是说,从 utils 包的 init.py 导入我在那里导入的东西.

此外,在 pystructure.py

改变这个

from .utils import  eggs, spam 

进入这个

from utils import  eggs, spam 

关于Python项目结构,项目主文件导入助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55231956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com