ipython reads wrong python version
Asked Answered
B

13

121

I've been having trouble with Python, iPython and the libraries. The following points show the chain of the problematics. I'm running Python 2.7 on Mac Lion.

  1. iPython doesn't read the libraries of scipy, matplotlib, but it does read numpy.
  2. To fix this, I tried installing Python's source code version, and it only gave me more problems since now I have two different versions: 2.7.1 and 2.7.2
  3. I noticed that running Python, uses version 2.7.2 and does import scipy, matplotlib, and numpy, but on iPython the version is 2.7.1 which doesn't open scipy or matplotlib.

I've tried several things that I've encountered from other blogposts. But none of them have helped, and also unfortunately I don't quite know what I'm doing with some of them. For example: I tried uninstalling and reinstalling ipython with easy_install and pip. I also tried reinstalling everything through homebrew, and modifying the path .bash_profile.

Backbend answered 21/2, 2012 at 22:11 Comment(6)
The ipython script is "tied" to the specific Python version it was installed with – it won't automatically switch to what you installed last. If you first installed 2.7.1, then IPython, then 2.7.2 from source, your IPython will keep using 2.7.1. You need to either undo all your flailing, go back to your original Python version and figure out what the reason was behind point 1; alternately, reinstall IPython using whichever Python install can access the libraries you need.Anisole
Could you be more specific?... thanksBackbend
Specific about which part? How IPython startup works, or how to make the problem go away?Anisole
Okay, so I think my problem got fixed, as now iPython runs with the right version of Python which is 2.7.2 instead of 2.7.1. I think this got fixed when using easy_install but wasn't working well when installing it with pip. Is this correct? Could you tell me a little bit about how IPython startup works, so that I can take something from this? RegardsBackbend
Essentially, when you install any python "program", like IPython or even easy_install, the startup script for the program is set to always use the python executable it was installed with. So, if you had Python 2.7.1 without your libraries, and then install IPython, IPython is installed "into" the Python 2.7.1 installation, and a link to the ipython executable is placed on your PATH. If you then install Python 2.7.2 from source, it's a new, separate copy of Python it won't upgrade the existing 2.7.1 that has IPython in it. So IPython won't see the 2.7.2 copy, or any libraries in it.Anisole
Libraries work the same. If you installed numpy etc. after installing Python 2.7.2 from source, they were likely installed into the 2.7.2 directory. This means that the Python 2.7.1 version wouldn't see those libraries at all. Since IPython was installed with 2.7.1, it couldn't see those libraries either. Basically, you can have as many completely separate copies of Python on your system as you want, but it can be hard keeping track of which one is used for what. Unless you know what you're doing, it's best to start with the built-in version of Python, and just reinstalling missing librariesAnisole
S
158

Okay quick fix:

which python

gives you /usr/bin/python, right? Do

which ipython

and I bet that'll be /usr/local/bin/ipython. Let's look inside:

Edit 9/7/16 -- The file now looks like this:

cat /usr/local/bin/ipython

#!/usr/bin/python

# -*- coding: utf-8 -*-
import re
import sys

from IPython import start_ipython

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(start_ipython())

And mine works properly like this, but my situation isn't exactly like the OP's.


Original answer -- 9/30/13:

cat /usr/local/bin/ipython

#!/usr/bin/python
# EASY-INSTALL-ENTRY-SCRIPT: 'ipython==0.12.1','console_scripts','ipython'
__requires__ = 'ipython==0.12.1'
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.exit(
        load_entry_point('ipython==0.12.1', 'console_scripts', 'ipython')()
    )

Aha - open /usr/local/bin/ipython in your editor (with privileges), and change the first line to

#!/usr/local/bin/python

save, start iPython, should say it's using the version you want now.

Salimeter answered 30/4, 2012 at 10:16 Comment(4)
in doubt, python -m IPython is equivalent to ipython, expect you are sure to use the right Python.Addison
also remember to close the terminal and open a new one. since the PATH is cached by the shell for efficiency. this is how I got this solution working for me. see: conda.pydata.org/docs/…Consols
@Matt. I also experienced. My case is "python -m IPython" will invoke the right version i wanted. But just 'ipython', it started a copy that uses old python2. The mystery is if i do a "which ipython", it is pointing to virtualenv copy with that python file content apparently correct.Cupidity
Im with @kawingkelvin, in example, using pyenv with other python version will not work using ipython, but will work with python -m IPython. Those are different entry points, using distintc variables, seems.Knoxville
W
54

Posting @Matt's comment as an answer just so its more visible

python -m IPython

Loads ipython as a module with whatever python is accessible on the path first. In my case I had one pre-installed and one I added from brew. This just works perfectly.

