How to downgrade numpy?

泄露秘密 提交于 2021-02-18 11:10:19

问题


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

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: numpy-discussion@scipy.org
License: BSD
Location: /usr/local/lib/python3.4/dist-packages
Requires: 
Required-by: 

now what is the version that python used ?

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)

回答1:


You are likely confused between python2, python3, and different python virtual environments.

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.

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.7
  • numpy==x.y.z will force the downgrade

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.)

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)

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.




回答2:


You can downgrade using the --upgrade flag it works both ways e.g

pip install --upgrade numpy==1.10.1



回答3:


I doubt that you really do need, or want, to downgrade 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.


The reason you're seeing different things is that your pip doesn't go with your 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/.

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/.

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.

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.

If you did intend 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.


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.


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:

  • Always use virtual environments. Whenever possible, don't install anything globally; pick an environment to install it in. Whatever environment is active, python and pip 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.


来源:https://stackoverflow.com/questions/51912284/how-to-downgrade-numpy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!