does the order in which the modules are placed in a file matters ?

Chris Angelico rosuav at gmail.com
Wed Dec 16 11:14:09 EST 2015


On Thu, Dec 17, 2015 at 3:09 AM, Ganesh Pal <ganesh1pal at gmail.com> wrote:
> Iam on python 2.7 and linux .I need to know if we need to place the
> modules  in a particular or it doesn't matter at all
>
> order while writing the program
>
> For Example
>
> import os
> import shlex
> import subprocess
> import time
> import sys
> import logging
> import  plaftform.cluster
> from util import run

The order of the import statements is the order the modules will get
loaded up. As a general rule this won't matter; when it comes to
standard library modules, you can generally assume that you can put
them in any order without it making any difference. It's common to
order them in some aesthetically-pleasing way (maybe alphabetical
order, or maybe sort them by the length of the name - whatever you
like).

There is a broad convention that standard library modules get imported
first, and modules that are part of the current project get imported
afterwards. But even that doesn't usually matter very much.

ChrisA



More information about the Python-list mailing list