Skip to content

'Libraries' lesson suggestion: clarify where to make imports #703

@sadielbartholomew

Description

@sadielbartholomew

How could the content be improved?

Hi, I am teaching a course where we make use of the Carpentries Python Novice materials here and taught the 'Libraries' lesson today. I noticed that at no point does this lesson clarify where imports should be made as best or common practice. All that is stated is:

A program must import a library module before using it.

I suggest that (as I clarified to the students I was teaching as a comment on top of the content in the syllabus / Notebook) a concise note be made in the form of a sentence or two be added to guide as to where in a script imports should be made: this could be a direct quote from the relevant statement in PEP 8, namely:

Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

Imports should be grouped in the following order: Standard library imports. Related third party imports. Local application/library specific imports.

You should put a blank line between each group of imports.

or paraphrasing this, or something that makes a general observation along those lines.

Specific suggestion

I would advise that, to not distract from the core theme of the lesson, the ecosystem of libraries themselves, only the location to place imports is covered, not extra good practice like what order and blank lines as per the final two statements above from the PEP 8. Specifically, I would go for something like:

import statements should generally be put at the top of the file.

and add that to the section with the bullet points under the heading 'A program must import a library module before using it.' as a new bullet point, between the first two bullets (i.e. between 'Use import to load a library module into a program’s memory.' and the 'Then refer to things from the module as module_name.thing_name Python uses . to mean “part of”.').

However, happy to hear other thoughts on how/where best to clarify this. If you like this suggestion, or more generally the idea to clarify when to make imports, I can put in a PR to update the lesson in that regard. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions