Dec-04-2023, 03:15 AM
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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.