简体   繁体   English

python项目的文件夹命名约定

[英]Folder naming convention for python projects

What is the naming convention in python community to set names for project folders and subfolders? python 社区中为项目文件夹和子文件夹设置名称的命名约定是什么?

my-great-python-project
my_great_python_project 
myGreatPythonProject 
MyGreatPythonProject

I find mixed up in the github.我发现在github中混淆了。 Appreciate your expert opinion.欣赏您的专家意见。

There are three conventions, which you might find confusing.共有三种约定,您可能会发现它们令人困惑。

  1. The standard标准

PEP8 defines a standard for how to name packages and modules: PEP8定义了如何命名包和模块的标准:

Modules should have short, all-lowercase names.模块应该有简短的全小写名称。 Underscores can be used in the module name if it improves readability.如果可以提高可读性,可以在模块名称中使用下划线。 Python packages should also have short, all-lowercase names, although the use of underscores is discouraged. Python 包也应该有简短的、全小写的名称,但不鼓励使用下划线。

  1. Actually, nobody cares about the recommendation about not using underscores实际上,没有人关心关于不使用下划线的建议

Even though it's in PEP8, many packages use underscores and the community doesn't consider it poor practice.尽管它在 PEP8 中,但许多包使用下划线,社区并不认为这是不好的做法。 So you see many names like sqlalchemy_searchable , etc.所以你会看到很多名字,比如sqlalchemy_searchable等。

Although you can create a folder with a name which does not match your package name, it's generally a bad idea to do so because it makes things more confusing.尽管您可以创建一个名称与您的包名称不匹配的文件夹,但这样做通常是个坏主意,因为它会使事情变得更加混乱。

So you'll usually use all-lowercase names with underscores for your folders.因此,您通常会为文件夹使用带下划线的全小写名称。

  1. Package naming on pypi pypi 上的包命名

The name of a package when it's installed doesn't need to match the name it's published to on pypi (the source for pip installs).安装包时的名称不需要与它在 pypi( pip安装的源)上发布的名称相匹配。 Packages on pypi tend to be named with hyphens, not underscores. pypi包往往用连字符命名,而不是下划线。 eg flask-cors , which installs the package flask_cors .例如flask-cors ,它安装了包flask_cors

However, you'll note that if you follow-up on this example that flask-cors's GitHub repo defines the package code in a flask_cors/ directory.但是,您会注意到,如果您跟进此示例, flask-cors 的 GitHub 存储库flask_cors/目录中定义了包代码。 This is the norm.这是常态。

It gets a bit messy though, because pip package installation is case-insensitive and treats underscores and hyphens equivalently.不过它有点混乱,因为pip包安装不区分大小写,并等效地对待下划线和连字符。 So Flask-Cors , fLASK_cOrs , etc are all "equivalent".所以Flask-CorsfLASK_cOrs等都是“等效的”。 Personally, I don't like playing games with this -- I recommend just naming packages on pypi in all-lowercase with hyphens, which is what most people do.就我个人而言,我不喜欢用这个玩游戏——我建议只用全小写的连字符命名 pypi 上的包,这是大多数人所做的。


Disclaimer: I don't own or maintain sqlalchemy-searchable or flask-cors , but at time of writing they're good examples of packages with underscores in their names.免责声明:我不拥有或维护flask-cors sqlalchemy-searchableflask-cors ,但在撰写本文时,它们是名称中带有下划线的包的好例子。

Here is an example of how we might organize a repository called altimeter-valport-lcm which contains the package altimeter_valeport_lcm .下面是我们如何组织一个仓库被调用的例子altimeter-valport-lcm其中包含包altimeter_valeport_lcm The package contains the module altimeter_valeport_lcm.parser :该包包含模块altimeter_valeport_lcm.parser

altimeter-valeport-lcm/
├── altimeter_valeport_lcm
│   ├── parser.py
│   ├── __init__.py
│   └── __main__.py
├── README.rst
└── setup.py

[ NOTE ]: [注意]:

  • All lowercase for choosing the package name.用于选择包名称的全部小写。
  • The repository should use the same name as the package, except that they repository substitute dashes (-) for underscores (_).存储库应该使用与包相同的名称,除了它们存储库用破折号 (-) 代替下划线 (_)。

Read More.阅读更多。

Python packages should also have short, all-lowercase names, although the use of underscores is discouraged. Python 包也应该有简短的、全小写的名称,但不鼓励使用下划线。Pep 8 Style GuidePep 8 风格指南

This is the recommendation for packages, which is the main folder containing modules, for testing, setup, and script files, *.py and __init__.py.这是对包的建议,它是包含模块的主文件夹,用于测试、设置和脚本文件,*.py 和 __init__.py。 Therefore, I am assuming the folder is the package and as such, should be all lower case with no underscore (see the link Some Package Github ).因此,我假设文件夹是包,因此,应该全部小写,没有下划线(请参阅链接Some Package Github )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM