Python: 命名一个具有两个单词名称的模块

我正在尝试把一个非常简单的模块和一个模块组合在一起。Py 源文件,并且已经遇到了路障。我本来想叫它 scons-config,但是 import scons-config在 Python 中不工作。我找到了 这个所以问题,看了看 PEP8风格指南,但是有点困惑,它并没有提到两个单词的名字惯例。

怎么处理才是正确的?

  • 模块名称: sconfig? scon _ config? sconfig? scons.config?
  • 其中的单个. py 文件的名称: sons-config.py? scon _ config.py?

编辑: 我确实看到了“不鼓励使用下划线”,这让我陷入了死胡同: 我应该使用“ sconconfig”还是“ scon _ config”(我猜其他的都过时了) ?

51668 次浏览

If you have to, always use underscores _.

Using a dot . would not even work, otherwise

from scons.config import whatever

would break.

But PEP 8 clearly describes it:

Package and Module Names

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.

UPDATE:

To directly target your question: I think sconsconfig is fine. It is not too long and quite readable.

But honestly, I don't think anyone will blame you if you use underscores and your code will run with either decision. There is always a certain level where you should not care that much anymore.

- is a no go. The symbol is used for minus operator. The same is true in most programming languages. Use _ or otherwise nothing at all.

First, the module name is the same as the name of the single .py file. In Python-speak, a collection of several .py files is a package.

PEP-8 discourages breaking up package names with underscores. A quick peak at my site-packages directory shows that multiword names are commonly just run together (e.g., setuptools, sqlalchemy)

Module names (that is, file names) may be broken up by underscores (and I usually do this, because I hate namesthatruntogethersoyoucanhardlyreadthem).

Stick with lower-case only (per PEP-8). This avoids problems when going from case-sensitive to case-insensitive filesystems and vice versa.

Aside from PEP-8, you can also check out how the native Python modules deal with this issue.

If you were to compare the native modules of Python 2 to that of Python 3, you would see that the new tendency with the official devs is to avoid uppercase and underscores. For example, ConfigParser in Python 2 becomes configparser in Python 3.

Looking at this, the best course of action would be to avoid uppercase and underscores, and just join the words together, i.e. sconsconfig.