python文件无法作为模块被导入

1、模块无法被导入

在项目tt下新建了文件夹tt包含模块文件a.py,里面包含t函数,在同路径下的1.py引入模块进行t函数的调用,具体如下

目录结构

/tt
  pyproject.toml
  /tt
    __init__.py
    /ttt
      __init__.py
      b.py
      ...

      ...
    a.py
    1.py
    ...

tt/tt/a.py

def t():
    print("run tt/a.py/t function")

tt/tt/1.py

from tt.a import t
t()

执行

python tt/1.py

报错ModuleNotFoundError

from tt.a import t
ModuleNotFoundError: No module named 'tt.tt'; 'tt' is not a package

2、python打包方式

2.1、pyproject.toml文件编写

pyproject.toml方式构建打包 ,本例子的pyproject.toml

[tool.poetry]
name = "tt"
version = "0.1.0"
description = "My project description"
authors = ["Your Name <you@example.com>"]
license = "MIT"

[tool.poetry.dependencies]
python = "^3.8"
requests = "^2.25.1"
# 更多依赖...

[tool.poetry.dev-dependencies]
pytest = "^6.2.4"
# 更多开发依赖...

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

2.2、包安装

执行

pip install -e .

可以输入python,进入到python环境,通过import tt,help(tt)查看安装包的具体情况。

退出python命令,执行

python tt/1.py

成功运行

3、pyproject.toml如何生成

3.1、手动编写pyproject.toml

模版

[tool.poetry]
name = "my_project"
version = "0.1.0"
description = "My project description"
authors = ["Your Name <you@example.com>"]
license = "MIT"

[tool.poetry.dependencies]
python = "^3.8"
requests = "^2.25.1"
# 更多依赖...

[tool.poetry.dev-dependencies]
pytest = "^6.2.4"
# 更多开发依赖...

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

在这个 pyproject.toml 文件中:

  • [tool.poetry] 部分包含你的项目的元数据,如名称、版本、描述、作者等信息。
  • [tool.poetry.dependencies] 部分列出了项目所需的第三方依赖。
  • [tool.poetry.dev-dependencies] 部分是开发时的依赖项,如测试框架。
  • [build-system] 部分指定了构建项目所需的最小要求,包括 poetry-core 和构建后端。

请注意,^ 符号是 poetry 版本标记,它意味着兼容的软件版本。在依赖版本中使用 ^ 表示你愿意接受在 major 版本不改变的前提下进行更新,这是遵循语义化版本控制。

3.2、通过poetry自动创建

3.2.1、poetry自动创建命令

poetry init

3.2.2、poetry安装及常见命令

pip install poetry
poetry install

# 查看包依赖
poetry show --tree

# 新增依赖
poetry add xxx

# 导出至 requirements.txt,用于线上服务部署
poetry export -f requirements.txt --output requirements.txt --without-hashes

4、pyproject.toml如何编写嵌套的目录

pyproject.toml 中执行嵌套目录的声明并不会直接在该文件中完成,而是通过相应的工具配置来管理多个包或模块的位置。pyproject.toml 文件本身主要用于指定包的元数据和构建依赖,并不直接处理包内文件和目录的布局。

在使用 poetry 管理项目时,如果你的项目结构具有嵌套目录,你通常需要在项目的 pyproject.toml 配置中指定如何发现和包含这些包。嵌套目录通常涉及到 source package(源包)和 subpackage(子包)的概念,poetry 将会根据项目文件夹结构自动识别并包含它们。

对于复杂项目,通常的做法是在项目根目录下有一个源代码目录(通常是项目名),而嵌套目录则作为子包存在。

例如,项目结构类似这样:

/my_project
  pyproject.toml
  /my_project
    __init__.py
    /subpackage1
      __init__.py
      ...

    /subpackage2
      __init__.py
      ...

    ...

在这种情况下,不需要在 pyproject.toml 中特别声明嵌套目录。只要每个子目录都包含 __init__.py 文件,poetry 就能正确地识别它们为子包。

如果有特殊的包含或排除规则需要指定,比如你想排除一些测试目录,或者有一些非标准的目录结构,那么你可以在 pyproject.toml[tool.poetry] 部分使用 includeexclude 参数来明确指定要包含和排除的文件和目录。

例如:

[tool.poetry]
name = "my_project"
# ... 省略其他元数据 ...

[tool.poetry.packages]
include = ["my_project", "my_project/subpackage1", "my_project/subpackage2"]

[tool.poetry.exclude]
exclude = ["tests", "**/*.tests.py", "**/*.test.py"]

相关推荐

  1. python文件无法作为模块导入

    2024-04-21 01:00:01       30 阅读
  2. 文件文件夹占用无法删除

    2024-04-21 01:00:01       35 阅读
  3. Python模块相对导入

    2024-04-21 01:00:01       55 阅读
  4. Python 模块导入方式

    2024-04-21 01:00:01       26 阅读
  5. c# sqlite导出导入数据表 作为sql文件

    2024-04-21 01:00:01       38 阅读
  6. MOJO中导入python模块

    2024-04-21 01:00:01       50 阅读
  7. Python模块导入的写法

    2024-04-21 01:00:01       21 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-21 01:00:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-21 01:00:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-21 01:00:01       82 阅读
  4. Python语言-面向对象

    2024-04-21 01:00:01       91 阅读

热门阅读

  1. 【Cookie,Session,Token,JWT的区别】

    2024-04-21 01:00:01       35 阅读
  2. Android 单一音量控制

    2024-04-21 01:00:01       35 阅读
  3. Spark面试整理-Spark集成Hive

    2024-04-21 01:00:01       32 阅读
  4. 4月20日,每日信息差

    2024-04-21 01:00:01       33 阅读
  5. Python框架django项目

    2024-04-21 01:00:01       35 阅读
  6. oepncv android 使用笔记

    2024-04-21 01:00:01       35 阅读
  7. 使用leaflet给地图添加蒙版(超级详细免费看)

    2024-04-21 01:00:01       39 阅读