Python Forum
module either imported by a command or itself run as a command
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
module either imported by a command or itself run as a command
#1
i have a module that sometimes i want to run as a command. the problem is that the script that is importing it could be used as a command. thus, there are two scenarios in which variable __name__ would have the value "__main__". but. i want to be able to distinguish these two scenarios so that the module can stay being one file (perhaps named "foo.py") and be imported by a script (done simply like "import foo") where the importing script (perhaps named "bar.py") is run as a command. how can i make the module determine that it is invoked as a command without being confused by the importing script being invoked as a command.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
module either imported by a command or itself run as a command - by Skaperen - Nov-24-2023, 11:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  reading a file like the unix tail -f command does Skaperen 2 461 Mar-31-2024, 12:09 AM
Last Post: Skaperen
  The command python3 -m venv venv does not correctly set sys.path echeadle 2 2,777 Jul-25-2023, 10:41 AM
Last Post: Aften1961
  run a command, capture output, what form do you want? Skaperen 7 2,062 Oct-31-2022, 01:06 AM
Last Post: Skaperen
  implementing the Linux touch command Skaperen 0 1,648 Apr-06-2021, 12:06 AM
Last Post: Skaperen
  review of command line parsers Skaperen 2 2,141 Mar-11-2021, 07:39 PM
Last Post: Skaperen
  the -m option for python command Skaperen 0 1,517 Jan-09-2021, 11:33 PM
Last Post: Skaperen
  Run Python 3 script with the same command on Windows, Linux, and macOS? ssrobins 3 2,844 Nov-15-2020, 07:32 AM
Last Post: Gribouillis
  command line options Skaperen 5 2,759 Aug-14-2020, 08:48 AM
Last Post: DeaD_EyE
  what pip command can list installed files for a name package? Skaperen 3 2,228 Aug-04-2020, 10:15 PM
Last Post: Skaperen
  opening python from the command line takes a long time to load? abdulkaderanwar 4 3,125 Jun-22-2020, 03:42 AM
Last Post: abdulkaderanwar

Forum Jump:

User Panel Messages

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