Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing function from module takes very long

When i import my self written function of a module in a python script it takes about 6 seconds to load. The function contains only about 50 lines of code but that shouldn't even matter since it has not been executed yet right?

This is the script that loads the function:

#/usr/bin/env python

import time
print(time.clock())
from os import chdir
print(time.clock())
from os.path import abspath, dirname
print(time.clock())
from Project.controllers.SpiderController import RunSpider
print(time.clock())

And the output is as follows:

0.193569
0.194114
0.194458
6.315348

I also tried to import the whole module but the result was the same.

What could be the cause of that?

Some side notes:

  • I use python 2.7.9
  • The module uses the scrapy framework
  • The python script is running on a Raspberry Pi 1 Model B
like image 452
Praind Avatar asked Jun 22 '17 13:06

Praind


People also ask

What is the difference between importing a module and a function?

Whether you import a module or import a function from a module, Python will parse the whole module. Either way the module is imported. "Importing a function" is nothing more than binding the function to a name. In fact import module is less work for interpreter than from module import func.

Why is the first method of importing a Python module faster?

But the reason I prefer first method is it is fast because we are importing only the function that is needed rather than import the whole module (which contains more useless functions which python will waste time importing them). Note that I need just argv and all other functions from sys are useless to me. So my questions are.

What is the difference between import Sys and import argv?

Importing the module doesn't waste anything; the module is always fully imported (into the sys.modules mapping), so whether you use import sys or from sys import argv makes no odds.

Is it faster to use a specific name in a module?

But only if you use a specific name in a module many times over, in a critical loop for example. But then creating a local name (within a function) is going to be faster still: Show activity on this post. There are two reasons in favor of using import module rather than from module import function. First is the namespace.


1 Answers

but that shouldn't even matter since it has not been executed yet right?

The code of the function itself is not executed, but the code in the file is executed. This is logical, since that file might contain decorators, library calls, inner constants, etc. It is even possible that the function is build (so that an algorithm constructs the function).

With from <module> import <item> you do a almost normal import, but you create only one reference to an item in that package.

So it can take a long time if there is a program written in the module (that is not scoped in an if __name__ == '__main__':) or when you import a large amount of additional libraries.

It is for instance possible to construct a function like:

def foo(x):
    return x + 5

def bar(y):
    return y * 2

def qux(x):
    return foo(bar(x))

If you then run from module import qux, then it will first have to define foo and bar, since qux depends on these.

Furthermore although the code itself is not executed, the interpreter will analyze the function: it will transform the source code into a syntax tree and do some analysis (which variables are local, etc.).

Finally note that a package typically has an __init__.py file, that initializes the package. That file is also executed and can take considerable time as well. For instance some packages that have a database connection, will already set up the connection to that database, and it can take some time before the database responds to the connection.

like image 79
Willem Van Onsem Avatar answered Oct 22 '22 16:10

Willem Van Onsem