What is the best import method for optimizing your code?

I am currently using:

import os

Should I import everything separately, like this:

from os import listdir, chdir, path, getcwd

I am looking for my compiled .exe to be as small as possible and optimized. Is it worth it, or does python not include unused functions and classes when compiling?

Im using pyinstaller

+4
source share
1 answer

The method is import osmore efficient in terms of runtime.

If we import the whole module:

import os

def list():
    print(os.listdir('.'))

it runs in 0.074s, but when importing only one method:

from os import listdir

def list():
    print(listdir('.'))

then it takes 0.076 s.

Here I used a module timeitto perform the above functions.

+1
source

Source: https://habr.com/ru/post/1681504/


All Articles