0

i've python in a usb stick and i'm designing a recursive descent parser.

the main script is recursive.py which is run by following code from command prompt.

python.exe compiler\recursive.py<compiler\rd_input

my directory structure is

python.exe
compiler\
    recursive.py
    rd_input

in my code i'm generating a python script with 3 functions.

compiler\   
    recursive_header.py

which i need to import in the main script recursive.py later.

i've tried import recursive_header and import compiler\recursive_header and import compiler/recursive_header it's showing the error

Traceback (most recent call last):
  File "compiler\recursive.py", line 74, in <module>
    import recursive_header
ImportError: No module named recursive_header

i've tried the solution given here. but same error.

also tried

import sys
sys.path.append('/compiler')
import recursive_header

here error numbers increased mentioning some about sys.

how can i import compiler\recursive_header.py in my script.

Community
  • 1
  • 1
RatDon
  • 3,403
  • 8
  • 43
  • 85
  • sorry silly mistake. i was creating the file with a little typo:`open('compiler/recusrive_header.py','a')` corrected that and then import is success with just `import recusrive_header`. – RatDon Aug 28 '13 at 23:24

1 Answers1

2

You need an empty __init__.py file in \compiler (to tell python that compiler is a module) and then do:

import compiler.recursive_header

However If you are generating the module try generating it in a different module and loading that, i.e have the following structure:

python.exe
compiler
   __init__.py
   recursive.py
compiled
   __init__.py
   compiled_file_1.py
   compiled_file_2.py

For more detail on why this works the way it does see this post

Community
  • 1
  • 1
Mike Vella
  • 10,187
  • 14
  • 59
  • 86
  • same error is showing. – RatDon Aug 28 '13 at 23:21
  • Just to be clear - are you *generating* the python code from recursive.py? – Mike Vella Aug 28 '13 at 23:23
  • yes. as per recursive descent parser we have to create the functions based on the grammar provided. different recursive_header.py files for different grammars. – RatDon Aug 28 '13 at 23:25
  • by the way thanks. i didn't know about `__init__.py`, but learnt it today. i've solved my code. – RatDon Aug 28 '13 at 23:26
  • 1
    OK - the problem is **probably** that the python interpreter won't load the same package twice, even if you change the code - it will use the old `*.pyc` files, you will need to create a new package and load it every time you generate the code. I've edited my answer. If it solves your problem please accept, if it doesn't please post your own answer :) – Mike Vella Aug 28 '13 at 23:26
  • it's not having a problem. the script is generating the file, compiling it during the import and using the functions nicely. – RatDon Aug 28 '13 at 23:40
  • 1
    I'm glad, that's what I thought should happen :) . It's because your `python.exe compiler\recursive.py – Mike Vella Aug 28 '13 at 23:48