[英]Python imports and unit tests
I am trying to build a Python package/library for the first time.我正在尝试第一次构建 Python 包/库。 So far I have these files:
到目前为止,我有这些文件:
src
Accentizer2
__init__.py
Accentizer.py
Annotation.py
test
test_1.py
File contents:文件内容:
Accentizer.py:加速器.py:
from Annotation import Annotation # this is where the error occurs
class Accentizer:
x = 10
Annotation.py:注释.py:
class Annotation:
Variants = []
test_1.py: test_1.py:
from src.Accentizer2.Accentizer import Accentizer
When I run test_1.py, I get the following error:当我运行 test_1.py 时,我收到以下错误:
Traceback (most recent call last):
File "..\tests\test_1.py", line 1, in <module>
from src.Accentizer2.Accentizer import Accentizer
File "..\src\Accentizer2\Accentizer.py", line 1, in <module>
from Annotation import Annotation
ModuleNotFoundError: No module named 'Annotation'
When I run Accentizer.py, it runs ok.当我运行 Accentizer.py 时,它运行正常。
What am I missing?我错过了什么? What is the conventional way of linking tests to code under test?
将测试链接到被测代码的传统方法是什么?
Changing the import statement in Accentizer.py
to the line below fixes the error:将
Accentizer.py
中的导入语句更改为以下行可修复错误:
from src.Accentizer2.Accentizer import Accentizer
But then I get the error 'src is not a module name' after I build and install the Accentizer2 package and try to import it in another project.但是在我构建并安装 Accentizer2 package 并尝试将其导入另一个项目后,我收到错误“src 不是模块名称”。
Several things are a little off in here这里有几件事有点不对劲
in your Accentizer.py
you are trying to import a global package Annotation
.在您的
Accentizer.py
中,您尝试导入全局 package Annotation
。 However, the Annotation.py
is a local file (local to the package/folder).但是,
Annotation.py
是一个本地文件(包/文件夹的本地文件)。
Assuming your src
folder is in the sys.path
, a correct way of doing it (given your folder structure) is either have a global or local import.假设您的
src
文件夹在sys.path
中,正确的做法(给定您的文件夹结构)是全局或本地导入。 Note: the names global and local are not really standard:)注意:名称 global 和 local 并不是真正的标准:)
This just imports the package, and provides a fully qualified path during import.这只是导入 package,并在导入期间提供完全限定的路径。 Assumption is that
src
is in the path.假设
src
在路径中。
from src.Accentizer2.Annotation import Annotation
class Accentizer:
x = 10
This one doesn't care about where everything is located, and uses .
这个不关心所有东西的位置,并使用
.
to refer to the current folder.引用当前文件夹。 You can also use
..
to refer to parent, ...
to grandparent, etc.您也可以使用
..
来指代父母, ...
来指代祖父母等。
from .Annotation import Annotation
class Accentizer:
x = 10
src
folder src
文件夹It is non-pythonic to have a src
folder.有一个
src
文件夹是非 Pythonic 的。 It is customary for a lot of languages, but not some much for python对于很多语言来说这是惯例,但对于 python 来说并不多
Generally, you don't wanna name the files the same as the class names.通常,您不想将文件命名为与 class 名称相同的名称。 I would suggest having files
accentizer.py
and annotation.py
with classes Accentizer
and Annotation
respectively.我建议分别使用带有
Accentizer
和Annotation
类的文件accentizer.py
和annotation.py
。 The reasoning is that having the same name for class and file might break the import process.原因是 class 和文件具有相同的名称可能会中断导入过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.