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
#3
that seems to work. __name__ has "foo", presumably the name it is imported as.
Output:
lt1a/forums/1 /home/forums 20> box foo.py bar.py +----<foo.py>-----------------------------------------------------+ | def spam(): | | print('Green ham') | | | | def eggs(): | | print('Green eggs') | | | | def main_task(): | | print('Running main task where __name__ is',repr(__name__)) | | | | if __name__ == '__main__': | | main_task() | +-----------------------------------------------------------------+ +----<bar.py>-------------------------------------------------+ | ## # first way: import as a module | | ## import foo | | # second way: execute main task | | import foo | | print('foo') | | print('running main task where __name__ is',repr(__name__)) | | foo.main_task() | | print('bar') | | ### third way: run as subprocess | | ##subprocess.run([sys.executable, 'foo.py']) | +-------------------------------------------------------------+ lt1a/forums/1 /home/forums 21> python3 foo.py Running main task where __name__ is '__main__' lt1a/forums/1 /home/forums 22> python3 bar.py foo running main task where __name__ is '__main__' Running main task where __name__ is 'foo' bar lt1a/forums/1 /home/forums 23>
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
RE: module either imported by a command or itself run as a command - by Skaperen - Dec-04-2023, 03:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  reading a file like the unix tail -f command does Skaperen 2 445 Mar-31-2024, 12:09 AM
Last Post: Skaperen
  The command python3 -m venv venv does not correctly set sys.path echeadle 2 2,769 Jul-25-2023, 10:41 AM
Last Post: Aften1961
  run a command, capture output, what form do you want? Skaperen 7 2,054 Oct-31-2022, 01:06 AM
Last Post: Skaperen
  implementing the Linux touch command Skaperen 0 1,639 Apr-06-2021, 12:06 AM
Last Post: Skaperen
  review of command line parsers Skaperen 2 2,134 Mar-11-2021, 07:39 PM
Last Post: Skaperen
  the -m option for python command Skaperen 0 1,511 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,838 Nov-15-2020, 07:32 AM
Last Post: Gribouillis
  command line options Skaperen 5 2,749 Aug-14-2020, 08:48 AM
Last Post: DeaD_EyE
  what pip command can list installed files for a name package? Skaperen 3 2,222 Aug-04-2020, 10:15 PM
Last Post: Skaperen
  opening python from the command line takes a long time to load? abdulkaderanwar 4 3,109 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