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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:45:07+00:00 2026-05-22T12:45:07+00:00

When writing a Python module, is there a way to tell if the module

  • 0

When writing a Python module, is there a way to tell if the module is being imported or reloaded?

I know I can create a class, and the __init__() will only be called on the first import, but I hadn’t planning on creating a class. Though, I will if there isn’t an easy way to tell if we are being imported or reloaded.

  • 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-22T12:45:08+00:00Added an answer on May 22, 2026 at 12:45 pm

    The documentation for reload() actually gives a code snippet that I think should work for your purposes, at least in the usual case. You’d do something like this:

    try:
        reloading
    except NameError:
        reloading = False # means the module is being imported
    else:
        reloading = True # means the module is being reloaded
    

    What this really does is detect whether the module is being imported “cleanly” (e.g. for the first time) or is overwriting a previous instance of the same module. In the normal case, a “clean” import corresponds to the import statement, and a “dirty” import corresponds to reload(), because import only really imports the module once, the first time it’s executed (for each given module).

    If you somehow manage to force a subsequent execution of the import statement into doing something nontrivial, or if you somehow manage to import your module for the first time using reload(), or if you mess around with the importing mechanism (through the imp module or the like), all bets are off. In other words, don’t count on this always working in every possible situation.

    P.S. The fact that you’re asking this question makes me wonder if you’re doing something you probably shouldn’t be doing, but I won’t ask.

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

Sidebar

Related Questions

I'm writing a Python program with a GUI built with the Tkinter module. I'm
I am writing a python module which checks several media and document formats whether
I have an Python ExcelDocument class that provides basic convenience methods for reading/writing/formatting Excel
I'm writing a python script that executes a csh script in Solaris 10. The
I'm writing a Python application that takes a command as an argument, for example:
I'm writing a Python program with a lot of file access. It's running surprisingly
I am writing a Python utility that needs to parse a large, regularly-updated CSV
I'm writing a mapping application that I am writing in python and I need
I'm coding the menu for an application I'm writing in python, using wxPython libraries
So I've decided to try to solve my physics homework by writing some python

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.