[英]QuantLib-SWIG-1.19 python 'from . import _QuantLib' fails
我下载了适用于 Windows 10 的 QuantLib-SWIG-1.19。
我能够构建和安装 python 版本。
但是当它尝试运行 build_ext 时它没有通过测试。
SET QL_DIR=C:\Users\admuser\Workspace\QuantLib-1.19
SET INCLUDE=C:\Users\admuser\ThirdParty\boost_1_74_0;%INCLUDE%
SET LIB=C:\Users\admuser\ThirdParty\boost_1_74_0;%LIB%
set PATH=%PATH%;C:\Users\admuser\ThirdParty\swigwin\swigwin-4.0.2
cd C:\Users\admuser\Workspace\QuantLib-SWIG-1.19\Python
调用以下命令
python setup.py build
python setup.py install
python setup.py test
build_ext 的 python setup.py 测试失败
running test
running build
running build_py
running build_ext
Traceback (most recent call last):
File "setup.py", line 237, in <module>
setup(name = "QuantLib",
File "C:\Users\admuser\ThirdParty\WinPython\python-3.8.2.amd64\lib\site-packages\setuptools\__init__.py", line 144, in setup
return distutils.core.setup(**attrs)
File "C:\Users\admuser\ThirdParty\WinPython\python-3.8.2.amd64\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\Users\admuser\ThirdParty\WinPython\python-3.8.2.amd64\lib\distutils\dist.py", line 966, in run_commands
self.run_command(cmd)
File "C:\Users\admuser\ThirdParty\WinPython\python-3.8.2.amd64\lib\distutils\dist.py", line 985, in run_command
cmd_obj.run()
File "setup.py", line 61, in run
module = __import__('QuantLibTestSuite', globals(), locals(), [''])
File "test\QuantLibTestSuite.py", line 22, in <module>
from date import DateTest
File "test\date.py", line 18, in <module>
import QuantLib as ql
File "build\lib.win-amd64-3.8\QuantLib\__init__.py", line 21, in <module>
from .QuantLib import *
File "build\lib.win-amd64-3.8\QuantLib\QuantLib.py", line 13, in <module>
from . import _QuantLib
ImportError: DLL load failed while importing _QuantLib: The parameter is incorrect.
setup.py build 创建了一个带有临时目录和 lib 目录的文件夹:
QuantLib-SWIG-1.19\Python\build\lib.win-amd64-3.8\QuantLib
which contains
__init__.py
_QuantLib.cp38-win_amd64.pyd
QuantLib.py
setup.py install 添加:
QuantLib-SWIG-1.19\Python\QuantLib.egg-info
QuantLib-SWIG-1.19\Python\build\lib.win-amd64-3.8\QuantLib\__pycache__
python-3.8.2.amd64\Lib\site-packages\QuantLib-1.19-py3.8-win-amd64.egg
python-3.8.2.amd64\Lib\site-packages\QuantLib-1.19-py3.8-win-amd64.egg\QuantLib
which contains:
__pycache__ folder
__init__.py
_QuantLib.cp38-win_amd64.pyd
_QuantLib.py
QuantLib.py
所以我尝试导入 QuantLib:
我打开了一个管理命令提示符窗口,它可以工作:
C:\WINDOWS\system32>python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import QuantLib as ql
>>> QuantLib.__file__
'C:\\Users\\admuser\\ThirdParty\\WinPython\\python-3.8.2.amd64\\lib\\site-packages\\quantlib-1.19-py3.8-win-amd64.egg\\QuantLib\\__init__.py'
>>>
但后来我 cd C:\\Users\\admuser\\Workspace\\QuantLib-SWIG-1.19\\Python
(我需要在那里 cd 才能运行 python setup.py)
C:\Users\admuser\Workspace\QuantLib-SWIG-1.19\Python>python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>import QuantLib as ql
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\admuser\Workspace\QuantLib-SWIG-1.19\Python\QuantLib\__init__.py", line 21, in <module>
from .QuantLib import *
File "C:\Users\admuser\Workspace\QuantLib-SWIG-1.19\Python\QuantLib\QuantLib.py", line 13, in <module>
from . import _QuantLib
ImportError: cannot import name '_QuantLib' from partially initialized module 'QuantLib' (most likely due to a circular import) (C:\Users\admuser\Workspace\QuantLib-SWIG-1.19\Python\QuantLib\__init__.py)
>>>
´´´
在我上面的描述中,如果您不在 QuantLib-SWIG-1.19\\Python 目录中,则可以从命令行 python 提示符导入 Quantlib。
这是因为 python 在 site-packages 文件夹中查找,该文件夹具有引导文件 _QuantLib.py。
如果您在 QuantLib-SWIG-1.19\\Python 目录中,它会在 QuantLib-SWIG-1.19\\Python\\QuantLib 目录中查找,该目录没有 _QuantLib.py 或关联的 _QuantLib.cp38-win_amd64.pyd 文件。
为了让它工作,我们直接加载了 _QuantLib 模块:
来自 QuantLib.py
# Import the low-level C/C++ module
if __package__ or "." in __name__:
from . import _QuantLib
else:
import _QuantLib
上面的语句'来自 . 导入 _QuantLib' 未能找到 _QuantLib
所以我们尝试直接加载:
# Import the low-level C/C++ module
if __package__ or "." in __name__:
import importlib.util
import sys
_quantLib_spec=importlib.util.find_spec("_QuantLib")
if _quantLib_spec is None:
print("cannot find _QuantLib (bootstrap) module")
print(sys.path)
else:
_QuantLib=importlib.util.module_from_spec(_quantLib_spec)
_quantLib_spec.loader.exec_module(_QuantLib)
sys.modules[_QuantLib] = _QuantLib
else:
import _QuantLib
我可能应该检测这是否是 QuantLib 模块的站点包版本,然后调用 'from . 导入_QuantLib'
我们需要设置 PYTHONPATH(如果我们没有使用 site_packages 版本):
set PYTHONPATH=C:\\admuser\\OneDrive\\ThirdParty\\WinPython\\python-3.8.2.amd64\\lib\\site-packages\\quantlib-1.19-py3.8-win-amd64.egg\\QuantLib
C:\Users\admuser\Workspace\QuantLib-SWIG-1.19\Python>python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import QuantLib
>>> exit()
C:\Users\admuser\Workspace\QuantLib-SWIG-1.19\Python>python examples\swap.py
0.0443
NPV = 19066.26
Fair spread = -0.4174 %
Fair rate = 4.4300 %
NPV = 19066.26
Fair spread = -0.4174 %
Fair rate = 4.4300 %
NPV = 40533.04
Fair spread = -0.9241 %
Fair rate = 4.9520 %
NPV = 37144.28
Fair spread = -0.8469 %
Fair rate = 4.8724 %
NPV = 26604.08
Fair spread = -0.5825 %
Fair rate = 4.6000 %
NPV = 46439.43
Fair spread = -1.0588 %
Fair rate = 5.0907 %
NPV = 26604.08
Fair spread = -0.5825 %
Fair rate = 4.6000 %
NPV = 43050.66
Fair spread = -0.9815 %
Fair rate = 5.0111 %
C:\Users\admuser\Workspace\QuantLib-SWIG-1.19\Python>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.