ipython3 does not work in the terminal with python

2020-04-02 07:09发布

问题:

I recently upgraded from Python3.6 to Python3.7. Since I have upgraded, when I type in ipython3 in the terminal I get an error:

~$ ipython3
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/IPython/__init__.py", line 48, in <module>
    from .core.application import Application
  File "/usr/lib/python3/dist-packages/IPython/core/application.py", line 25, in <module>
    from IPython.core import release, crashhandler
  File "/usr/lib/python3/dist-packages/IPython/core/crashhandler.py", line 28, in <module>
    from IPython.core import ultratb
  File "/usr/lib/python3/dist-packages/IPython/core/ultratb.py", line 124, in <module>
    from IPython.utils import path as util_path
  File "/usr/lib/python3/dist-packages/IPython/utils/path.py", line 18, in <module>
    from IPython.utils.process import system
  File "/usr/lib/python3/dist-packages/IPython/utils/process.py", line 19, in <module>
    from ._process_posix import system, getoutput, arg_split, check_pid
  File "/usr/lib/python3/dist-packages/IPython/utils/_process_posix.py", line 24, in <module>
    import pexpect
  File "/usr/lib/python3/dist-packages/pexpect/__init__.py", line 75, in <module>
    from .pty_spawn import spawn, spawnu
  File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 14, in <module>
    from .spawnbase import SpawnBase
  File "/usr/lib/python3/dist-packages/pexpect/spawnbase.py", line 224
    def expect(self, pattern, timeout=-1, searchwindowsize=-1, async=False):
                                                                   ^
SyntaxError: invalid syntax

Furthermore I have noticed that my jupyter-notebook does not seem to work with the python3 kernel now as well (I get the kernel dead error).

NOTEs:

  • ipython and jupyter-notebook are working with fine when I use them with Python2.
  • I am using Ubuntu 18.04 (Bionic Beaver) although I don't think this is relevant

回答1:

UPDATE Please update your installed packages. This error for pexpect has been reported and closed already issue


In Python 3.7, async and await are now reserved keywords. This is what is breaking some of your installed packages.

If you do not need the new features in 3.7, roll back to 3.6 and wait until your packages are updated to support the new syntax in 3.7

What's New in Python 3.7



回答2:

For me, it was conflicting with apt-get installed python3, so I had to first uninstall it/them:

sudo apt-get remove python-pexpect python3-pexpect

And then

sudo pip3.7 install --upgrade pexpect



回答3:

I just solve this problem by upgrading pexpect manually.

Download pexpect4.6 source code from https://github.com/pexpect/pexpect/releases/tag/4.6

Extract the source code, get into the folder and install the pexpect by:

sudo python3.7 setup.py install



回答4:

This appears to be an issue related to async being keyword in Python 3.7

As given here, updating pipenv might be the solution



回答5:

async is a reserved word in Python 3.7

You can edit the packages yourself if you feel comfortable doing it. Here is a shell command that does just that:

sed "s/async/_async/g" "/usr/lib/python3/dist-packages/pexpect/spawnbase.py" > tmp.txt && cat tmp.txt > "/usr/lib/python3/dist-packages/pexpect/spawnbase.py" && rm tmp.txt

Otherwise, you might want to use Python 3.6 while waiting for an update.



回答6:

As others have noted, having the current version of the pexpect package should solve this. In my case, the python3-pexpect package that I had installed through apt was stuck on an old version and I first had to remove it. Then installing the current pexpect package through pip3 fixed the problem.