基本上我正在尝试从savReaderWriter模块运行一些Python代码,以便创建一个准备在IBM SPSS中打开的.sav文件。作为macOS用户,我需要先在终端中运行这两行,才能使模块正常工作:
echo 'export DYLD_LIBRARY_PATH=/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/spssio/macos' >> ~/.bash_profile
echo 'export LC_ALL=en_US.UTF-8' >> ~/.bash_profile
下面你可以看到我试图在Python中运行的一段代码:
import savReaderWriter
savFileName = "someFile.sav"
records = [['Test1', 1, 1], ['Test2', 2, 1]]
varNames = ['var1', 'v2', 'v3']
varTypes = {'var1': 5, 'v2': 0, 'v3': 0}
with savReaderWriter.SavWriter(savFileName, varNames, varTypes, ioUtf8=True) as writer:
for record in records:
writer.writerow(record)
我的问题是,当通过terminal.app运行Python代码时,就像魅力和新的.sav文件一样,尝试在IDE中执行相同的代码(尝试过PyCharm和Spyder)会给我一个错误:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2847, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-94007b092d47>", line 7, in <module>
with savReaderWriter.SavWriter(savFileName, varNames, varTypes, ioUtf8=True) as writer:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/savWriter.py", line 198, in __init__
super(Header, self).__init__(savFileName, ioUtf8, ioLocale)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/generic.py", line 29, in __init__
self.spssio = self.loadLibrary()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/generic.py", line 117, in loadLibrary
spssio = self._loadLibs("macos")
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/generic.py", line 89, in _loadLibs
return [load(os.path.join(path, lib)) for lib in libs][-1]
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/generic.py", line 89, in <listcomp>
return [load(os.path.join(path, lib)) for lib in libs][-1]
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ctypes/__init__.py", line 348, in __init__
self._handle = _dlopen(self._name, mode)
OSError: dlopen(/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/spssio/macos/libicuuc48.1.dylib, 6): Library not loaded: @executable_path/../lib/libicudata48.1.dylib
Referenced from: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/spssio/macos/libicuuc48.1.dylib
Reason: image not found
模块作者无法帮助我解决这个问题,因此我很乐意接受这个社区的任何建议。
编辑(添加了sys.path):
从终端:
['',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages',
'/Users/mg/mne-python']
来自IDE:
['/Applications/PyCharm.app/Contents/helpers/pydev',
'/Users/mg/Documents/Python/Projects/MD',
'/Applications/PyCharm.app/Contents/helpers/pydev',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages',
'/Users/mg/mne-python',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/extensions',
'/Users/mg/Documents/Python/Projects/MD']
此致
MG
答案 0 :(得分:2)
找到了解决方案!
基本上我需要创建出现在错误中的每个dylib的符号链接,例如:
sudo ln -s /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/spssio/macos/libicudata48.1.dylib /usr/local/lib/libicudata48.1.dylib
sudo ln -s /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/spssio/macos/libicui18n48.1.dylib /usr/local/lib/libicui18n48.1.dylib
sudo ln -s /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/spssio/macos/libspssdio.dylib /usr/local/lib/libspssdio.dylib
sudo ln -s /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/savReaderWriter/spssio/macos/libzlib123spss.dylib /usr/local/lib/libzlib123spss.dylib
理论上说:
sudo ln -s /path/to/original /path/to/symbolic/link
此致
MG
答案 1 :(得分:1)
我将为这个问题添加一个答案,以使其更具通用性。本来希望发表评论,但没有代表!尽管Maciej完全正确,并且他的回答帮助我找到了问题,但savReaderWriter已更新。从3.4.2版开始,现在有6个.dlyb文件要复制。
一旦您拥有文件夹的路径(路径是错误消息的位置),请确保在创建符号链接之前列出(在终端窗口中的ls
)该文件夹内的所有文件。然后为每个.dylib
再次感谢Maciej!
答案 2 :(得分:0)
我不是Mac用户,但你真的使用Python 3.6吗? savReaderWriter是否支持该版本?我怀疑它使用的I / O模块是为该版本构建的。
如果这通过终端而不是IDE工作,请检查Python搜索路径是否相同。
答案 3 :(得分:0)
对于其他懒惰的人:
ls /Users/your_user/anaconda/envs/quattro8/lib/python2.7/site-packages/savReaderWriter/spssio/macos/ | xargs -I {} sudo ln -s /Users/your_user/anaconda/envs/quattro8/lib/python2.7/site-packages/savReaderWriter/spssio/macos/{} /usr/local/lib/{}
这会链接该仓库中的所有软件包
答案 4 :(得分:0)
您还可以在PyCharm中指定库的路径,从而无需创建符号链接:
从菜单栏中单击“运行”->“编辑配置”。在左窗格中,选择python脚本的配置(通常PyCharm为每个.py文件创建一个配置)。将库的路径(如错误消息中所示)插入“环境变量”中。
示例:
DYLD_LIBRARY_PATH=/Users/username/miniconda3/envs/savreader/lib/python3.6/site-packages/savReaderWriter/spssio/macos;
答案 5 :(得分:0)
升级到Mac OS Catalina后,将所有六个dylib文件复制到/usr/local/lib/
不再起作用(至少对我而言)。为了找出正确的目的地,我做了以下事情:
ctypes/__init__.py
print(os.getcwd())
以获取工作目录。对我来说,工作目录为/Users/Username/GitHub/myproject
@executable_path/../lib/
)的,因此dylib文件的正确目的地是/Users/Username/lib