問題描述
我是 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模板網!