Python Forum
best practice for import libraries and using pyinstaller
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
best practice for import libraries and using pyinstaller
#1
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 os
or only the needed functions:
from os.path import join
Which 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?
Reply
#2
aster Wrote:Should I import the whole library:
import os
or only the needed functions:
from os.path import join
Which is better?
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.
aster likes this post
Reply
#3
(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/1879...rent-files

One more question: where should I put my imports?

import os # Here

if __name__ == '__main__':

    import os # or Here?
What does it change?
Reply
#4
(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,
just after any module comments and docstrings,and before module globals and constants.
aster likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Best practice on using virtual environments in Python bytecrunch 6 841 Feb-14-2024, 03:22 PM
Last Post: snippsat
  Threading best practice EvanS1 2 1,941 Apr-21-2020, 10:11 PM
Last Post: EvanS1
  How to properly close db connection, best practice t4keheart 6 3,026 Jan-24-2020, 06:58 PM
Last Post: Marbelous
  How to Import Libraries like Numpy ajay3adhikari 2 3,031 Jul-08-2019, 02:41 AM
Last Post: DeaD_EyE
  Help with string practice Hermann_Fegelein 2 2,690 Aug-15-2018, 04:56 PM
Last Post: Hermann_Fegelein
  Best Practice For String Quotations ? Zork_3 9 49,965 Sep-01-2017, 07:16 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020