【发布时间】:2016-08-05 22:52:21
【问题描述】:
我需要在我的 python 项目上执行一个函数的导入。
我知道关于 SO 有许多类似的问题,但是,不幸的是,我找不到适合我的解决方案,因为答案要么过于具体,要么过于笼统,要么只是丑陋的 hack (就像使用绝对路径操作一样)。
我的文件夹结构如下所示:
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
我需要从./diffie_hellman/diffie_hellman.py 导入./hashes/hash_function.py。
./hashes/hash_function.py 文件包含唯一名为 hash_function 的函数。
我已经尝试了很多方法来执行导入,但就是无法做到。 我总是得到任何一个
SystemError: 父模块''未加载,无法执行相对 进口
当我在导入语句中使用. 时(即from .hashes.hash_function)
或者我明白了:
ImportError: 没有名为“哈希”的模块
每个__init__.py 文件都是空的。
这是我的尝试列表:
from hashes import hash_functionfrom hashes.hash_function import hash_functionfrom .hashes.hash_function import hash_functionfrom ..hashes.hash_function import hash_functionimport hashesimport hash_functionfrom .. import hash_functionfrom . import hash_functionfrom PythonClient.hashes.hash_function import hash_function
您能帮我解决我的问题并了解如何使用此类导入吗?
PS:这里找不到解决方案stackoverflow.com/questions/14132789/
【问题讨论】:
-
我建议你先阅读stackoverflow.com/questions/14132789/…。仔细阅读,99%的python导入问题都能解决。
标签: python python-3.x python-import