Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalization standards in importing in python

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)

like image 267
Joel Avatar asked Oct 26 '25 16:10

Joel


2 Answers

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 ).

like image 90
Hamms Avatar answered Oct 29 '25 05:10

Hamms


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.

like image 27
ShadowRanger Avatar answered Oct 29 '25 05:10

ShadowRanger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!