Recieve output from a second Python script that I have called from my first Python script
Question: I tried to look for an answer to this before I posted it, but I'm having
trouble wording it. So, if theres a duplicate question on the site, I
apologise.

I have a Commend Line python script (in this example we'll call it Script 1)
which calls a function from another script (which we'll call Script 2). I do
this by:

    
    
    import Script2
    Script2.Some_Function()
    

Some_Function does various things, such as connecting to servers on the
internet and performing file look ups. Ordinarily, if one of these tasks
failed, it would print an error stating this to the screen:

    
    
    def Some_Function():
       def error(err):
         print "Error: " + err
    

However, when I know an error should be being printed, I'm not seeing anything
being printed to the screen. I suspect this is because I'm calling it from
Script 1. Is there anything I can do? I really need the output from Script 2
to be printed.


Answer: if your code is exactly like your snippet, it's not likely to work, but first
things first, let's use the correct wording, in python you use the term
`module` to name another python file containing symbols you want to import in
the current (`module`) one.

A script is usually a small piece of code in a high level language that you
run from your command line interface. So as a rule of thumb, a script in
python is where you put `if __name__ == '__main__':`

So I'm renaming your example:

**myscript.py** :

    
    
    import external_module
    external_module.some_function()
    

**external_module** :

    
    
    def some_function():
       def error(err):
           print "Error: " + err
    

> _However, when I know an error should be being printed, I'm not seeing
> anything being printed to the screen. I suspect this is because I'm calling
> it from Script 1. Is there anything I can do? I really need the output from
> Script 2 to be printed._

now that the code is "cleaned" a bit, when you run your program what is
happening?

    
    
    python myscript.py
    

well, nothing, and that's expectable: because you're doing nothing! Let's add
comments:

**myscript.py** :

    
    
    import external_module            # you import the module external_module
    external_module.some_function()   # you run the function some_function()
                                      # from external_module
    

in myscript nothing's wrong. But your problem is in external_module:

**external_module** :

    
    
    def some_function():              # you declare the function some_function
       def error(err):                # you declare the function error 
                                      # that lives only in some_function() scope
           print "Error: " + err      # in error() you print something out!
    

so, when you do `external_module.some_function()`, you just _declare_ the
function `error()` and you never run it, which means you never run the `print`
statement. If you forget the `import` aspect, and only do in the python REPL:

    
    
    >>> def foo():
    ...    def bar():
    ...        print("Hello world?")
    ...
    >>> foo()
    >>>
    

it does nothing! But if you do:

    
    
    >>> def foo():
    ...    def bar():
    ...        print("Hello World!")
    ...    bar()            # here you call the function bar() inside foo()
    ...
    >>> foo()
    Hello World!
    >>>
    

you get to run `bar()`!

I hope that my explanation was exhaustive enough!

HTH

