3

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 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 )

Gordon Seidoh Worley
  • 7,839
  • 6
  • 45
  • 82
Moin Ahmed
  • 2,898
  • 21
  • 33
  • 5
    This is a remarkably bad way to build a Python app from separate modules. Using `os.system` to run another python script is rather silly. `execfile` is slightly better. Importing it is best. You really should redo your design so that your various modules have functions that you can import and call rather than continue trying to make this work. – S.Lott Feb 25 '12 at 12:26
  • I'm confused as to what the os.system call is achieving, but really you should be importing the other python modules to execute them, rather than breaking out to shell and running them. – Karl Barker Feb 25 '12 at 12:28
  • 1
    Not sure if it's relevant to your problem, but the backslash in the file name strings in your could should be doubled or they will be interpreted as string escape characters and not reference the intended files. – martineau Feb 25 '12 at 16:30

4 Answers4

2

This should be easy if you do it the right way. There's a couple steps that you can follow to set it up.

Step 1: Set your files up to be run or imported

#!/usr/bin/env python

def main():
    do_stuff()

if __name__ == '__main__':

The __name__ special variable will contain __main__ when invoked as a script, and the module name if imported. You can use that to provide a file that can be used either way.


Step 2: Make your subdirectory a package

If you add an empty file called __init__.py to folder, it becomes a package that you can import.


Step 3: Import and run your scripts

from folder import first, second, third

first.main()
second.main()
third.main()
Community
  • 1
  • 1
Daenyth
  • 35,856
  • 13
  • 85
  • 124
1

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.

Umur Kontacı
  • 35,403
  • 8
  • 73
  • 96
0

Future readers:

Pradyumna's answer from here solved Moin Ahmed's second issue for me:

import sys, change "sys.path" by appending the path during run time,then import the module that will help

[i.e. sys.path.append(execfile's directory)]

Shmuelt
  • 119
  • 8
0

I second S.Lott: You really should rethink your design.

But just to provide an answer to your specific problem: From what I can guess so far, you have second.py and third.py in folder, along with xyz.jpg. To make this work, you will have to change your working directory first. Try it in this way in first.py:

import os
....
os.chdir('folder')
execfile('second.py')

Try reading about the os module.

Markus
  • 494
  • 3
  • 12