Setuptools / distutils:在Windows上将文件安装到发行版的DLL目录

5 投票
1 回答
2814 浏览
提问于 2025-04-17 02:59

我正在写一个setup.py文件,使用setuptools/distutils来安装我写的Python包。这个包需要安装两个DLL文件(其实是一个DLL文件和一个PYD文件),并把它们放到Python可以加载的位置。我认为这个位置是我Python安装目录下的DLLs文件夹(比如说c:\Python27\DLLs)。

我使用了data_files选项来安装这些文件,使用pip时一切正常:

data_files=[(sys.prefix + "/DLLs", ["Win32/file1.pyd", "Win32/file2.dll"])]

但是用easy_install时我遇到了以下错误:

error: Setup script exited with error: SandboxViolation: open('G:\\Python27\\DLLs\\file1.pyd', 'wb') {}
The package setup script has attempted to modify files on your system that are not within the EasyInstall build area, and has been aborted.

那么,安装这些文件的正确方法是什么呢?

1 个回答

2

我通过以下几个步骤解决了这个问题:
1. 所有的数据文件路径都改成了相对路径。

data_files=["myhome", ["Win32/file1.pyd", "Win32/file2.dll"])]

2. 我尝试在包的初始化文件中找到“myhome”的位置,这样我就能使用它们。这需要写一些复杂的代码,因为它们要么在当前Python的根目录下,要么在一个专门为这个包创建的egg目录下。所以我只是查看哪个目录存在。

POSSIBLE_HOME_PATH = [
    os.path.join(os.path.dirname(__file__), '../myhome'), 
    os.path.join(sys.prefix, 'myhome'),
]
for p in POSSIBLE_HOME_PATH:
    myhome = p
    if os.path.isdir(myhome) == False:
        print "Could not find home at", myhome
    else:
       break

3. 然后我需要把这个目录添加到路径中,这样我的模块才能从那里加载。

sys.path.append(myhome) 

撰写回答