从便携式python中的文件夹导入模块

时间:2013-08-28 23:14:18

标签: python module

我在一个usb棒中蟒蛇,我正在设计一个recursive descent parser

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

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

我的目录结构是

python.exe
compiler\
    recursive.py
    rd_input

在我的代码中,我正在生成一个包含3个函数的python脚本。

compiler\   
    recursive_header.py

我需要稍后在主脚本recursive.py中导入。

我已尝试import recursive_headerimport compiler\recursive_header以及import compiler/recursive_header 它显示错误

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

我尝试了here给出的解决方案。但同样的错误。

也尝试了

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

此处错误编号增加,提及一些sys

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

1 个答案:

答案 0 :(得分: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

有关其原因的详细信息,请参阅this post

相关问题