我目前正在使用python使用Gurobi求解器。以前,我刚刚使用文本编辑器和终端的混合来编写和运行我的模型,但我目前正在尝试使用Enthought Canopy Editor。我有一个模型,当我使用python model.py
在终端中运行时,但是当我尝试在Canopy的iPython shell中执行%run model.py
时,我得到:
%run /home/cdhagmann/Copy/Code_Env/Farmer/model.py
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/home/cdhagmann/Canopy/appdata/canopy-1.0.1.1189.rh5-x86_64/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
181 else:
182 filename = fname
--> 183 __builtin__.execfile(filename, *where)
/home/cdhagmann/Copy/Code_Env/Farmer/model.py in <module>()
----> 1 from gurobipy import *
2 from data_IO import read_data,currency
3 import time
4
5 # Import model data
ImportError: No module named gurobipy
我的猜测是终端和Canopy在不同的地方寻找模块,但我不知道如何纠正它。我在Gurobi小组网站上找到了关于Enthought Canopy and Gurobi on OS X 10.8的参考资料,但不知道这些指令与在Linux上做的有多大差别,甚至不知道他在Canopy的网站包装中安装Gurobi的意义。
更新:根据我点击的第一个链接找到的信息,我确实留下了盒装点击使Canopy成为我的默认目录。您必须手动添加PATH变量,我错过了他们说我必须这样做。所以我做到了。我现在在我的终端上运行Canopy的iPython版本(使用sys.path检查);但是,当我尝试运行python时,我得到了一个巨大的错误列表。
cdhagmann@Crispin ~ $ python
Traceback (most recent call last):
File "/usr/lib/python2.7/site.py", line 563, in <module>
main()
File "/usr/lib/python2.7/site.py", line 545, in main
known_paths = addusersitepackages(known_paths)
File "/usr/lib/python2.7/site.py", line 272, in addusersitepackages
user_site = getusersitepackages()
File "/usr/lib/python2.7/site.py", line 247, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/usr/lib/python2.7/site.py", line 237, in getuserbase
USER_BASE = get_config_var('userbase')
File "/usr/lib/python2.7/sysconfig.py", line 578, in get_config_var
return get_config_vars().get(name)
File "/usr/lib/python2.7/sysconfig.py", line 505, in get_config_vars
import re
File "/usr/lib/python2.7/re.py", line 105, in <module>
import sre_compile
File "/usr/lib/python2.7/sre_compile.py", line 14, in <module>
import sre_parse
File "/usr/lib/python2.7/sre_parse.py", line 17, in <module>
from sre_constants import *
File "/usr/lib/python2.7/sre_constants.py", line 18, in <module>
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
连连呢?
回答@Jonathan问题:
sys.prefix
= '/usr'
sys.path
:
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages/PILcompat
/usr/lib/python2.7/dist-packages/gtk-2.0
/usr/lib/pymodules/python2.7
/usr/lib/python2.7/dist-packages/ubuntu-sso-client
gurobipy.__file__
= '/usr/local/lib/python2.7/dist-packages/gurobipy/gurobipy.so'
答案 0 :(得分:1)
如果为Canopy Python正确配置了终端,那么终端中的python和Canopy Python应该在同一个地方寻找模块。如果在安装过程中,您禁用了“将Canopy设置为默认Python”,或者如果您在安装后未重新启动终端,则不会自动正确配置它。
这些文章可以帮助您理解和配置:
如果这些文章不足以让你解决这个问题,请报告 - 当你从终端运行python时,这些是什么?
sys.prefix
sys.path
gurobipy.__file__