无法在我的项目中导入自定义Python包

问题描述 投票:0回答:1

我正在尝试使用 Poetry 在 Python 中创建一个包,然后在另一个项目中使用它。但是导入包后,我无法使用它。该项目无法识别它。

图书馆

pyproject.toml:

[tool.poetry]
name = "python-lib-example"
version = "0.5.0"
description = ""
authors = ["xxx"]
readme = "README.md"
packages = [{include = "python_lib_example"}]

[tool.poetry.dependencies]
python = "^3.12"


[tool.poetry.group.dev.dependencies]
devpi-client = "^7.1.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api

测试班:

class PersonInfo:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def get_name(self):
        return self.name

    def get_age(self):
        return self.age

    def get_info(self):
        return f'{self.name} is {self.age} years old'

    def __str__(self):
        return self.get_info()

__init__.py
是空的

项目结构:

Project structure

我以这种方式构建包并将其上传到在 Docker 中运行的 DevPi。然后我将其从 DevPi 安装到目标项目中。

目标项目

pyproject.toml:

[tool.poetry]
name = "python-app-example"
version = "0.0.1"
description = ""
authors = ["xxx"]
readme = "README.md"
package-mode = false

[tool.poetry.dependencies]
python = "^3.12"
python-lib-example = {version = "^0.5.0", source = "mjana"}


[[tool.poetry.source]]
name = "mjana"
url = "http://localhost:3141/mjana/mjana/+simple"
priority = "supplemental"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api

现在我正在尝试导入

from python_lib_example import PersonInfo

但该项目似乎无法识别该包,即使从 DevPi 完成安装没有任何问题。我哪里可能出错了?

python package python-poetry
1个回答
0
投票

PersonInfo
类位于
person_info
模块的命名空间中。所以你的导入必须是:

from python_lib_example.person_info import PersonInfo
© www.soinside.com 2019 - 2024. All rights reserved.