2016-06-08 31 views
0

我正在寫一些依賴於C++庫(我正在使用Boost.Python)的python代碼。我使用cmake構建並將庫dll和pyd文件放入bin。我的文件夾結構,看起來有點像這樣:運行與dll/pyd依賴項的python演示腳本

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等環境進行調試。

回答

0

最後,我滿足於將bin文件夾添加到Python編程路徑與

import os, sys 
moduleDir = os.path.dirname(os.getcwd()) + os.sep + "bin" 
sys.path.insert(0, moduleDir) 

它解決了在短期內我的問題,雖然它並不好看。