Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8151489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:24:26+00:00 2026-06-06T15:24:26+00:00

I have created a number of personal libraries to help with my daily coding.

  • 0

I have created a number of personal libraries to help with my daily coding. Best practice is to put imports at the beginning of python programs. But say I import my library, or even just a function or class from the library. All of the modules are imported (even if those modules are used in other unused classes or functions). I assume this increases the overhead of the program?

One example. I have a library called pytools which looks something like this

import difflib

def foo():
    # uses difflib.SequenceMatcher

def bar():
    # benign function ie
    print "Hello!"
    return True

class foobar:
    def __init__():
        print "New foobar"
    def ret_true():
        return True

The function foo uses difflib. Now say I am writing a new program that needs to use bar and foobar. I could either write

import pytools
...
item = pytools.foobar()
vals = pytools.bar()

or I could do

from pytools import foobar, bar
...
item = foobar()
vals = bar()

Does either choice reduce overhead or preclude the import of foo and its dependencies on difflib? What if the import to difflib was inside of the foo function?

The problem I am running into is when converting simple programs into executables that only use one or two classes or functions from my libraries, The executable ends up being 50 mb or so.

I have read through py2exe’s optimizing size page and can optimize using some of its suggestions.

http://www.py2exe.org/index.cgi/OptimizingSize

I guess I am really asking for best practice here. Is there some way to preclude the import of libraries whose dependencies are in unused functions or classes? I’ve watched import statements execute using a debugger and it appears that python only “picks up” the line with “def somefunction” before moving on. Is the rest of the import not completed until the function/class is used? This would mean putting high volume imports at the beginning of a function or class could reduce overhead for the rest of the library.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:24:28+00:00Added an answer on June 6, 2026 at 3:24 pm

    The only way to effectively reduce your dependencies is to split your tool box into smaller modules, and to only import the modules you need.

    Putting imports at the beginning of unused functions will prevent loading these modules at run-time, but is discouraged because it hides the dependecies. Moreover, your Python-to-executable converter will likely need to include these modules anyway, since Python’s dynamic nature makes it impossible to statically determine which functions are actually called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, I have created a python script, which checks the number of uncompleted tasks
I have created a report which contains a number of parameters so that the
I have created an activity which sends a number of notifications to status bar.
I have created a line chart using Reporting Services that charts the number of
I have created custom cell in which there are n number of image views.
Using xlwDotNet, I have created a simple function which returns one number. Sometimes, something
I have a userform in 2008 vb express edition. A part number is created
I have a listview that contain several EditTexts created dynamically according to the number
I have a stdClass object created from json_decode that won't return the right number
I have a data.frame called series_to_plot.df which I created by combining a number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.