2013-08-28 68 views
0

我已经在USB棒python,我设计了一个recursive descent parser从便携式Python中的文件夹导入模块

主要脚本是recursive.py它是由命令提示符下面的代码运行。

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

我的目录结构是

python.exe 
compiler\ 
    recursive.py 
    rd_input 
在我的代码

我生成一个Python脚本,用3种功能。

compiler\ 
    recursive_header.py 

我需要导入主脚本recursive.py以后。

我试过import recursive_headerimport compiler\recursive_headerimport compiler/recursive_header 它的显示我已经试过给here解决错误

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

。但同样的错误。

也试过

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

这里的错误数量增加提了一些关于sys

如何在我的脚本中导入编译器\ recursive_header.py。

+0

抱歉愚蠢的错误。我用一个小错字创建了这个文件:'open('compiler/recusrive_header.py','a')'纠正了这个错误,然后导入成功只用'import recusrive_header'。 – RatDon

回答

2

你需要一个空__init__.py文件\compiler(来告诉Python这compiler是一个模块),然后执行:

import compiler.recursive_header 

但是如果你是产生模块尝试在不同的生成它模块和加载,即具有以下结构:

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

有关为什么th的更多细节是工作方式它看到的this post

+0

显示同样的错误。 – RatDon

+0

只是要清楚 - 你是否从recursive.py中生成python代码? –

+0

是的。根据递归下降解析器,我们必须根据提供的语法创建函数。不同语法的不同recursive_header.py文件。 – RatDon