Wallachia answered 15/3, 2019 at 0:11 Comment(4)
Perhaps not the answer for this question but this is the answer to the question: "how to run ipython with any python version installed". Thanks.Primatology
IMPORTANT: the command above worked for me, but in the beginning it gave me the error /usr/bin/python: No module named IPython. To fix that I had run python3.7 -m pip install IPython, and them the command python -m IPython worked like a charm :) . Just letting you know.Leaves
Thank you! This is exactly what I was looking for. I had tried python -m ipython and was wondering if it was possible to disambiguate between the different python versions I have installed (3.8 vs the system's 3.6) in that way. After having installed IPython for each version, of course.Playback
To make your workflow even cleaner you might find convenience in adding the statement above that works for you in your bash profile or whatever source profile is applicable. I added alias ipython='python -m IPython' for example and can now forget about the details under the hood on a daily basis by simply using ipython in my terminal.Reconstruction
R
10

What about using a virtualenv? I really like it. Maybe it's not the faster way, but I think it's very clear.

When you create a virtualenv, you can specify the python path with the -p flag.

for python 2.7

$ virtualenv -p /usr/bin/python2.7 venv2.7
$ source venv2.7/bin/activate
(venv2.7)$ pip install ipython
(venv2.7)$ ipython

for python 3.4

$ virtualenv -p /usr/bin/python3.4 venv3.4
$ source venv3.4/bin/activate
(venv3.4)$ pip install ipython
(venv3.4)$ ipython
Rieth answered 17/12, 2015 at 10:12 Comment(3)
This is an old thread, but I've also come accross virtualenvs and like them nice for developing products. For the purpose of "everyday" analysis however, I'd rather just have a straight up usable version of python/ipython in my system.Backbend
python -m IPython will start ipython with the given version of pythonChefoo
note that python -m IPython need this installed: pip install ipythonChefoo
E
6

First, I would make sure you're using the right python. At a command prompt type:

which python
python -V

The first will tell you the path, the second tells you the Python version you're using.

Entomb answered 21/2, 2012 at 22:15 Comment(1)
Okay so, it gives me Python 2.7.2 on /usr/local/bin/pythonBackbend
C
6

My solution is simple, stupid but work.

I use python -V to make sure what version is

$ python -V
Python 2.7.10

and then make alias in .bash_profile

$ vi ~/.bash_profile

Add a line

alias ipython="python -m IPython"

then you will get an ipython in python 2.7. 🙂

(By the way, my ipython is install via homebrew, it default will got an ipython run in python 3.)

$ brew install ipython
Chrissychrist answered 12/1, 2018 at 9:33 Comment(0)
C
3

extremely relevant: http://conda.pydata.org/docs/troubleshooting.html#shell-command-location.

td;lr problems are encountered because of shell 'hashing' and path variables.

Consols answered 18/12, 2016 at 18:25 Comment(2)
also a general good practice is to install ipython in the virtualenv. your which python should point to the virtualenv python binary.Consols
The shell hashing bit me, even after installing ipython inside the virtualenv. hash -r fixed it for me; thanks!Ensample
P
1

A similar method using pyenv

pyenv install 3.4.5
pyenv local 3.4.5
pip install ipython
ipython

Now it will show correct version of python

Python 3.4.5
Package answered 23/9, 2016 at 11:40 Comment(0)
G
1

The absolute simplest solution I could think of, which requires no fiddling with environments, installed files, or anything else, relies on the facts that

  1. The executable ipython is actually a Python script.
  2. The IPython package is installed separately for each interpreter that you ran pip intall with.

If the version of Python you are runninig with has an IPython package installed, you can just do

/path/to/desired/python $(which ipython)

This will run the ipython script with the interpreter you want instead of the one listed in the shebang.

Glorious answered 9/6, 2017 at 23:29 Comment(2)
Instead of calling the above every time, how do I set it as default, so that every time I type ipython in my shell, it automatically reads the custom interpreter instead of the default ?Collegium
Add a script that does that to the path before the default. But you have to mess with the environment to do that.Glorious
T
0

Your problem is basically making ipython use the right python.

so the fix to the problem is to make ipython use the right python (which has the libraries like scipy installed)

I have written a solution here:

How to make iPython use Python 2 instead of Python 3

Trevethick answered 15/6, 2017 at 2:21 Comment(0)
T
0

I came across the same issue but the following was the only solution what worked for me on OSX 12, Sierra.

ipython was always launching for python 3.6 but I needed it for 2.7. I could not find an ipython startup script for 2.7, nor could I find the IPython module to execute with python -m. None of brew instally ipython pip install ipython or pip2 install ipython could get me the 2.7 version. So I got it manually.

brew install ipython@5 installs the 2.7 version from here but won't put it on your $PATH because it knows the name conflicts with another package. ln -s /usr/local/Cellar/ipython@5/5.5.0_1/bin/ipython /usr/local/bin/ipython2 will fix this and let you just run ipython2 from your shell prompt

For me, because I was serious about using ipython for 2.7, I also ran the following commands.

ln -s /usr/local/Cellar/ipython/6.2.1/bin/ipython /usr/local/bin/ipython3
rm -f /usr/local/bin/ipython
ln -s /usr/local/bin/ipython2 /usr/local/bin/ipython
Typewriting answered 30/10, 2017 at 22:28 Comment(0)
E
0

All the answers mentioned here do not help in solving the issue if you are using anaconda or some other virtual environment wrapper.

This answer is based on the assumption that you are using anaconda.

Say you are in a python 3 environment and when creating a notebook on jupyter notebook it shows "Python 2" instead of "Python 3".

This is because "ipython" is essentially a script which is run and in this script it mentions which python version it is using to execute the command. All you need to do is change this line for ipython to use the version of python you want.

First stop the ipython server and get the location of the python executable of the current environment using the command "which python"

My output is :

/home/sourabh/anaconda2/envs/py3/bin/python

Now get the executable location of ipython use the command "which ipython"

mine is :

/home/sourabh/anaconda2/envs/py2/bin/python

Notice that it is using a different version of python ie. python from a specific environment running a different version of python ie running python from a different environment.

Now navigate to the directory anaconda2/bin(for anaconda 3 users it should be anaconda3/bin ) and search for "ipython" file. in this edit the first line to be point it to the current python version which you want. ie the output of "which python" i.e. :

#!/home/sourabh/anaconda2/envs/py3/bin/python

Notice that I changed my python environment from py2(running python 2.7) to py3(running python 3.5).

Save the file. And run jupyter notebook, now when creating a new notebook the "Python 3" option should be visible.

Cheers!

Elementary answered 13/3, 2019 at 5:32 Comment(0)
W
0

For me, on windows 10, my ipython.exe did not match my python. I knew this because ipython said

----> 3 from pywinauto import handleprops
      4 from pywinauto import timings
      5 from pywinauto.application import Application

ModuleNotFoundError: No module named 'pywinauto'

However, this worked when I executed the containing program with > python

Here's the mis-match.

ipython info

Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.9.0 -- An enhanced Interactive Python. Type '?' for help.

[DOT DOT DOT]

python info

Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Here's my fix. I changed my reference to Python\...\Scripts, which is where ipython.exe is hiding

  1. I CHANGED my system PATH in settings - environment variables
  • was C:\Users\joeco\AppData\Roaming\Python\Python38\Scripts
  • now C:\Users\joeco\AppData\Roaming\Python\Python39\Scripts\
  1. MOVED the above value UP to the top of the path
  2. REBOOTED

RESULTS

  1. ipython now matches python
  2. ipython from pywinauto import handleprops SUCCEEDS
Wendel answered 20/4, 2021 at 19:46 Comment(0)
P
0

I had python2.7, python3.6 and python3.8 installed

sahil@rubeus:~$ ls -ls /usr/bin/python*
   0 lrwxrwxrwx 1 root root       9 Apr 16  2018 /usr/bin/python -> python2.7
   0 lrwxrwxrwx 1 root root       9 Apr 16  2018 /usr/bin/python2 -> python2.7
3548 -rwxr-xr-x 1 root root 3633000 Feb 27 20:40 /usr/bin/python2.7
   0 lrwxrwxrwx 1 root root       9 Oct 25  2018 /usr/bin/python3 -> python3.6
4424 -rwxr-xr-x 2 root root 4526456 Jan 26 21:03 /usr/bin/python3.6
5016 -rwxr-xr-x 1 root root 5134032 Jun  3 23:23 /usr/bin/python3.8

I had ipython file located at this location

sahil@rubeus:~$ which ipython
/home/sahil/.local/bin/ipython

It was having below content

#!/usr/bin/python2

# -*- coding: utf-8 -*-
import re
import sys

from IPython import start_ipython

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(start_ipython())

So I just made two copies of that file at the same location with name ipython3.6 and ipython3.8 and updated the first line to point to specific python version, /usr/bin/python3.6 and /usr/bin/python3.8 in my case.

So there are now 3 files each pointing to specific python version

sahil@rubeus:~$ ls ~/.local/bin/ipython*
/home/sahil/.local/bin/ipython  
/home/sahil/.local/bin/ipython3.6  
/home/sahil/.local/bin/ipython3.8

Now when I need to start python2 I just run ipython from terminal and same way to get ipython in python3.8, I run ipython3.8

sahil@rubeus:~$ ipython3.8
Python 3.8.11 (default, Jul  3 2021, 17:53:42) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.25.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: 

Note: you may need to install ipython for specific python version using command python3.6 -m pip install IPython or python3.8 -m pip install IPython. I am not sure whether it was installed with python or I installed separately.

Pardo answered 15/7, 2021 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.