Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'reason,': 0.07; 'dependency': 0.09; 'function,': 0.09; 'function:': 0.09; 'lookup': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:modules': 0.09; 'python': 0.11; 'django': 0.11; 'called,': 0.16; 'called.': 0.16; 'cons': 0.16; 'dict': 0.16; 'imports': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'script?': 0.16; 'subject: \n ': 0.16; 'subject:import': 0.16; 'subject:top': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'library': 0.18; 'module': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'circular': 0.24; 'helper': 0.24; 'received:comcast.net': 0.24; 'file.': 0.24; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'fixed': 0.29; 'statement': 0.30; 'subject:the': 0.34; 'executing': 0.36; 'subject:?': 0.36; 'should': 0.36; 'example,': 0.37; 'performance': 0.37; 'sometimes': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'users': 0.40; 'subject:? ': 0.60; 'worry': 0.60; 'first': 0.61; 'sam': 0.68 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: Is it better to import python modules inside function or at the top? What are the pros and cons? Date: Sat, 11 Jan 2014 20:49:02 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: c-50-133-228-126.hsd1.ma.comcast.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389491356 news.xs4all.nl 2918 [2001:888:2000:d::a6]:49338 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63742 On 1/11/14 8:28 PM, Sam wrote: > I have python modules which are used only in specific functions and the functions are not called all the time. Is it better to import the function inside the function only or is it a better practice to always import all modules at the top of the script? If I import the module inside the function, will it cause a big performance hit because of the import module action that gets to be called every time the function is called? > > What are the pros and cons of each approach? > Unless there's a good reason, you should import all the modules at the top of the file. Reasons to import in a function: 1) if a function is only sometimes called, and the import is expensive. 2) if a function is only sometimes called, and needs a dependency that not all users of your package need. For example, your library has both Flask and Django helper functions, and only Django users call the Django function, etc. 3) if you have a circular import, though that can often be fixed in better ways. Note that the cost of imports is only incurred at the first import, so you don't have to worry about the import statement executing each time your function is called. After the first import, the cost is about the same as a dict lookup (very fast). -- Ned Batchelder, http://nedbatchelder.com