Python: 为什么我在 MacOSX 上导入 glpk 时会遇到这个错误?

Python: Why am I encountering this error with importing glpk on MacOSX?

我按照这些步骤操作并遇到了第一个错误:

wget http://www.dcc.fc.up.pt/~jpp/code/python-glpk/python-glpk_0.4.43.orig.tar.gz
tar -xzf python-glpk_0.4.43.orig.tar.gz
cd python-glpk-0.4.43/src/
sudo make install

我收到这个错误:

make -C swig all
make[1]: pyversions: Command not found
gcc -Wall -c -fPIC glpkpi_wrap.c -DHAVE_CONFIG_H -I/usr/include/ -I/usr/lib//config
glpkpi_wrap.c:130:11: fatal error: 'Python.h' file not found
# include <Python.h>
          ^
1 error generated.
make[1]: *** [glpkpi_wrap.o] Error 1
make: *** [all] Error 2

然后我通过更改 Python 版本链接到 python 或者让我们说部分: 里面 swig/Makefile

已更改

PYVERS := $(shell pyversions -d)

PYVERS := "Python 2.7.6" 

这是我的 python 版本

make -C swig all
swig -python  glpkpi.i
./glpk.h:916: Warning 314: 'in' is a python keyword, renaming to '_in'
sed -i 's/:in /:_in /g' glpkpi.py
sed: 1: "glpkpi.py": extra characters at the end of g command
make[1]: *** [glpkpi.py] Error 1
make: *** [all] Error 2

现在,当我 运行 python test.py 在示例文件夹中时,它会崩溃并显示:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/glpk/__init__.py", line 26, in <module>
    from glpk_parser import *
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/glpk/glpk_parser.py", line 352, in <module>
    yacc.yacc(write_tables=0, debug=0)
  File "/Library/Python/2.7/site-packages/ply/yacc.py", line 3244, in yacc
    read_signature = lr.read_table(tabmodule)
  File "/Library/Python/2.7/site-packages/ply/yacc.py", line 1967, in read_table
    if parsetab._tabversion != __tabversion__:
AttributeError: 'module' object has no attribute '_tabversion'

我尝试了以下教程: Build and install from source

然后我尝试了

Installing-Python-glpk Tutorial

有些不对劲,我缺少哪一步导致

import glpk

失败?

我也尝试了 brew 安装,没有成功!我也包含了我的 Python 路径,仍然没有帮助。

啊,最后我试了这个:

import sys    

sys.path.append('/Library/Python/2.7/site-packages/glpk/')

仍然没有运气!最后一步的灵感来自这个 answer

好的,我尝试重现您的问题并遇到了同样的错误, 我设法通过将 ply 的版本从 3.6 更改为 3.4.

使其工作

请注意,我在 glpk 库中遇到错误,不得不将其降级为 4.43