0

私は USB スティックで Python を使用しており、再帰降下パーサーを設計しています。

メイン スクリプトは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_headerた が、エラーが表示されていimport 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

私はここで与えられた解決策を試しました。しかし、同じエラー。

も試した

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

ここでエラー番号が増加し、いくつかについて言及していsysます。

スクリプトに compiler\recursive_header.py をインポートするにはどうすればよいですか。

4

1 に答える 1

2

(Pythonにモジュールであることを伝えるため)に空の__init__.pyファイルが必要であり、次のようにします。\compilercompiler

import compiler.recursive_header

ただし、モジュールを生成している場合は、別のモジュールで生成してロードしてみてください。つまり、次の構造を持っています。

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

なぜこれが機能するのかについての詳細は、この投稿を参照してください

于 2013-08-28T23:16:22.363 に答える