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

Carl Meyer carl at oddbird.net
Wed Dec 16 11:19:18 EST 2015


Hi Ganesh,

On 12/16/2015 09:09 AM, Ganesh Pal 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

As you've probably already noticed, it usually doesn't matter to Python
(though it can become relevant in certain unusual circular-import
cases). Most people will have some opinion about what constitutes good
style, though. Opinions tend to look something like these (though
details will vary):

1. All imports at the top of the module.
2. Standard library imports, then third-party imports, then local imports.
3. Sometimes the above types of imports are grouped with intervening
blank lines.
4. Sometimes imports are alphabetized within those groups.

> For Example
> 
> import os
> import shlex
> import subprocess
> import time
> import sys
> import logging
> import  plaftform.cluster
> from util import run
> 
> 
> def main():
>      """ ---MAIN--- """
> 
> if __name__ == '__main__':
>     main()
> 
> In the above example :
> 
> 1. Iam guessing  may be the python modules like os , shlex etc come
> first and later the user defined modules like import
> plaftform.cluster .etc come latter
> 
> Sorry if my question sounds dump , I was running pep8 and don't see
> its bothered much about it

AFAIK the pep8 module doesn't care about import order. If you'd like to
enforce an import order in your project, you can look at isort. [1]

Carl

  [1] https://pypi.python.org/pypi/isort

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20151216/829bedd2/attachment.sig>


More information about the Python-list mailing list