如何在不重复顶层名称的情况下构造python包 [英] How to structure python packages without repeating top level name for import

查看:85
本文介绍了如何在不重复顶层名称的情况下构造python包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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()

不是想要的....

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.

这篇关于如何在不重复顶层名称的情况下构造python包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