Python 如何降级numpy?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51912284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to downgrade numpy?
提问by James Lim
I got error
我有错误
TypeError: slice indices must be integers or None or have an __index__
method
and searched for a solution and got that i need to downgrade the version of numpy , then tried to use this command
并搜索了一个解决方案,发现我需要降级 numpy 的版本,然后尝试使用此命令
python
import numpy
numpy.__version__
and got
并得到
>>> numpy.__version__
'1.14.5'
but when i used
但是当我用
pip show numpy
Name: numpy
Version: 1.11.0
Summary: NumPy: array processing for numbers, strings, records, and
objects.
Home-page: http://www.numpy.org
Author: NumPy Developers
Author-email: [email protected]
License: BSD
Location: /usr/local/lib/python3.4/dist-packages
Requires:
Required-by:
now what is the version that python used ?
现在python使用的版本是什么?
Commands
命令
$ python3 -m pip --version
$ pip --version
pip 18.0 from /usr/local/lib/python3.4/dist-packages/pip (python 3.4)
and
和
$ python -m pip --version
pip 18.0 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
回答by James Lim
You are likely confused between python2, python3, and different python virtual environments.
您可能对 python2、python3 和不同的 python虚拟环境感到困惑。
This is the most reliable source, in your case
这是最可靠的来源,就你而言
$ python
>>> import numpy
>>> numpy.__version__
'1.14.5'
To upgrade/downgrade numpy, you need to use pip
that corresponds to the python
that you are using. I think you are using python 2.7. Look around for a pip
executable that corresponds to the installed package at /usr/local/lib/python2.7/dist-packages/pip
.
要升级/降级 numpy,您需要使用pip
与python
您正在使用的numpy相对应的。我认为您正在使用 python 2.7。四处寻找pip
与安装的软件包相对应的可执行文件/usr/local/lib/python2.7/dist-packages/pip
。
This is not the "right" way, but it will work
这不是“正确”的方式,但它会起作用
python -m pip install numpy==x.y.z
python
will just correspond to python interpreter you are using-m pip
will find the right pip that corresponds to your installation of python 2.7numpy==x.y.z
will force the downgrade
python
将仅对应于您正在使用的 python 解释器-m pip
将找到与您安装的 python 2.7 相对应的正确 pipnumpy==x.y.z
将强制降级
Now, you will probably run into permissions problems that will tempt you to use sudo
. At that point, you can either try adding the --user
flag ... but if you really have to use sudo, then consider creating a virtualenv. (Please.)
现在,您可能会遇到诱使您使用sudo
. 那时,您可以尝试添加--user
标志...但如果您真的必须使用 sudo,那么请考虑创建一个 virtualenv。(请。)
Probably The Right Thing to Do
可能是正确的做法
Others have commented on this: maybe your indices are actually not integers.
其他人对此发表了评论:也许您的索引实际上不是整数。
(Related: Slice indices must be integers or None or have __index__ method)
(相关:切片索引必须是整数或 None 或具有 __index__ 方法)
Find the places in your code that is indexing into a list, and make sure that are actually integers.
查找代码中索引到列表中的位置,并确保它们实际上是整数。
assert isinstance(a, int), 'a must be an int'
assert isinstance(b, int), 'b must be an int'
x = y[a:b]
Keep adding those type assertions until you find the bug.
继续添加这些类型断言,直到找到错误为止。
回答by johnny 5
You can downgrade using the --upgrade flag it works both ways e.g
您可以使用 --upgrade 标志降级它可以双向工作,例如
pip install --upgrade numpy==1.10.1
回答by abarnert
I doubt that you really doneed, or want, to downgrade NumPy.
我怀疑你真的做需要或者希望,降级NumPy的。
But that's not what your question is really about. You want to know why pip
is showing one thing and python
is showing another, and what you can do about that.
但这不是你的问题的真正含义。你想知道为什么pip
展示一件事而python
展示另一件事,以及你能做些什么。
The reason you're seeing different things is that your pip
doesn't go with your python
.
您看到不同事物的原因是pip
您的python
.
When you run python
, that's your Python 2.7, and packages you import
there come from your 2.7 library, at /usr/local/lib/python2.7/
.
当你运行时python
,这是你的 Python 2.7,你的包import
来自你的 2.7 库,位于/usr/local/lib/python2.7/
.
When you run pip
it's using your Python 3.4, and installing and looking for things in your Python 3.4's library, which is at /usr/local/lib/python3.4/
.
当您运行pip
它时,它将使用您的 Python 3.4,并在您的 Python 3.4 库中安装和查找内容,该库位于/usr/local/lib/python3.4/
.
So, pip show numpy
is showing you the version of NumPy your Python 3.4 has, which is completely independent of the version of NumPy your Python 2.7 has.
因此,pip show numpy
向您展示了您的 Python 3.4 拥有的 NumPy 版本,它完全独立于您的 Python 2.7 拥有的 NumPy 版本。
If you didn't intend to use Python 2.7, the solution is to run Python 3.4 instead, usually just by using python3
instead of python
.
如果您不打算使用 Python 2.7,则解决方案是运行 Python 3.4,通常只需使用python3
代替python
.
If you didintend to use Python 2.7, the solution is to use the pip
that goes with it. You may have a command named pip2
or pip2.7
for this, but the safest way is to use python -m pip
instead of pip
.
如果您确实打算使用 Python 2.7,那么解决方案是使用pip
它附带的 。您可能有一个名为pip2
or的命令pip2.7
,但最安全的方法是使用python -m pip
代替pip
。
As a side note, given where your 3.4 NumPy is installed, it looks like you may have done something like apt-get python3-numpy
or yum python-numpy
or similar to install it, not pip install numpy
. And probably something like apt-get python2-numpy
to get the 2.7 version as well. If so, you may want to downgrade or upgrade it the same way you installed it in the first place, using your distro's package manager, instead of using pip
. If not… then ignore this paragraph.
作为一个侧面说明,鉴于已安装3.4 NumPy的地方,它看起来像你可能已经做过类似apt-get python3-numpy
或yum python-numpy
或安装的话,不相似pip install numpy
。并且可能类似于apt-get python2-numpy
获得 2.7 版本。如果是这样,您可能希望按照最初安装它的方式降级或升级它,使用发行版的包管理器,而不是使用pip
. 如果不是……那么忽略这一段。
If this all seems way too complicated, but you really do need to have both Python 2.7 and Python 3.4 around, there are two things you should consider:
如果这一切看起来太复杂了,但您确实需要同时安装 Python 2.7 和 Python 3.4,那么您应该考虑两件事:
- Always use virtual environments. Whenever possible, don't install anything globally; pick an environment to install it in. Whatever environment is active,
python
andpip
will both be for that environment. - Install the latest version of Anaconda, with the latest version of Python (3.7 as of today), then ask it to install 3.4 and 2.7 conda environments. Use those environments, and never even touch your system 3.4 and 2.7.
- 始终使用虚拟环境。尽可能不要全局安装任何东西;选择一个环境来安装它。无论环境是活动的,
python
并且pip
都适用于该环境。 - 安装最新版本的 Anaconda,使用最新版本的 Python(截至今天为 3.7),然后要求它安装 3.4 和 2.7 conda 环境。使用这些环境,甚至永远不要接触您的系统 3.4 和 2.7。