Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100523

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

From Carl Meyer <carl@oddbird.net>
Newsgroups comp.lang.python
Subject Re: does the order in which the modules are placed in a file matters ?
Date 2015-12-16 09:19 -0700
Message-ID <mailman.19.1450284973.30845.python-list@python.org> (permalink)
References <CACT3xuW5hdCUDaO4i-Fd8583j9CC0ATm3xHWYsqEXcbuW8_pgw@mail.gmail.com>

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: does the order in which the modules are placed in a file matters ? Carl Meyer <carl@oddbird.net> - 2015-12-16 09:19 -0700

csiph-web