简体   繁体   中英

QuantLib-SWIG-1.19 python 'from . import _QuantLib' fails

I downloaded QuantLib-SWIG-1.19 for Windows 10 .

I was able to build and install the python version.

But it fails the test when it tries to run 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

called the following commands

python setup.py build
python setup.py install
python setup.py test

python setup.py test failed for build_ext

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 created a folder with a temp dir and a lib dir:

    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 adds:

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

So I tried to import QuantLib:

I opened an admin command prompt window and it works:

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'
>>>

But then I cd C:\\Users\\admuser\\Workspace\\QuantLib-SWIG-1.19\\Python

(I need to cd there to be able to run 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)
>>>
´´´

In my description above, you can import Quantlib from the command line python prompt if you are NOT in the QuantLib-SWIG-1.19\\Python directory.

This is because python looks in the site-packages folder which does have the bootstrapping file _QuantLib.py.

If you are in the QuantLib-SWIG-1.19\\Python directory it looks in the QuantLib-SWIG-1.19\\Python\\QuantLib directry which does not have _QuantLib.py or the associated _QuantLib.cp38-win_amd64.pyd file.

To get this to work we loaded the _QuantLib module directly:

From QuantLib.py

# Import the low-level C/C++ module
if __package__ or "." in __name__:
    from . import _QuantLib
else:
    import _QuantLib

Above the statement 'from . import _QuantLib' fails to find _QuantLib

So we try to load it directly:

# 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

I probably should detect if this is the site-packages version of the QuantLib module and just call 'from . import _QuantLib'

We need to set PYTHONPATH (if we are not using the site_packages version):

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM