Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does importing more slow down scripts python?

Tags:

python

Just wondering if importing more functions from other scripts slows down a script in general?

Some background:

I have two scripts, one that runs much faster than the other, one has an extra import statement at the top and extra function at the bottom, but its the stuff in the middle, thats the same between scripts that is running slower.

like image 775
user124123 Avatar asked Aug 27 '13 10:08

user124123


People also ask

Do imports slow down Python?

Starting a Python interpreter and importing Python modules is relatively slow if you care about milliseconds. If you need to start hundreds or thousands of Python processes as part of a workload, this overhead will amount to several seconds of overhead.

What slows down a Python program?

If your Python code runs too fast, a call to time. sleep() is a simple way to slow down your code.

Does order of imports matter Python?

Import order does not matter. If a module relies on other modules, it needs to import them itself. Python treats each . py file as a self-contained unit as far as what's visible in that file.


1 Answers

More information about your case, Import_Statement_Overhead:

Import statements can be executed just about anywhere. It's often useful to place them inside functions to restrict their visibility and/or reduce initial startup time. Although Python's interpreter is optimized to not import the same module multiple times, repeatedly executing an import statement can seriously affect performance in some circumstances.

like image 145
badc0re Avatar answered Sep 18 '22 17:09

badc0re