4

There are a lot of standard abbreviations used for importing modules in python. I frequently see

import matplotlib.pyplot as plt
import numpy as np
import networkx as nx

I notice that all of these are lower case. I can't think of any exceptions. However, it is case sensitive, so we certainly can use capital letters. Is there any PEP standard about this? In particular, would there be any problem with creating modules with capitalized names and importing them with capitalizations?

For example:

import MyClass as MC
import TomAndJerry as TaJ

(please note - I'm not really interested in personal opinions - but rather whether there are official standards)

Joel
  • 22,598
  • 6
  • 69
  • 93
  • 2
    I'm surprised you've mentioned PEP in your question, but don't know about PEP8. – byxor Mar 30 '17 at 23:38
  • 2
    @byxor not sure if you meant to be annoying with your comment... I had already searched pep8 for both "capital" and "lower case". I guess when there was a (single) match for lower case, I should have considered that there might be an inconsistency and searched for "lowercase" as well. But seriously though, your comment came across as insulting. Maybe you didn't mean it, but ... – Joel Mar 31 '17 at 03:43
  • It's more of a compliment really. PEP8's usually the first thing people are familiar with, but you've already dived into the more complicated stuff. – byxor Mar 31 '17 at 08:13
  • Related: [What is the naming convention in Python for variable and function names?](https://stackoverflow.com/q/159720/3357935) – Stevoisiak Nov 08 '17 at 19:57

3 Answers3

7

There are indeed official standards:

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.

When an extension module written in C or C++ has an accompanying Python module that provides a higher level (e.g. more object oriented) interface, the C/C++ module has a leading underscore (e.g. _socket ).

Hamms
  • 5,016
  • 21
  • 28
5

PEP 8 covers package and module names specifically stating:

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.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
1

There is a standard.

It also gets violated not infrequently. The most common example of off the top of my head is cPickle.

Batman
  • 8,571
  • 7
  • 41
  • 80
  • That said, that naming weirdness was one of several that got fixed in Python 3 (`pickle` is the only official name; the C accelerated code is in `_pickle` and seamlessly replaces the Python level code when it's available). The only remaining module in the Python 3 standard library that violates it is `xml.etree.ElementTree`, and that one is odd, in that it has an API that feels a bit more class-like than most (you parse with `ElementTree.fromstring`, as if it were an alternate class constructor). All other such modules were either eliminated or changed to lowercase in the transition. – ShadowRanger Oct 24 '18 at 19:23