在Anaconda上安装CPLEX

问题描述

尝试安装CPLEX以进行优化。这就是发生的情况:

conda install -c ibmdecisionoptimization cplex
Collecting package Metadata (current_repodata.json): done
Solving environment: Failed with initial frozen solve. retrying with flexible solve.
Solving environment: Failed with repodata from current_repodata.json,will retry with next repodata source.
Collecting package Metadata (repodata.json): done
Solving environment: Failed with initial frozen solve. retrying with flexible solve.
Solving environment: |
Found conflicts! Looking for incompatible packages.
This can take several minutes.  Press CTRL-C to abort.
Failed

UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:

Specifications:

  - cplex -> python[version='2.7.*|3.5.*|>=2.7,<2.8.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0|>=3.5,<3.6.0a0']

Your python: python=3.8

If python is on the left-most side of the chain,that's the version you've asked for.
When python appears to the right,that indicates that the thing on the left is somehow
not available for the python version you are constrained to. Note that conda will not
change your python version to a different minor version unless you explicitly specify
that.

所以我想我安装了错误的Python版本。我该如何更改?

还有,这是在代码中使用它的正确方法吗?

import pulp
import cplex
prob.solve(pulp.CPLEX_CMD())

解决方法

最简单的解决方案是创建一个新的虚拟环境。在这种情况下,要指定您的python版本,可以在conda create命令中传递软件包版本。例如,如果我想使用python 3.6创建一个名为 env_w_python36 的新环境,请运行以下命令:

conda create -n env_w_python36 python=3.6

然后您可以检查您的环境中是否安装了正确的python版本:

$ conda activate env_w_python36
(env_w_python36) $ python --version
Python 3.6.12 :: Anaconda,Inc.

另一种解决方案可能是通过请求特定版本来在当前环境中降级/升级python版本:

conda install python=3.6

请注意,它可以与已安装的软件包产生很多冲突。每当您要使用其他版本的python时,创建新环境几乎总是解决之道。