Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: naming a module that has a two-word name

I'm trying to put together a really simple module with one .py source file in it, and have already run into a roadblock. I was going to call it scons-config but import scons-config doesn't work in Python. I found this SO question and looked at PEP8 style guide but am kind of bewildered, it doesn't talk about two-word-name conventions.

What's the right way to deal with this?

  • module name: SconsConfig? scons_config? sconsconfig? scons.config?
  • name of the single .py file in it: scons-config.py? scons_config.py?

edit: I did see "the use of underscores is discouraged" and that left me at a dead end: should I use "sconsconfig" or "scons_config" (I guess the other ones are out)?

like image 894
Jason S Avatar asked May 17 '10 19:05

Jason S


People also ask

What are the rules for naming a Python module?

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.

How do you name a package in Python?

How to name a Package in python? Only use lower case while naming a Package in Python. Use an underscore as a separator to join multiple words when needed and produce a logical name. However, keeping a short name (preferably one word long) is what PEP 8 prefer.

How do you know all the names defined in a module?

The dir() function is used to find out all the names defined in a module. It returns a sorted list of strings containing the names defined in a module. In the output, you can see the names of the functions you defined in the module, add & sub .


2 Answers

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.

like image 59
Felix Kling Avatar answered Sep 21 '22 21:09

Felix Kling


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.

like image 37
Dan Menes Avatar answered Sep 21 '22 21:09

Dan Menes