11

According to PEP8 modules should be lowercase. Some popular ones out there (e.g. Gtk) however follow the CamelCase convention.

In order to have a pythonic codebase and mitigate spillage of this policy breach it seems like the following is a clean way to deal with this:

import CamelcasedModule as camelcased_module

Linters such as pep8-naming however claim that such practice violates PEP8 and throw an N813 error.

As I failed to find any direct passage in PEP8 addressing this I was wondering which way to go in order to stay true to the zen of python.

Sitenote: Previously this question mentioned Gtk as an example:

from gi.repository import Gtk as gtk

Which was misleading as Gtk is a class not a module and as such does not apply to the question. For transparency and because the answers to this may still be usefull it is mentioned here.

Elbenfreund
  • 296
  • 1
  • 3
  • 12
  • There's often legacy reasons why some modules use CamelCase. Often it's something that is ported from Java or C++, and keeping the original naming conventions makes sense. In this case I would not change the casing for consistency reasons. [I think this section of pep8 is relevant](https://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds) – Håken Lid Jun 02 '16 at 11:05
  • @Thomas Plaskota et all: How should I proceed with this question? Whilst you have answered/cleared up my error with regards to the Gtk example, the question relates to a more general case (actually, GTK turned out not to be an example of this). Do I leave Thomasz Answer as accepted or shall I simply rewrite the question not to use Gtk as an example? – Elbenfreund Jun 02 '16 at 11:24
  • If you find a better example of a camelcased module name, you can edit the question and add that one, explaining why you edited. You don't have to remove the Gtk example, if you want to keep the context for Thomasz' answer. – Håken Lid Jun 02 '16 at 11:32
  • @Elbenfreund if you have found better example just add it in edit, and we'll try to answer that instead (or as well). – Tomasz Plaskota Jun 02 '16 at 11:35
  • @Thomasz Plaskota: In order to clean up question and distinguish between the two aspects I used a made up example for now. If anyone finds a real life instance, please feel free to edit. – Elbenfreund Jun 02 '16 at 11:45
  • Interestingly, in the Python docs an example they give includes an import with CamelCase to CONSTANT ('ElementTree' -> 'ET'). Strange that the Python docs seem to violate PEP8...? – kasimir Nov 22 '19 at 20:15

3 Answers3

6

Gtk is a class not a module. Class names should use CapWords convention.

You can read more about that here.

Community
  • 1
  • 1
Tomasz Plaskota
  • 1,329
  • 12
  • 23
  • Yes! The module name would have been `gi.repository` - good catch! – Dilettant Jun 02 '16 at 11:10
  • 1
    Well, that is somewhat embarrassing. Thanks for pointing this out. I guess at least some good came out of it: a definitive and concise answer on SO :) – Elbenfreund Jun 02 '16 at 11:12
4

I second the answer of Tomasz Plaskota that this indeed is an anti-pattern and I like to amend:

In chapter/section "Public and internal interfaces" of pep8:

Imported names should always be considered an implementation detail. Other modules must not rely on indirect access to such imported names unless they are an explicitly documented part of the containing module's API, such as os.path or a package's __init__ module that exposes functionality from submodules.

Otherwise it is clear, that even the standard library cleans up steadily making classes CamelCase and modules lower_underscore_case ...

At first I only skimmed over the question and stored more the trial of:

import Gtk as gtk

The aliasing on import I often do use - but more in the classical local shortcut use case:

import very_impressive_hierarchical_name_for_tools as our_tools

or as in the famous self overwriting fun case of the datetime module with the datetime class:

import datetime as dt

to then be able in actual client code using like:

a_datetime = dt.datetime.now()
0xc0de
  • 8,028
  • 5
  • 49
  • 75
Dilettant
  • 3,267
  • 3
  • 29
  • 29
0

You can do this, to get rid of the warning:

from gi.repository import Gtk
gtk = Gtk

Why would you do this ? Sometimes it's really inconvenient to keep the original naming. My use case was python2/python3 compatible code. Consider this example:

Instead of:

try:
    import pickle
except ImportError:
    import cPickle as pickle # pycharm will complain about import camelcase as lowercase

# do_something_with_pickle_later()

I wrote:

try:
    import pickle
except ImportError:
    import cPickle
    pickle = cPickle # pycharm will not complain about this.

# do_something_with_pickle_later()
KoKlA
  • 898
  • 2
  • 11
  • 15