Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the number of imported modules in python effect memory and performance?

When importing modules in python, is there a memory difference when you import a single module from a package eg: from math import ceil vs the whole package eg: import math? I guess what I am really asking is whether or not it slows processing down when the script is run?

like image 735
TsvGis Avatar asked Jul 29 '15 04:07

TsvGis


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 happens when a Python module is imported?

When a module is first imported, Python searches for the module and if found, it creates a module object 1, initializing it. If the named module cannot be found, a ModuleNotFoundError is raised. Python implements various strategies to search for the named module when the import machinery is invoked.

How many libraries can be imported in Python?

Python is a primary language used by data scientists for data science projects, because of the presence of thousands of open-source libraries that can ease and perform a data scientist task. Over 235,000 Python packages can be imported through PyPl.

How many times does a module gets loaded when imported multiple times in Python?

The rules are quite simple: the same module is evaluated only once, in other words, the module-level scope is executed just once. If the module, once evaluated, is imported again, it's second evaluation is skipped and the resolved already exports are used.


1 Answers

There is no difference between those. Raymond Hettinger, one of the core Python developers, recently tweeted this:

#python tip: from-imports don't save memory. They execute and cache the entire module just like a regular import.

like image 84
lvc Avatar answered Oct 18 '22 11:10

lvc