how to execute nested python files
Question: I have 3 python files.(first.py, second.py, third.py) I'm executing 2nd python
file from the 1st python file. 2nd python file uses the 'import' statement to
make use of 3rd python file. This is what I'm doing. This is my code.

first.py

    
    
    import os
    file_path = "folder\second.py" 
    os.system(file_path)
    

second.py

    
    
    import third
    ...
    (rest of the code)
    

third.py (which contains [ReportLab](http://www.reportlab.com) code for
generating PDF )

    
    
    ....
    canvas.drawImage('xyz.jpg',0.2*inch, 7.65*inch, width=w*scale, height=h*scale)
    ....
    

when I'm executing this code, it gives error

    
    
    IOError: Cannot open resource "xyz.jpg"
    

**But when i execute second.py file directly by writing`python second.py` ,
everything works fine..!!**

Even i tried this code,

    
    
    file_path = "folder\second.py" 
    execfile(file_path)
    

But it gives this error,

    
    
    ImportError: No module named third
    

But as i stated everything works fine if i directly execute the second.py
file. !!

why this is happening? Is there any better idea for executing such a kind of
nested python files? Any idea or suggestions would be greatly appreciated.

I used this three files just to give the basic idea of my structure. You can
consider this flow of execution as a single process. There are too many
processes like this and each file contains thousandth lines of codes. That's
why i can't change the whole code to be modularize which can be used by import
statement. :-( So the question is how to make a single python file which will
take care of executing all the other processes. (If we are executing each
process individually, everything works fine )


Answer: The way you are doing thing is invalid.

You should: create a main application, and import 1,2,3.

In 1,2,3: You should define the things as your functions. Then call them from
the main application.

IMHO: I don't need that you have much code to put into separate files, you
just also put them into one file with function definitions and call them
properly.

