![]() |
best practice for import libraries and using pyinstaller - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: best practice for import libraries and using pyinstaller (/thread-33338.html) |
best practice for import libraries and using pyinstaller - aster - Apr-17-2021 Hello, I have a python program splitted in multiple files, I convert it to an executable with pyinstaller. I am not sure about witch is the best practise for importing libraries to decrease the size of the executable Should I import the whole library: import osor only the needed functions: from os.path import joinWhich is better? Also, since my program is splitted in multiple files, I have a few import for the same library (for example os), is it advisable to use a main file where I import all needed libraries for the whole program? RE: best practice for import libraries and using pyinstaller - snippsat - Apr-17-2021 aster Wrote:Should I import the whole library:The whole library is always imported, the only difference is now that join is in global namespace so can write less when call it.Look at this post where i have several example of making a package. See that i lift sub-modules so get one clean import ,i do not like when maker of a package use serval imports or long imports.As example eg Requests so when do simple import import requests you have access to 95% of most common usage.>>> import requests >>> >>> requests.get <function get at 0x00000231500BF550> >>> requests.post <function post at 0x00000231500BF700>Over is the common way,but can short down name bye doing this. >>> from requests import get, post >>> >>> get <function get at 0x00000231500BF550> >>> post <function post at 0x00000231500BF700>As mention no difference as all of library is always imported. RE: best practice for import libraries and using pyinstaller - aster - Apr-17-2021 (Apr-17-2021, 10:48 AM)snippsat Wrote: The whole library is always imported, Thanks, also I checked and importing multiple tile the same lib is not a problem (until I don't get a cyclic import): https://stackoverflow.com/questions/18792145/same-module-is-being-imported-in-different-files One more question: where should I put my imports? import os # Here if __name__ == '__main__': import os # or Here?What does it change? RE: best practice for import libraries and using pyinstaller - snippsat - Apr-17-2021 (Apr-17-2021, 11:01 AM)aster Wrote: One more question: where should I put my imports?Always on to top. PEP-8 Imports Quote:Imports are always put at the top of the file, |