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
#2
I would simply write the main code in a function, for example
# foo.py

def spam():
    ...

def eggs():
    ...

def main_task():
    print('Running main task')

if __name__ ==  '__main__':
    main_task()
Now you can use it in three different ways
# bar.py

# first way: import as a module
import foo
# second way: execute main task
import foo
foo.main_task()
# third way: run as subprocess
subprocess.run([sys.executable, 'foo.py'])
Reply
#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


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