简体   繁体   中英

IPython startup script has permanently changed Jupyter Notebooks behaviour

I followed a guide about setting up a pyenv workflow, it includes an IPython Startup Script that modifies the PYTHONPATH when using Jupyter. Now I don't want to use pyenv, but even after uninstalling everything in this guide. This undesirable script behavior remains.

I followed this guide https://medium.com/@henriquebastos/the-definitive-guide-to-setup-my-python-workspace-628d68552e14 .

It has a separate virtual environment for Jupyter that the other virtual environments use. It includes an IPython Startup Script that runs and loads the virtualenv's site-packages into the Jupyter Notebook environment.

I no longer want to use pyenv. I am using MacOSX. I used Homebrew to install pyenv, pyenv-virtualenv, pyenv-virtualenvwrapper and so I removed all of those using 'brew uninstall'. I removed any trace of pyenv, jupyter, ipython, and python from /usr/local/bin, /usr/local/lib, and /usr/local/share, and ~/.

I then installed python using homebrew again, and ran pip3 install jupyter . I ran jupyter notebook, and it is still trying to run the kernel from this non-existent location '/Users/dhemming/.pyenv/versions/jupyter3/bin/python'.

I really don't think I understand that script or the Python Path and I'm really hoping someone can explain what is going on.

When following the guide I did this:

create jupyter3 virtual env (along with other virtual environments):

pyenv virtualenv 3.6.0 jupyter3

Install jupyter

pyenv activate jupyter3
pip install jupyter
python -m ipykernel install --user
pyenv deactivate

Set pyenv global:

pyenv global 3.6.0 2.7.13 jupyter3 ipython2 tools3 tools2

Check which jupyter:

~$ pyenv which jupyter
/Users/dhemming/.pyenv/versions/jupyter3/bin/jupyter

Install the IPython Script:

ipython profile create
curl -L http://hbn.link/hb-ipython-startup-script > ~/.ipython/profile_default/startup/00-venv-sitepackages.py

Here is the script that that curl command fetches:

import os
import sys
from warnings import warn


virtualenv = os.environ.get('VIRTUAL_ENV')

if virtualenv:

    version = os.listdir(os.path.join(virtualenv, 'lib'))[0]
    site_packages = os.path.join(virtualenv, 'lib', version, 'site-packages')
    lib_dynload = os.path.join(virtualenv, 'lib', version, 'lib-dynload')

    if not (os.path.exists(site_packages) and os.path.exists(lib_dynload)):
        msg = 'Virtualenv site-packages discovery went wrong for %r' % repr([site_packages, lib_dynload])
        warn(msg)

    sys.path.insert(0, site_packages)
    sys.path.insert(1, lib_dynload)

Upon completing the guide everything worked fine and whenever I created a new virtual environment using pyenv it used the jupyter3 virtual environment to run the notebook loaded with the new virtual environments libraries etc.

Then I no longer wanted this setup. So I remove every trace of python and anything related to python and I install a fresh python using Homebrew and do a pip3 install jupyter. After that I run jupyter notebook and I still get this:

Failed to run the command:
    ['/Users/dhemming/.pyenv/versions/jupyter3/bin/python', '-m', 'ipykernel_launcher', '-f', '/Users/dhemming/Library/Jupyter/runtime/kernel-1d721ce4-1619-498d-9f0b-62b98b12d0ac.json']
        PATH='/usr/local/bin:/Users/dhemming/.rbenv/shims:/usr/local/opt/openssl/bin:/Users/dhemming/.nvm/versions/node/v8.11.2/bin:/Users/dhemming/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin'
        with kwargs:
    {'stdin': -1, 'stdout': None, 'stderr': None, 'cwd': '/Users/dhemming/workspace/tmp/tmp_01', 'start_new_session': True}

Where is that '/Users/dhemming/.pyenv/versions/jupyter3/bin/python' coming from?

I ended up deleting ~/Library/Jupyter and that fixed the problem. However I'm sure that there is a less extreme method of fixing this problem if someone knows.

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