我正在尝试在PyCharm中运行google ortools示例,并出现以下错误:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap onChange Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/davidstutz/bootstrap-multiselect/dist/js/bootstrap-multiselect.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/davidstutz/bootstrap-multiselect/dist/css/bootstrap-multiselect.css" type="text/css"/>
<script type="text/javascript">
$(document).ready(function() {
$('#example').multiselect(
{
enableClickableOptGroups: true,
onChange:function(option, checked, value)
{
//console.log(option);
console.log($.map(option, x=>{return $(x).val();}));
//console.log($.map(option, x=>{return $(x).attr("id");}));
if(option.length > 1){
//if I have more than one picked, then return the ID of the first-items group
console.log($(option[0]).closest("optgroup").attr("id"));
}
// how to access option.???
//console.log($(option).val());
// Al Bundy extension:
optionId = Array.isArray(option)
? $(option[0]).closest("optgroup").attr("id")
: option.attr("id");
}
}
)
});
</script>
</head>
<body>
<select id="example" multiple="multiple">
<optgroup label="MyGroup" id='YourGroup'>
<option id="opt1" value="1">Option 1</option>
<option id="opt2" value="2">Option 2</option>
<option id="opt3" value="3">Option 3</option>
</optgroup>
</select>
</body>
</html>
但是,如果我在PyCharm外部(在相应的项目venv下)运行相同的代码,则效果很好:
/Users/edamame/project/ortools/simple_program.py
Traceback (most recent call last):
File "/Users/edamame/project/ortools/simple_program.py", line 3, in <module>
from ortools.linear_solver import pywraplp
ModuleNotFoundError: No module named 'ortools.linear_solver'
Process finished with exit code 1
然后在两种情况下我都尝试在下面进行打印:
(venv) edamame$ pwd
/Users/edamame/project/ortools/
(venv) edamame$ python simple_program.py
Solution:
Objective value = 4.0
x = 1.0
y = 1.0
输出:
在PyCharm中:
import sys
print('\n'.join(sys.path))
外部PyCharm:
/Users/edamame/project/venv/bin/python
/Users/edamame/project/ortools/so_example.py
/Users/edamame/project/ortools
/Users/edamame/project
/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
/Users/edamame/project/venv/lib/python3.6/site-packages
/Users/edamame/project/venv/lib/python3.6/site-packages/setuptools-39.1.0-py3.6.egg
/Users/edamame/project/venv/lib/python3.6/site-packages/pip-10.0.1-py3.6.egg
尽管它们似乎相同...不确定可能是什么问题...
如何在PyCharm中使用相同的代码?
答案 0 :(得分:1)
环境变量PYTHONPATH是相关的。在两种情况下,都输出sys.path
的值:
import sys
print('\n'.join(sys.path))
在bash中,输入venv,然后输入$ open /Applications/PyCharm.app
(或Mac上的任何名称)可能会有所帮助。然后,正在运行的pycharm将从bash继承PYTHONPATH。
这是另一种方法。在pycharm类型CMD-(命令逗号)中,选择首选项,搜索Project Structure,右键单击edamame / project文件夹,然后选择alt-S Sources。这会将文件夹添加到pycharm中的路径中。再次使用print()
验证sys.path
包含您期望包含的内容。
答案 1 :(得分:1)
自定义ortools
软件包与已安装的软件包之间存在命名空间冲突。 PyCharm默认将项目根目录(/Users/edamame/project
添加到PYTHONPATH
,这会引起问题。
重命名您的自定义ortools
软件包,或在PYCharm的专用“运行配置”中禁用将内容根添加到PYTHONPATH 选项。