16

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)
James Lim
  • 12,915
  • 4
  • 40
  • 65
  • 5
    Where's the code? it's very unlikely that you should *downgrade* numpy – blue_note Aug 18 '18 at 20:29
  • 1
    Most likely you have (at least) two separate copies of Python installed on your system, and `python` runs one, but `pip` is for the other on. If you don't want to learn how to deal with this mess, the simplest thing to do is to always use `python -m pip` in place of `pip`. That way, you know that whatever `python` is, you're using the `pip` that goes with it. – abarnert Aug 18 '18 at 20:43
  • One common way to get into this problem on Unix systems is to use a pre-installed Python (like Apple's 2.7, or Ubuntu's 3.4) that doesn't come with `pip`, and then do a third-party install of a second Python that _does_ come with `pip`. Now `python` may run the pre-installed Python, because that comes earlier on the PATH, but `pip` will install into your second Python, because there is no other `pip` to come before it on the PATH. – abarnert Aug 18 '18 at 20:45
  • @abarnert thanks for answering , yes i have two versions but excuse me the command you wrote will not give the version ! should i try another thing –  Aug 18 '18 at 20:48
  • I don't know what the second half of that sentence means. Can you run `pip --version` and `python --version` and `python3 -m pip --version` and [edit] the result into your question? – abarnert Aug 18 '18 at 20:51
  • @abarnert i edited the post –  Aug 18 '18 at 23:22
  • what is the problem with the post to down vote ?!! –  Aug 19 '18 at 00:07

3 Answers3

20

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.

James Lim
  • 12,915
  • 4
  • 40
  • 65
  • 1
    `python -m pip` actually is the right way. The PyPA docs at one point suggested that you _always_ run `pip` that way if you have more than one Python and aren't using virtual environments. They seem to have backed off from that to instead just push you to always use virtual environments when you have more than one Python, and only mention `-m pip` as a fallback for users who can't or won't do that. But it's still better than trying to figure out which `pip` executable goes with which `python`. – abarnert Aug 19 '18 at 00:09
  • is round () function used to convert numbers into integers ? –  Aug 19 '18 at 00:10
  • @prog try using just `int(a)`, as suggested by Pieters in the linked question. – James Lim Aug 19 '18 at 00:12
  • @prog It depends. Do you want `2.91` to mean `2` or `3`? The `int` function will truncate to `2`. The `round` function will round off to the nearest integer, which is `3`. (In Python 2.7, it may give you the float `3.0`, so you may have to actually do `int(round(a))`. In 3.x, that isn't a problem.) – abarnert Aug 19 '18 at 00:15
  • `pip install numpy==1.14.5` ... `Collecting numpy==1.14.5` ... `Successfully installed numpy-1.16.3` What!? – Apollys supports Monica Jun 19 '19 at 00:30
15

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

pip install --upgrade numpy==1.10.1
johnny 5
  • 19,893
  • 50
  • 121
  • 195
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.
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • I think you are right. Given that it's in `dist-packages`, it was probably installed using `apt` or `yum` instead of pip. Very astute observation. – James Lim Aug 18 '18 at 23:54
  • thanks for your time , i'm using jupyter to run the code ,, with pyhton2 or python3 gave the same error so my question here is that because numpy ? –  Aug 19 '18 at 00:08
  • @prog As I said at the top, and has others have said in comments, your error probably has nothing to do with needing to downgrade numpy. You probably have a bug in your code that you need to fix. If you want help with that, create a new question, and give us a [mcve] that demonstrates the problem so we can debug it for you. – abarnert Aug 19 '18 at 00:14
  • the code isn't mine and worked well with others but the problem with me so i guess the problem with versions because installed two versions of python and i'm confused with them –  Aug 19 '18 at 00:18