如何从 python 中的父文件夹导入函数?
How to import a function from parent folder in python?
我需要在我的 python 项目中导入一个函数。
我知道在 SO 上有很多类似的问题,但是,不幸的是,我找不到适合我的解决方案,因为答案要么太具体,要么太笼统,或者它们只是丑陋的技巧(比如使用绝对路径操作)。
我的文件夹结构如下所示:
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: Parent module '' not loaded, cannot perform relative
import
当我在导入语句中使用 .
时(即 from .hashes.hash_function
)
或者我明白了:
ImportError: No module named 'hashes'
每个 __init__.py
文件都是空的。
这是我的尝试列表:
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: 无法在此处找到解决方案 whosebug.com/questions/14132789/
你有一个 __init__.py
的事实告诉我 PythonClient 本身就是一个库。做from PythonClient.hashes.hash_function import hash_function
。我总是喜欢完全限定的路径。
您还需要先安装您的库,然后才能从中导入。这需要在您的主目录中有一个 setup.py 文件。之后,您应该 pip 安装您的库以进行测试,例如,`pip install -e .
我知道你已经接受了一个答案,但是如果你想要一个更少的 "permanent" 解决方案(也就是说,如果你不想 install你的代码),另一种选择是简单地将 PythonClient 目录的父目录添加到你的路径中。这可以永久完成(取决于操作系统)或临时在代码中完成:
import os
import sys
p = os.path.abspath('../..')
if p not in sys.path:
sys.path.append(p)
from PythonClient.hashes.hash_function import hash_function
干杯!
我需要在我的 python 项目中导入一个函数。
我知道在 SO 上有很多类似的问题,但是,不幸的是,我找不到适合我的解决方案,因为答案要么太具体,要么太笼统,或者它们只是丑陋的技巧(比如使用绝对路径操作)。
我的文件夹结构如下所示:
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: Parent module '' not loaded, cannot perform relative import
当我在导入语句中使用 .
时(即 from .hashes.hash_function
)
或者我明白了:
ImportError: No module named 'hashes'
每个 __init__.py
文件都是空的。
这是我的尝试列表:
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: 无法在此处找到解决方案 whosebug.com/questions/14132789/
你有一个 __init__.py
的事实告诉我 PythonClient 本身就是一个库。做from PythonClient.hashes.hash_function import hash_function
。我总是喜欢完全限定的路径。
您还需要先安装您的库,然后才能从中导入。这需要在您的主目录中有一个 setup.py 文件。之后,您应该 pip 安装您的库以进行测试,例如,`pip install -e .
我知道你已经接受了一个答案,但是如果你想要一个更少的 "permanent" 解决方案(也就是说,如果你不想 install你的代码),另一种选择是简单地将 PythonClient 目录的父目录添加到你的路径中。这可以永久完成(取决于操作系统)或临时在代码中完成:
import os
import sys
p = os.path.abspath('../..')
if p not in sys.path:
sys.path.append(p)
from PythonClient.hashes.hash_function import hash_function
干杯!