-1

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.

1
  • 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. Commented Aug 28, 2013 at 23:24

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

7 Comments

same error is showing.
Just to be clear - are you generating the python code from recursive.py?
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.
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 :)
I'm glad, that's what I thought should happen :) . It's because your python.exe compiler\recursive.py<compiler\rd_input command is not importing a module.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.