Ipython Reads Wrong Python Version

ipython reads wrong python version

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.

How to make iPython use Python 2 instead of Python 3

Following ipython reads wrong python version, in /usr/local/bin/ipython, I simply changed

#!/usr/bin/python3

in the first line to

#!/usr/bin/python

and Python 2 has become the default version used by iPython:

kurt@kurt-ThinkPad:~$ ipython
Python 2.7.12 (default, Jul 1 2016, 15:12:24)
Type "copyright", "credits" or "license" for more information.

IPython 2.4.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

How do I change the kernel/python version for iPython?

I just found the answer. In essence, this stems from not understanding the python installation layout and how resources are separated between installed interpreters. It appears each python version will have its own repository of tools, and the current "pip" command I had installed on the system was mapped for use with python 2.7, so all libraries, tools, and other details it managed where available only to python 2.7. This included iPython.

I thought that installing python3 would make all these libraries available to the new interpreter by default, but it appears the system keeps them all separate (which makes sense). The main issue here was continuing to use "pip" which targeted the old installation, instead of the new "pip3" command included with python3. By using pip3 to reinstall iPython, I was able to get iPython3 installed properly.

Unfortunately this setup means needing to re-download other libraries, but that's not too difficult.

It might seem like a trivial issue in hindsight, but this had me completely stuck. I hope this helps someone else in this situation.

How to set other Python interpreter to IPython

It seems I've found the solution.

You need to edit the file which starts IPython. On Linux you can enter it with: sudo nano $(which ipython).
Once you're inside the file change the shebang line to what ever Python interpreter you like.
And directories that contain Python3.4 modules need to be added to $PYTHONPATH variable.

What is a shebang line?
First line in a file that represents the path to python interpreter that will be used.

Thanks to @code_byter.

Jupyter using the wrong version of python

It could be a problem in your python kernel.json configuration. For example my python kernel is located at:

/usr/local/share/jupyter/kernels/python/kernel.json

and contains:

    {
"language": "python",
"display_name": "Python 2.7",
"argv": [
"/usr/local/bin/python2.7",
"-m",
"ipykernel",
"-f",
"{connection_file}"
]
}

Make sure that the path in argv section points to correct version of python.

ipython using 2.6 version instead of 2.7

iPython 1.0 is the last main version of iPython to support Python < 2.7 interpreter.

So you need to install iPython 1.2.1 at most, if that's not done already.

Now to associate it to Python2.6:

$ sudo python2.6 setup.py install

More information on this link. The author skipped the part about which version of iPython to use because at this time all versions of iPython supported Python < 2.7.

It could work, but it's not certain because it depends on your existing config, etc.

If it doesn't, a different way (a bit hacky however) could be found here.

How do I get the IPython version I'm currently using?

You can import IPython, then use IPython.version_info to get it as a tuple, or IPython.__version__ to get it as a string. For example:

In [1]: import IPython

In [2]: IPython.version_info
Out[2]: (4, 1, 2, '')

In [3]: IPython.__version__
Out[3]: '4.1.2'

IPython.version_info seems to be the same layout as sys.version_info: major, minor, micro, releaselevel.



Related Topics



Leave a reply



Submit