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
#1
how can a module detect if it is being imported, possibly by a command script, as opposed to being run directly?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#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.
    ...
...
Gribouillis and Skaperen like this post
Reply


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