简体   繁体   中英

Use same name for module and package

When developing in Python, I try to follow PEP8 conventions. So I use lower case for module (ie file) names and package (ie directories) names.

Most of the time, I end up with the same name. For example, say I work on a calendar manager, I'll create a directory called calendar_manager 1 and put inside a file called calendar_manager.py .

This always bothers me a little when I then have to type from calendar_manager import calendar_manager , as I wonder if this is slightly wrong, or goes against the guidelines of the language. On the other hand, there is often one single name that makes sense to me for a project, and it seems natural to use it both for the package and module.

I notice few of the well known python packages do this. datetime is a notable exception, as both the package and module are called datetime , therefore needing to import it like this from datetime import datetime .

Is it recognized as good practice or not to have the same name for both? As I do not want to encourage subjective answers, can anyone point me to an authoritative source, or objective data that indicates one way or the other? I could not find anything on that specific point in PEP8 .

1 I know PEP8 does not encourage underscores for package names, but this has actually become accepted practice in the Python community. I find it clearer than juxtaposing words together, like calendarmanager

There are a few questions on Stack Overflow on using the same name for module and package, but they address solving technical problems around it, not higher level discussion about naming conventions.

Python Enhancement Proposals (PEP)

A common theme across several Python Enhancement Proposals (PEP) to enforce the use of all-lowercase or sneak_case for names (packages, modules, functions, variables, etc.), so is the use of underscores being usually discouraged when naming a package as you too mentioned.

That doesn't mean that you need to use fused lowercase names like calendarmanager and/or stick with only using descriptive or non-single names for packages and modules alike.

Although it is recommended that one should adhere to using certain PEP conventions, it is also advised that everyone should do whatever makes more sense according to their needs , the same why reason people use underscores for package names despite it being discouraged .


Same Name & Non-Single Names

Using the exact same name for a package and the module that's inside can, depending on the name and name length, make it difficult to read and remember every time.

If you decide to use non-single names, consider using a shortname :

import calendar_manager.calendar_manager as shortname

Add the same name again:

import calendar_manager.calendar_manager as calendar_manager

Names, Cases & Examples

Names should have meaning , while also being relatively simple . Modules & packages should both have a meaningfully and memorable name, just like projects.

You have two names and nothing says they have to be the same length or the same case, you are free to choose what you feel makes more sense to you and your use case.

There are a plethora of different supported case formats available out there. Use them to suit your needs , not to hinder your progress.

Example using snake_case and camelCase :

Package name: “calendar_utils”
Module name: “calenderManager”

from calender_utils import calenderManager

A different example using camelCase and then snake_case :

Package name: “calendarUtils”
Module name: “calender_manager”

from calenderUtils import calender_manager

Example using snake_case and a shorter name :

Package name: “calendar_utils”
Module name: “manager”

from calender_utils import manager

Example using camelCase and a shorter name :

Package name: “calendarUtils”
Module name: “manager”

from calenderUtils import manager

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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