Python Forum
module detecting if imported vs not
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
module detecting if imported vs not
#2
https://docs.python.org/3/library/__main__.html Wrote:a module can discover whether or not it is running in the top-level environment by checking its own __name__, which allows a common idiom for conditionally executing code when the module is not initialized from an import statement:

if __name__ == '__main__':
    # Execute when the module is not initialized from an import statement.
    ...
...
Skaperen and Gribouillis like this post
Reply


Messages In This Thread
module detecting if imported vs not - by Skaperen - Nov-19-2021, 02:30 AM
RE: module detecting if imported vs not - by Yoriz - Nov-19-2021, 07:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 743 Aug-06-2023, 01:09 AM
Last Post: aupres
  iterating and detecting the last Skaperen 3 1,110 Oct-01-2022, 05:23 AM
Last Post: Gribouillis
  Detecting float or int in a string Clunk_Head 15 4,703 May-26-2022, 11:39 PM
Last Post: Pedroski55
  Can a module tell where it is being imported from? stevendaprano 3 1,218 Apr-12-2022, 12:46 AM
Last Post: stevendaprano
  detecting a generstor passed to a funtion Skaperen 9 3,726 Sep-23-2021, 01:29 AM
Last Post: Skaperen
  Python BLE Scanner not detecting device alexanderDennisEnviro500 0 2,034 Aug-01-2021, 02:29 AM
Last Post: alexanderDennisEnviro500
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,441 Apr-13-2021, 07:22 PM
Last Post: HeRo
  [newbie] Why is a module imported twice? Winfried 3 4,113 Apr-02-2021, 04:48 AM
Last Post: deanhystad
  Detecting power plug Narayan 2 2,751 Aug-01-2020, 04:29 AM
Last Post: bowlofred
  Detecting USB Device Insertion on Windows 10 Atalanttore 0 2,426 Jan-17-2020, 02:46 PM
Last Post: Atalanttore

Forum Jump:

User Panel Messages

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