1. <i id='O5uEw'><tr id='O5uEw'><dt id='O5uEw'><q id='O5uEw'><span id='O5uEw'><b id='O5uEw'><form id='O5uEw'><ins id='O5uEw'></ins><ul id='O5uEw'></ul><sub id='O5uEw'></sub></form><legend id='O5uEw'></legend><bdo id='O5uEw'><pre id='O5uEw'><center id='O5uEw'></center></pre></bdo></b><th id='O5uEw'></th></span></q></dt></tr></i><div id='O5uEw'><tfoot id='O5uEw'></tfoot><dl id='O5uEw'><fieldset id='O5uEw'></fieldset></dl></div>

      <small id='O5uEw'></small><noframes id='O5uEw'>

    2. <legend id='O5uEw'><style id='O5uEw'><dir id='O5uEw'><q id='O5uEw'></q></dir></style></legend>
      <tfoot id='O5uEw'></tfoot>
        <bdo id='O5uEw'></bdo><ul id='O5uEw'></ul>

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

      时间:2023-09-14

      <small id='9D8De'></small><noframes id='9D8De'>

        <tbody id='9D8De'></tbody>
      • <tfoot id='9D8De'></tfoot>

          <legend id='9D8De'><style id='9D8De'><dir id='9D8De'><q id='9D8De'></q></dir></style></legend>

          1. <i id='9D8De'><tr id='9D8De'><dt id='9D8De'><q id='9D8De'><span id='9D8De'><b id='9D8De'><form id='9D8De'><ins id='9D8De'></ins><ul id='9D8De'></ul><sub id='9D8De'></sub></form><legend id='9D8De'></legend><bdo id='9D8De'><pre id='9D8De'><center id='9D8De'></center></pre></bdo></b><th id='9D8De'></th></span></q></dt></tr></i><div id='9D8De'><tfoot id='9D8De'></tfoot><dl id='9D8De'><fieldset id='9D8De'></fieldset></dl></div>
              • <bdo id='9D8De'></bdo><ul id='9D8De'></ul>

                本文介绍了如何在不重复导入顶级名称的情况下构造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()
                

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

                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.

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

                上一篇:在 OpenShift 上安装 python 包 下一篇:配置 Python 以使用站点包的其他位置

                相关文章

                最新文章

              • <i id='ZVJXp'><tr id='ZVJXp'><dt id='ZVJXp'><q id='ZVJXp'><span id='ZVJXp'><b id='ZVJXp'><form id='ZVJXp'><ins id='ZVJXp'></ins><ul id='ZVJXp'></ul><sub id='ZVJXp'></sub></form><legend id='ZVJXp'></legend><bdo id='ZVJXp'><pre id='ZVJXp'><center id='ZVJXp'></center></pre></bdo></b><th id='ZVJXp'></th></span></q></dt></tr></i><div id='ZVJXp'><tfoot id='ZVJXp'></tfoot><dl id='ZVJXp'><fieldset id='ZVJXp'></fieldset></dl></div>
              • <legend id='ZVJXp'><style id='ZVJXp'><dir id='ZVJXp'><q id='ZVJXp'></q></dir></style></legend>

                <small id='ZVJXp'></small><noframes id='ZVJXp'>

                      <bdo id='ZVJXp'></bdo><ul id='ZVJXp'></ul>
                    <tfoot id='ZVJXp'></tfoot>