我正在使用Python3.6。我已经用(PybDun11)创建了一个C++扩展名。https://github.com/pybind/pybind11]. 我将编译的*.pyd
文件以及依赖的dll复制到站点包中。但是当我试图从外部DLL加载任何函数时,python抱怨函数不存在。如果我想访问函数,我需要写
sys.path.append(r'C:\Users\test\AppData\Local\Programs\Python\Python36\Lib\site-packages\CppProject')
或者我需要向PYTHONPATH
环境变量添加相同的路径
为什么Python不能加载函数,即使它与pyd位于同一路径中?我不想每次需要使用模块或环境变量时都附加sys路径?有没有办法避免这种情况?是否有任何方法可以在用户导入模块时自动将此路径添加到sys
例如:
CppExport.dll
#ifdef CPPEXPORT_EXPORTS
#define CPPEXPORT_API __declspec(dllexport)
#else
#define CPPEXPORT_API __declspec(dllimport)
#endif
extern "C" CPPEXPORT_API double sin_impl(double x);
const double e = 2.7182818284590452353602874713527;
double sin_impl(double x){
return (1 - pow(e, (-2 * x))) / (2 * pow(e, -x));
}
cpproject.pyd
PYBIND11_MODULE(CppProject, m) {
m.def("sin_impl", &sin_impl, R"pbdoc(
Compute a hyperbolic tangent of a single argument expressed in radians.
)pbdoc");
#ifdef VERSION_INFO
m.attr("__version__") = VERSION_INFO;
#else
m.attr("__version__") = "dev";
#endif
}
Setup.py
from setuptools import setup
import distutils
import sys
from setuptools.dist import Distribution
from distutils.sysconfig import get_python_lib
relative_site_packages = get_python_lib().split(sys.prefix+os.sep)[1]
date_files_relative_path = os.path.join(relative_site_packages, "CppProject")
class BinaryDistribution(Distribution):
"""Distribution which always forces a binary package with platform name"""
def has_ext_modules(foo):
return True
setup(
name='CppProject',
version='1.0',
description='CppProject Library',
packages=['CppProject'],
package_data={
'CppProject': ['CppProject.pyd'],
},
data_files = [(date_files_relative_path, ["CppExport.dll"])],
distclass=BinaryDistribution
)
在Python中:
from CppProject import sin_impl
错误:
ImportError: cannot import name 'sin_impl'
完整代码出现在Github中
当您显式地将目录添加到
sys.path
时,代码可以工作,这一事实是理解发生了什么的关键由于
site-packages
是解释器在导入模块时搜索的位置之一,因此此语句:实际上正在
CppProject
文件夹中搜索名为sin_impl
的模块相反,你应该:
它指向同名的实际模块
实际上,这并不需要在
CppProject
内部存在__init__.py
来将其限定为Python包,因为Python 3.3+实现了隐式命名空间包但是,当您构建具有许多依赖项的复杂程序时,包构造函数允许您在执行任何常规模块之前添加某种要执行的初始化
对不起,前面的回复,这里有一些更好的建议:
要分发库,需要创建setup.py和init.py。完成此操作后,您将能够使用
python setup.py install
安装软件包对于我来说,setup.py看起来像:
在init.py中,您必须找到您的库并导入它。以下是Qt的一个示例:
希望这有帮助
相关问题 更多 >
编程相关推荐