如何在不重复导入顶级名称的情况下构造python包

2022-01-13 00:00:00 python module package

问题描述

我是 python 包管理的新手,肯定做错了什么.我被鼓励创建一个目录结构如下:

I'm brand new at python package management, and surely have done something wrong. I was encouraged to create a directory structure as follows:

bagoftricks
├── bagoftricks
│   ├── bagoftricks
│   │   ├── __init__.py
│   │   └── bagoftricks.py
│   └── __init__.py
├── README.md
└── setup.py

bagoftricks.py 包含两个函数,levenshtein()geofind().

bagoftricks.py contains two functions, levenshtein() and geofind().

我想把它们称为:

import bagoftricks

x = bagoftricks.levenshtein(arg1,arg2) 

相反,我发现我必须这样做:

Instead, I find I have to do this:

import bagoftricks

x = bagoftricks.bagoftricks.levenshtein(arg1,arg2) 

有没有更好的方法来组织我的包裹,而不用重复命名?

Is there a better way to organize my packages in the first place, without the naming redundancy?

更新

所以,我按照下面 Avichal Badaya 的说明,移除了一层嵌套.也就是说,我现在有……

So, I followed Avichal Badaya's instructions below, and removed one level of nesting. That is, I now have...

bagoftricks
├── bagoftricks
│   ├── __init__.py
│   └── bagoftricks.py
├── README.md
└── setup.py

但是,要调用这个包,我还是有...

However, to call this package, I still have...

from bagoftricks.bagoftricks import geofind()

import bagoftricks

然后

>>> bagoftricks.bagoftricks.geofind()

而不是想要的......

Rather than the desired....

from bagoftricks import geofind()

import bagoftricks

>>> bagoftricks.geofind()

我无法移除额外的嵌套层.以此类推,当我尝试移除一层嵌套时,我的模块是扁平的,如下所示:

I cannot remove that extra layer of nesting. When I try, by analogy, to remove one more level of nesting, so that my module is flat, as:

bagoftricks
├── __init__.py
├── bagoftricks.py
├── README.md
└── setup.py

我根本无法构建包...

I cannot build the package at all...

$ python setup.py build
running build
running build_py
error: package directory 'bagoftricks' does not exist

像标准包一样使用自然导入,没有多余的顶级名称导入的秘诀是什么?

What's the secret for natural imports like standard packages use, without redundant top-level name imports?


解决方案

第一级bagoftricks"就可以了.可以这么说,这只是您的项目"的名称.在你有一个 setup.py 和其他文件告诉打包系统他们需要知道什么.

The first level "bagoftricks" is fine. That's just the name of your "project" so to speak. In the you have a setup.py, and other files that tell the packaging systems what they need to know.

然后您可以将代码直接放在该模块中,或者放在 src 目录中.您甚至可以只使用这种结构:

You can then have the code directly in this module, or in a src directory. You can even go as far as just having this structure:

bagoftricks
├── bagoftricks.py
├── README.md
└── setup.py

但我不建议这样做,主要是因为您可能想稍后重新组织,如果您已经有一个合适的"包会更容易.此外,大多数人、工具和文档都假设你有一个包,所以它更容易.

But I would not recommend that, mostly because you might want to reorganize things later, and it's easier if you already have a "proper" package. Also most people, tools and docs assume you have a package, so it's easier.

所以最小值是:

bagoftricks
├── bagoftricks
│   └── __init__.py
├── README.md
└── setup.py

使用 __init__.py 包含您要导入的函数.然后你可以像这样使用这些函数:

With __init__.py containing the functions you want to import. You then use these functions like this:

from bagoftricks import levenshtein, anotherfunction

一旦 __init__.py 变得太大,你想把它分成几个模块,给你这样的东西:

Once that __init__.py becomes too big, you want to split it up in several modules, giving you something like this:

bagoftricks
├── bagoftricks
│   ├── __init__.py
│   ├── anothermodule.py
│   └── levenshtein.py
├── README.md
└── setup.py

您的 __init__.py 然后应该从各个模块导入函数:

Your __init__.py should then import the functions from the various modules:

from bagoftricks.levenshtein import levenshtein
from bagoftricks.anothermodule import anotherfunction

然后你仍然可以像以前一样使用它们.

And then you can still use them like like you did before.

相关文章