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 871517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:39:06+00:00 2026-05-15T10:39:06+00:00

What are the pros and cons of importing a Python module and/or function inside

  • 0

What are the pros and cons of importing a Python module and/or function inside of a function, with respect to efficiency of speed and of memory?

Does it re-import every time the function is run, or perhaps just once at the beginning whether or not the function is run?

  • 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-05-15T10:39:06+00:00Added an answer on May 15, 2026 at 10:39 am

    Does it re-import every time the function is run?

    No; or rather, Python modules are essentially cached every time they are imported, so importing a second (or third, or fourth…) time doesn’t actually force them to go through the whole import process again. 1

    Does it import once at the beginning whether or not the function is run?

    No, it is only imported if and when the function is executed. 2, 3

    As for the benefits: it depends, I guess. If you may only run a function very rarely and don’t need the module imported anywhere else, it may be beneficial to only import it in that function. Or if there is a name clash or other reason you don’t want the module or symbols from the module available everywhere, you may only want to import it in a specific function. (Of course, there’s always from my_module import my_function as f for those cases.)

    In general practice, it’s probably not that beneficial. In fact, most Python style guides encourage programmers to place all imports at the beginning of the module file.

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

Sidebar

Related Questions

TLDR: What are the pros/cons of using an in-memory database vs locks and concurrent
In Java what pros/cons exist surrounding the choice to use a.getClass() or A.class ?
What are the pros/cons of using the GIT as opposed to CoMarshalInterThreadInterfaceInStream and CoGetInterfaceAndReleaseStream
What are some pros/cons for using the Reflection.Emit library versus CodeDOM for dynamically generating
Are there any pros/cons of using the following two alternatives in your action signature:
This is more a question of pros/cons between PHP and JAVA. Iv been doing
Is it good to use float in table less design? What is the pros/cons
What's the pros and cons of using a Model-View-Controller model in building your application?
What are the pros and cons of using Qt containers ( QMap , QVector
What's the pros and cons for each of them?

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.