0

I tried splitting my program into 2 files

File 1 is named a (path: /root/a)

#!/usr/bin/env python
from os import *

print('Hello world')

def main():
    system("echo 'All the best'")
    system("./b")

File 2 is named b (path: /root/b)

if __name__ == "__main__":
    main()

system("echo Bbye")

(worth mentioning that I have not mentioned #!/usr/bin/env python in file /root/b , all that there is, is the code above. Nothing else)

I made both file a and b executable and then ran ./a

But the output was only

Hello world

However if I remove the line

system("./b")

from file /root/a and merge both the above files into one and then run that file, I get the output

Hello world
All the best
Bbye

How do I get the above output by splitting my file in two separate files ?

Also could you please explain, why the above code didn't work when split into 2 files ? What went wrong when I split this code ?

Note: I did try searching, but couldn't understand which is why I'm asking here, you might find duplicates of this question question on other StackExchange websites but I couldn't because maybe I'm inexperienced in Python.

0
1

The problem with your code is that main() is a function that doesn't get called at all inside a.py. Python does not work like C, where the main() function always gets called at startup. You have to explicitly call it.

But let's go through your code, in order to better understand what is going on.

In your a.py file, you first call print(). All good up until your main declaration. There you declare a function. The python interpreter knows that a function called main exists.

BUT, in the first case it does not get called, because the code calling it is in your other file b.py, which does not get executed, as the system("./b") line is never reached. So, roughly, what the interpreter says is the following:

from os import *              # great, lets import everything from the os module

print('Hello world')          # simple print statement, execute it

def main():                   # function declaration, from now on, I will keep in mind that it exists
    system("echo 'All the best'")   # don't know what's there, I will find out when it gets called
    system("./b")             # don't know what's there, I will find out when it gets called

In your second example, though, your a.py file looks something like this.

from os import *

print('Hello world')

def main():
    system("echo 'All the best'")

if __name__ == "__main__":
    main()

system("echo Bbye")

Now the Python Interpreter goes into the if-clause and says "Hey I know a function main exists, let's call it". So it goes up to the main function and executes it.

As to what is the best way to split the whole thing into two files, remember that in most cases you want to split a big chuck of code in a separate function and the call it from your program. Therefore we will just declare a function, called main, in a.py as in the following snippet of code:

from os import *

def main():
    print('Hello world')
    print('All the best')
    print('echo Bbye')

Note that the whole functionality goes into the function. It does not make sense in this context to have a print statement outside of the main function. And we will just call it from our b.py file:

#!/usr/bin/env python
from a import main

if __name__ == "__main__":
    main()

And one final comment. I do not use a system call in my code, because that is generally not the best choice. In most cases, you can write native python code to do what you want. In your case you could just use print statements, insted of system("echo ...").

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .