問題描述
我正在嘗試使我的 git 存儲庫 pip 可安裝.為此,我正在重組 repo 以遵循正確的約定.通過查看其他存儲庫,我的理解是,我應該將所有源代碼放在與存儲庫名稱同名的包中.例如.如果我的存儲庫名為 myrepo
,那么源代碼將全部放入一個也稱為 myrepo
的包中.
I am trying to make my git repository pip-installable. In preparation for that I am restructuring the repo to follow the right conventions. My understanding from looking at other repositories is that I should put all my source code in a package that has the same name as the repository name. E.g. if my repository is called myrepo
, then the source code would all go into a package also called myrepo
.
為了便于閱讀,我的存儲庫中有一個連字符:例如我的倉庫
.所以如果我想為它創建一個同名的包,它也會有一個連字符.在本教程中 它說 python 包不要使用連字符"名字.但是,我見過一些完善的軟件包,例如 scikit-learn
,它們的名稱中有連字符.我注意到的一件事是,在 scikit-learn
存儲庫中,包名稱與存儲庫名稱不同,而是稱為 sklearn
.
My repository has a hyphen in it for readability: e.g. my-repo
. So if I wanted to make a package for it with the same name, it would have a hyphen in it as well. In this tutorial it says "don't use hyphens" for python package names. However I've seen well-established packages such as scikit-learn
that have hyphens in their name. One thing that I have noticed though is that in the scikit-learn
repo, the package name is not the same as the repo name and is instead called sklearn
.
我認為我上面的討論歸結為以下問題:
I think my discussion above boils down to the following questions:
- 打包repo的時候,repository的名字和package的名字是什么關系?名字不匹配時有什么要注意的嗎?
- 可以在包名中使用連字符嗎?存儲庫名稱呢?
- 如果
scikit-learn
的包名是sklearn
,那么我安裝它時怎么會使用pip install scikit-learn
pip install sklearn
的?
- When packaging a repo, what is the relationship between the repository's name and the package's name? Is there anything to beware of when having names that don't match?
- Is it okay to have hyphens in package names? What about in repository names?
- If the package name for
scikit-learn
issklearn
, then how come when I install it I dopip install scikit-learn
instead ofpip install sklearn
?
推薦答案
為了回答你的第一點,讓我改寫 我的回答 換一個問題.
To answer your 1st point let me rephrase my answer to a different question.
誤解的最大來源是包"這個詞.嚴重超載.游戲中有 4 個不同的名稱——存儲庫名稱、用于開發的目錄名稱(包含 setup.py
的目錄名稱)、包含 的目錄名稱__init__.py
和其他可導入模塊,PyPI 的分發名稱.這 4 個通常相同或相似,但這不是必需的.
The biggest source of misunderstanding is that the word "package" is heavily overloaded. There are 4 different names in the game — the name of the repository, the name of the directory being used for development (the one that contains setup.py
), the name of the directory containing __init__.py
and other importable modules, the name of distribution at PyPI. Quite often these 4 are the same or similar but that's not required.
存儲庫和開發目錄的名稱可以是任何名稱,它們的名稱不起任何作用.當然,正確命名它們很方便,但這只是方便.
The names of the repository and development directory can be any, their names don't play any role. Of course it's convenient to name them properly but that's only convenience.
包含 Python 文件的目錄名稱是要導入的包的名稱.一旦包被命名為導入,名稱通常會卡住并且無法更改.
The name of the directory with Python files name the package to be imported. Once the package is named for import the name usually stuck and cannot be changed.
發行版的名稱在 PyPI 中提供了一個頁面和發行版文件的名稱(源發行版、雞蛋、輪子).這是 setup(name='distribution')
調用中的名稱.
The name of the distribution gives one a page at PyPI and the name of distribution files (source distribution, eggs, wheels). It's the name one puts in setup(name='distribution')
call.
讓我展示詳細的真實示例.我一直在維護一個名為 CheetahTemplate 的模板庫.我在名為 cheetah3/
的開發目錄中開發它.PyPI 的分布稱為 Cheetah3;這是我放入 setup(name='Cheetah3')
.頂級模塊是 Cheetah
因此有一個import Cheetah.Template
或 from Cheetah import Template
;這意味著我有一個目錄 cheetah3/Cheetah/
.
Let me show detailed real example. I've been maintaining a templating library called CheetahTemplate. I develop it in the development directory called cheetah3/
. The distribution at PyPI is called Cheetah3; this is the name I put into setup(name='Cheetah3')
. The top-level module is Cheetah
hence one does import Cheetah.Template
or from Cheetah import Template
; that means that I have a directory cheetah3/Cheetah/
.
2 的答案是:您可以在存儲庫名稱和 PyPI 分發名稱中使用破折號,但不能在包(包含 __init__.py
文件的目錄)名稱和模塊(.py
文件)名稱,因為你不能在 Python 中編寫 import xy-zzy
,這將是減法和 SyntaxError
.
The answer to 2 is: you can have dashes in repository names and PyPI distribution names but not in package (directories with __init__.py
files) names and module (.py
files) names because you cannot write in Python import xy-zzy
, that would be subtraction and SyntaxError
.
第 3 點:站點和存儲庫名稱是 scikit-learn
,以及 發行版名稱,但可導入的包(帶有 __init__.py
的頂級目錄)是 sklearn.
Point 3: The site and the repository names are scikit-learn
, as well as the distribution name, but the importable package (the top-level directory with __init__.py
) is sklearn.
PEP 8 與該問題無關,因為它不討論分發,只討論可導入的包和模塊.
PEP 8 has nothing to do with the question as it doesn't talk about distribution, only about importable packages and modules.
這篇關于在 python 存儲庫名稱和包名稱中使用連字符/破折號的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!