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

  • Home
  • SEARCH
  • 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 7986921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:59:34+00:00 2026-06-04T11:59:34+00:00

Does it matter where modules are loaded in a code? Or should they all

  • 0

Does it matter where modules are loaded in a code?
Or should they all be declared at the top, since during load time the external modules will have to be loaded regardless of where they are declared in the code…?

Example:

from os import popen

try:
    popen('echo hi')
    doSomethingIllegal;
except:
    import logging                   #Module called only when needed?
    logging.exception("Record to logger)

or is this optimized by the compiler the same way as:

from os import popen
import logging                      #Module will always loaded regardless

try:
    popen('echo hi')
    doSomethingIllegal;
except:
    logging.exception("Record to logger)
  • 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-04T11:59:35+00:00Added an answer on June 4, 2026 at 11:59 am

    This indicates it may make a difference:

    “import statements can be executed just about anywhere. It’s often useful to place them inside functions to restrict their visibility and/or reduce initial startup time. Although Python’s interpreter is optimized to not import the same module multiple times, repeatedly executing an import statement can seriously affect performance in some circumstances.”

    These two OS questions, local import statements? and import always at top of module? discuss this at length.

    Finally, if you are curious about your specific case you could profile/benchmark your two alternatives in your environment.

    I prefer to put all of my import statements at the top of the source file, following stylistic conventions and for consistency (also it would make changes easier later w/o having to hunt through the source file looking for import statements scattered throughout)

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

Sidebar

Related Questions

Does endianness matter at all with the bitwise operations? Either logical or shifting? I'm
If I have a text stored in db, file, resource does not matter: <code><%=
In what scenarios we should go for <dl> not <ul> ? Does it matter
It does not matter what programming language. Can anyone please give me an idea
I have a Python program (with Django - does this matter?) that I want
For doing development, does it matter what OS PHP is running on? In other
I need a simple parser generator. The language does not matter. It only has
Possible Duplicate: How is floating point stored? When does it matter? Using the built-in
Does the ordering of source formats matter within HMTL5 videos tags? Also, if 1
How does C#, or other languages for that matter, handle memory allocation (and memory

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.