如何将类导入其他文件?

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

How can I import classes into other files?

问题

我有这样的文件结构:

/app/sense/abstract/init.py
/app/sense/abstract/sensor.py
/app/sense/init.py
/app/sense/gps.py
/app/components.py
/app/main.py
/tests/unit/init.py
/tests/unit/context.py
/tests/unit/test_sense.py

sensor.py 定义了一个由 gps.py 使用的抽象基类 Sensor 以塑造一个 GPS 类。

components.py 的目的是以一行代码导入子文件夹中的所有类。到目前为止,唯一的代码行是:

from .sense.gps import GPS

context.py 的目的是允许我导入用于单元测试的类。它目前包括以下代码行:

from pathlib import Path
import sys

path = Path(file).resolve().parent.parent.parent
sys.path.insert(0, path)

import app.components as avc

最后,test_sense.py 类包括用于测试 GPS 是否实现了 Sensor 的代码。

我遇到的问题是,每当我尝试运行 test_sense.py 文件时,都会收到 ModuleNotFoundError,显示 'app' 不存在。我该如何解决这个问题?

英文:

I've got a file structure like this:

/app/sense/abstract/__init__.py
/app/sense/abstract/sensor.py
/app/sense/__init__.py
/app/sense/gps.py
/app/components.py
/app/main.py
/tests/unit/__init__.py
/tests/unit/context.py
/tests/unit/test_sense.py

sensor.py defines an abstract base class Sensor used by gps.py to shape a GPS class.

The intent of components.py is to facilitate
importing all classes in the subfolders in one line. The only line of code so far is:

from .sense.gps import GPS

The intent of context.py is to allow me to import classes for unit testing. It currently has the following lines of code:

from pathlib import Path
import sys

path = Path(__file__).resolve().parent.parent.parent
sys.path.insert(0, path)

import app.components as avc

And finally the test_sense.py class includes code to test whether the GPS implements Sensor.

The problem I'm having is that: whenever I try to run the test_sense.py file, I get ModuleNotFoundError, saying 'app' doesnt exist. How can I get around that?

答案1

得分: 1

错误是因为在运行 test_sense.py 文件时,'app' 包不在 Python 路径中。修复这个问题的一种方法是在 test_sense.py 文件中添加 'app' 包到 Python 路径,类似于在 context.py 文件中所做的。以下是如何修改你的 test_sense.py 文件以将 'app' 包添加到 Python 路径中的代码:

import sys
from pathlib import Path

# 将 'tests' 目录的父目录添加到 Python 路径中
sys.path.append(str(Path(__file__).resolve().parents[2]))

# 从 'app' 包中导入 GPS 类
from app.sense.gps import GPS

# 从 'app' 包中导入 Sensor 类
from app.sense.abstract.sensor import Sensor

# 定义测试类
class TestGPS:

    # 测试 GPS 是否实现了 Sensor
    def test_implements_sensor(self):
        assert issubclass(GPS, Sensor)

这段代码将 'tests' 目录的父目录添加到 Python 路径中,这应该使 Python 能够找到 'app' 包。然后,它从 'app' 包中导入 GPS 和 Sensor 类,并像以前一样定义测试类。

英文:

The error occurs because the 'app' package is not in the Python path when running the test_sense.py file. One way to fix this is to add the 'app' package to the Python path in the test_sense.py file, similar to what is done in the context.py file. Here is how you can modify your test_sense.py file to add the 'app' package to the Python path:

import sys
from pathlib import Path

# Add the parent directory of the 'tests' directory to the Python path
sys.path.append(str(Path(__file__).resolve().parents[2]))

# Import the GPS class from the 'app' package
from app.sense.gps import GPS

# Import the Sensor class from the 'app' package
from app.sense.abstract.sensor import Sensor

# Define the test class
class TestGPS:

    # Test that GPS implements Sensor
    def test_implements_sensor(self):
        assert issubclass(GPS, Sensor)

This code adds the parent directory of the 'tests' directory to the Python path, which should allow Python to find the 'app' package. Then, it imports the GPS and Sensor classes from the 'app' package and defines the test class as before.

huangapple
  • 本文由 发表于 2023年3月31日 22:27:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899689.html
匿名

发表评论

匿名网友

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

确定