简体   繁体   English

正确构建 Python 包?

[英]Properly structuring Python packages?

I have a Python3.7 package structured like so.我有一个 Python3.7 package 结构像这样。

project             # Repository
--\README
--\LICENSE
--\setup.py
--\project          # Source
----\project.py
----\__init__.py    # Init1
----\functions
-------\__init__.py # Init2
-------\moduleA.py
-------\ ...
-------\moduleZ.py

Inside project.py I have the following.project.py里面我有以下内容。

from functions.moduleA import functionA
from functions.moduleB import functionB
...
functionA = functionA
functionB = functionB

Init2 is completely blank. Init2 完全空白。 But Init1 has the following.但 Init1 有以下内容。

from .project import functionA
from .project import functionB

But when I execute $ python -c 'import project' inside the Repository directory, I get a ModuleNotFoundError: No module named 'functions' .但是当我在 Repository 目录中执行$ python -c 'import project'时,我得到一个ModuleNotFoundError: No module named 'functions' There is something perhaps very simple that is going wrong here.这里可能有一些非常简单的问题。 The import works correctly inside the Source directory though.不过,导入在 Source 目录中可以正常工作。 Ultimately, I'm interested in users installing everything via setup.py and using my project like so.最终,我对用户通过setup.py安装所有东西并像这样使用我的项目感兴趣。

import project

project.functionA(...)
...
project.functionB(...)

Your project.py file doesn't work as you intend.您的project.py文件无法按预期工作。 These lines are causing the error:这些行导致错误:

from functions.moduleA import functionA
from functions.moduleB import functionB

Those imports don't work because functions is not a top-level module, but rather a subpackage that is part of the project package.这些导入不起作用,因为functions不是顶级模块,而是project package 的子包。 You can fix them by adding a .您可以通过添加. at the start of each module name ( .functions.moduleA , etc.).在每个模块名称的开头( .functions.moduleA等)。 I suppose you could also name the absolute module name, if you preferred it for some reason (eg project.functions.moduleA ).我想您也可以命名绝对模块名称,如果您出于某种原因喜欢它(例如project.functions.moduleA )。

The assignment statements also don't do anything.赋值语句也不做任何事情。 functionA = functionA could be deleted and there would be no difference at all. functionA = functionA可以删除,完全没有区别。

It's not really clear to me what the purpose of the project.py file is.我不清楚project.py文件的目的是什么。 It seems entirely redundant next to the project/__init__.py file, which seems like it's supposed to contain exactly the same values.project/__init__.py文件旁边似乎完全是多余的,它似乎应该包含完全相同的值。 Probably you should merge them together, into a single file (probably the __init__.py file).可能您应该将它们合并到一个文件中(可能是__init__.py文件)。

I'd also think carefully about whether you really need so many subpackages and modules.我还会仔细考虑您是否真的需要这么多子包和模块。 While it's sometimes just a matter of style, keeping things that are closely related in the same module can be very handy.虽然有时只是风格问题,但将密切相关的东西放在同一个模块中会非常方便。 You certainly do not need a separate module for every function you write!您当然不需要为您编写的每个 function 单独的模块!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM