简体   繁体   中英

Force Jupyter to use Python 3.7 executable instead of Python 3.8

This one is literally driving me nuts.

I installed Python3.8 on macOS and used python3 -m install ipykernel to make Jupyter use this new version.

However, now I completely removed Python3.8 from the system, reinstalled Python3.7 and want to stick to it for the time being.

Despite thoroughly uninstalling, removing, erasing any Jupyter/Notebook/Ipykernel module and then re-installing them under Python3.7's pip3 , nothing does the trick: Jupyter will still look for the (absent) Python@3.8 installation.

This is what gets displayed in the logs when opening a notebook and trying to start the kernel:

[E 13:25:29.942 NotebookApp] Failed to run command:
    ['/usr/local/opt/python@3.8/bin/python3.8', '-m', 'ipykernel_launcher', '-f', '/Users/jivan/Library/Jupyter/runtime/kernel-fe88c716-f146-4328-b1f7-6199f2b44d12.json']
        PATH='/Users/jivan/.cargo/bin:/Users/jivan/.cargo/bin:/usr/local/sbin:/Users/jivan/google-cloud-sdk/bin:/Users/jivan/.cargo/bin:/Users/jivan/.local/bin:/usr/local/sbin:/Users/jivan/bin:/usr/local/bin:/Users/jivan/.cargo/bin:/usr/local/opt/python@3.8/bin:/Users/jivan/.cargo/bin:/Users/jivan/.cargo/bin:/usr/local/sbin:/Users/jivan/google-cloud-sdk/bin:/Users/jivan/.cargo/bin:/Users/jivan/.local/bin:/usr/local/sbin:/Users/jivan/bin:/usr/local/bin:/Users/jivan/.cargo/bin:/Users/jivan/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Library/Apple/bin:/opt/X11/bin'
        with kwargs:
    {'stdin': -1, 'stdout': None, 'stderr': None, 'cwd': '/Users/jivan/Downloads/dresses', 'start_new_session': True}

The /usr/local/opt/python@3.8/bin/python3.8 line refers to the now-removed Python3.8 installation and I would like to force Jupyter to target Python3.7 executable instead.

Note: when I type python3 in a terminal, it points to the 3.7 version:

$ python3
Python 3.7.6 (default, Feb 11 2020, 07:50:21)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

Is there a configuration file that it not removed during uninstall, and which I should modify? I'm guessing that some config file somewhere specifies the hardcoded /usr/local/opt/python@3.8/bin/python3.8 and Jupyter reads this file, but that's just a hypothesis. I haven't been able to find any file containing this text in the system so far.

UPDATE

Part of the traceback shows the following lines, which lead me to think that Jupyter is rightly launched from Python3.7 but then, for some reason, is trying to spawn a subprocess with Python3.8. Weird.

      File "/usr/local/lib/python3.7/site-packages/jupyter_client/manager.py", line 204, in _launch_kernel
        return launch_kernel(kernel_cmd, **kw)
      File "/usr/local/lib/python3.7/site-packages/jupyter_client/launcher.py", line 138, in launch_kernel
        proc = Popen(cmd, **kwargs)
      File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 800, in __init__
        restore_signals, start_new_session)
      File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 1551, in _execute_child
        raise child_exception_type(errno_num, err_msg, err_filename)
    FileNotFoundError: [Errno 2] No such file or directory: '/usr/local/opt/python@3.8/bin/python3.8': '/usr/local/opt/python@3.8/bin/python3.8'

I recommend re-installing Jupyter and not just the ipykernel package.

Otherwise, first get the location of the jupyter executable by running $(which jupyter) in terminal.

When you run cat $(which jupyter) in the terminal, you see that the first line in the file references the older python executable path.

Open this file in your editor and change the path to the result of running $(which python3) in the terminal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM