私はPythonプロジェクトで関数のインポートを実行する必要があります。Pythonの親フォルダから関数をインポートするには?
私は答えがあまりにも具体的すぎる、あまりにも一般的すぎる、またはちょうど醜いので、残念なことに、残念なことに、私は私のための適切な解決策を見つけることができませんでしたハッキング(絶対パスでの操作など)私は./diffie_hellman/diffie_hellman.py
から./hashes/hash_function.py
をインポートする必要が
PythonClient:.
│ .gitignore
│ des.py
│ des_test.py
│ des_var2.py
│ gui.py
│ index.py
│ __init__.py
│
├───diffie_hellman
│ │ diffie_hellman.py
│ │ diffie_hellman_test.py
│ │ __init__.py
│ │
│ └───__pycache__
│ diffie_hellman.cpython-35.pyc
│
├───hashes
│ │ collision.py
│ │ hash_function.py
│ │ __init__.py
│ │
│ └───__pycache__
│ hash_function.cpython-35.pyc
│ __init__.cpython-35.pyc
│
└───__pycache__
des.cpython-35.pyc
des_var2.cpython-35.pyc
:ここ
は次のように私のフォルダ構造がどのように見えるかです。
./hashes/hash_function.py
ファイルには、hash_function
という名前の唯一の機能が含まれています。
私はかなり多くの方法でインポートを実行しようとしましたが、できませんでした。 私はいつものいずれかを取得
SystemError: Parent module '' not loaded, cannot perform relative import
私は私のimport文(すなわちfrom .hashes.hash_function
)
またはIで.
を使用し、この取得:すべての__init__.py
ファイルが空である
ImportError: No module named 'hashes'
を。
ここに私の試みのリストです:
from hashes import hash_function
from hashes.hash_function import hash_function
from .hashes.hash_function import hash_function
from ..hashes.hash_function import hash_function
import hashes
import hash_function
from .. import hash_function
from . import hash_function
from PythonClient.hashes.hash_function import hash_function
私の問題を解決し、そのような輸入品で作業する方法を理解するのを助けてください。
PS:ソリューションはここstackoverflow.com/questions/14132789/
最初にhttp://stackoverflow.com/questions/14132789/python-relative-imports-for-the-billionth-time#answer-14132912を読むことをおすすめします。あなたがそれを十分に慎重に読むと、Pythonのインポートの質問の99%を解決することができます。 – laike9m