Reputation: 5938
I was thinking if is relevant to import just what i need from a module in order to reduce the memory consume of any script, or should i just import everything ? I believe if i start to write that way could consume more time write , but it worth it? I mean, could reduce the chance of more usage of the memory?
With the code bellow, a tleast just the relevant parts is an current example of what im planning to do:
from ftplib import FTP as FTP_LIB
from ftplib.FTP import connect as FTP_CONNECT
from ftplib.FTP import login AS FTP_LOGIN
from ftplib.FTP import cwd as FTP_CWD
from ftplib.FTP import storbinary as FTP_STORE_BIN
from ftplib.FTP import retrbinary as FTP_RETRIV_BIN
from ftplib.FTP import delete as FTP_DELETE
from ftplib.FTP import quit as FTP_QUIT
from zipfile import ZipFile
from zipfile import ZIP_DEFLATED
from sys import exit as SYS_EXIT
#--------------------------------------------------------------------------
# FTP Download
#--------------------------------------------------------------------------
def get_file(self, iServer, ftpPort, login, pwd, fileName, path):
parts = iServer.split(":")
host = parts[0]
ftp = FTP_LIB()
try:
FTP_CONNECT(host, ftpPort, 20)
FTP_LOGIN(login, pwd)
FTP_CWD(path)
FTP_RETRIV_BIN('RETR ' + fileName, open(fileName, 'wb').write)
except Exception, e:
print " Download failed : " + str(e)
SYS_EXIT(1)
finally:
FTP_QUIT()
Thanks in advance.
Upvotes: 4
Views: 678
Reputation: 1122512
Importing the module doesn't waste anything; the module is always fully imported (into the sys.modules
mapping), so whether you use import ftplib
or from ftplib import FTP
makes no odds.
I elaborate on why this is and what importing a module really means over on Programmers, on a cross-site duplicate question at 'import module' vs. 'from module import function'.
Upvotes: 5
Reputation: 1446
You should try and import as little as possible. i.e.
"from sys import a" is better than "from sys import *"
The rationale behind it, is so that we do not end up with duplicates and hence things not working as desired. There is already fair amount of module duplication present. so, there are changes that we might end up with the wrong module by importing everything from multiple packages.
from modA import *
from modB import *
what if subModA is available in both modA and modB.
Try to avoid "from modA import submodA as renamed_submodA"
. It makes it difficult for others to understand code.
I would rewrite your definitions for readability as below,
from ftplib import FTP
from ftplib.FTP import connect, login, cwd, storbinary, retrbinary, delete, quit
from zipfile import ZipFile, ZIP_DEFLATED
from sys import exit as SYS_EXIT
Upvotes: 3
Reputation: 628
There's no performance difference between importing everything and using the from
syntax.
The reason why the from <module> import <function>
syntax is useful and sometimes preferred is demonstrated in your code: you do not need to use the module.function
syntax and instead can use the module function as a native function, making your code a lot cleaner.
Upvotes: 2
Reputation: 3514
Full module will be loaded once, but no submodules (if they are not imported by parent module)
Upvotes: 2