使用dll / pyd依赖项运行python演示脚本

时间:2016-06-08 19:59:03

标签: python c++ dll

我正在编写一些依赖于c ++库的python代码(我使用的是Boost.Python)。我使用cmake构建并将库dll和pyd文件放入bin。我的文件夹结构看起来有点像这样:

Project
|-- bin: folder where test executables, dll, and pyd files get put after build
|-- cpp: all cpp/h files to build the dll
|-- python: the cpp files with the boost.python interface as well as python code that depends on it (and the dlls)
|-- test: cpp code with tests

要运行我的python文件,我需要与python脚本位于同一目录中的dll和pyd文件。目前,我有cmake将python中的.py文件复制到bin中供我运行和测试。但是,我真的不喜欢这样,因为它很丑陋而且很麻烦。我读到我可以将我的bin文件夹添加到PYTHONPATH和PATH变量来解决这个问题,但这会让我认为我的python代码有点难看。

我是Python的新手;因此,我想知道是否有更好的方法来处理这个问题。理想情况下,我想从python文件夹运行我的python脚本/模块,这样我就可以从像PyCharm或Visual Studio这样的环境进行调试。

1 个答案:

答案 0 :(得分:0)

最后,我决定使用

以编程方式将bin文件夹添加到python路径
import os, sys
moduleDir = os.path.dirname(os.getcwd()) + os.sep + "bin"
sys.path.insert(0, moduleDir)

它在短期内解决了我的问题,虽然看起来并不好看。