Mac 下安装Jupyter (踩过的坑)

Mac 下安装Jupyter (踩过的坑)

  • 安装
  • jupyter启动

安装

本地环境如下:

$ pip -V
pip 18.0 from /Library/Python/2.7/site-packages/pip-18.0-py2.7.egg/pip (python 2.7)
$ python --version
Python 2.7.10

直接安装jupyter

bash-3.2# pip install jupyter

报错如下:

此处省略安装信息
Running setup.py install for simplegeneric ... done
  Running setup.py install for scandir ... done
Could not install packages due to an EnvironmentError: [Errno 1] Operation not permitted: '/System/Library/Frameworks/Python.framework/Versions/2.7/share'
Consider using the `--user` option or check the permissions.

再次尝试

$ sudo pip install jupyter

报错依旧,仔细查看前面错误提示,–user 于是尝试如下:

$ pip install jupyter --user
结果成功
Successfully installed ipywidgets-7.4.2 jupyter-1.0.0 jupyter-console-5.2.0

jupyter启动

jupyter安装完了,紧接着我们进行启动

jupyter notebook
The Jupyter HTML Notebook.

这将启动一个基于tornado的HTML笔记本服务器,它提供一个html5/
javascript笔记本客户端。

Traceback (most recent call last):
  File "/usr/local/bin/jupyter-notebook", line 11, in <module>
    sys.exit(main())
  File "/Library/Python/2.7/site-packages/jupyter_core/application.py", line 266, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/Library/Python/2.7/site-packages/traitlets/config/application.py", line 657, in launch_instance
    app.initialize(argv)
  File "", line 2, in initialize
  File "/Library/Python/2.7/site-packages/traitlets/config/application.py", line 89, in catch_config_error
    app.print_help()
  File "/Library/Python/2.7/site-packages/traitlets/config/application.py", line 385, in print_help
    self.print_subcommands()
  File "/Library/Python/2.7/site-packages/traitlets/config/application.py", line 377, in print_subcommands
    print(os.linesep.join(lines))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 4: ordinal not in range(128)

看起来像是编码问题,于是上万能的百度求助一下,博主搜索了各大网站发现基本上都是说这是Python2.7的一个bug,凑巧我本来还就是想用Python2.7做实验的,下面看看怎么解决一下:
解决方案:
方案1:

先卸载当前安装的 jupyter notebook
		$ sudo pip install pip-autoremove
		$ sudo pip autoremove jupyter -y

用pip3(没有就安装一个)安装
		$ sudo pip3 install pip-autoremove
		$ jupyter notebook

方案2:
在出现错误的脚本加上如下语句

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

博主本次安装报错的地方是application.py文件,于是就在application.py文件的头部加上上面的语句

sudo vim /Library/Python/2.7/site-packages/jupyter_core/application.py	
再次启动
$ jupyter-notebook
The Jupyter HTML Notebook.

这将启动一个基于tornado的HTML笔记本服务器,它提供一个html5/
javascript笔记本客户端。

Subcommands
-----------

Subcommands are launched as `jupyter-notebook cmd [args]`. For information on
using subcommand 'cmd', do: `jupyter-notebook cmd -h`.

stop
    Stop currently running notebook server for a given port
password
    Set a password for the notebook server.
list
    列出当前运行的Notebook服务.

Options
-------

Arguments that take values are actually convenience aliases to full
Configurables, whose aliases are listed on the help line. For more information
on full configurables, see '--help-all'.

--script
    DEPRECATED, IGNORED
--pylab
    DISABLED: use %pylab or %matplotlib in the notebook to enable matplotlib.
--debug
    set log level to logging.DEBUG (maximize logging output)
--no-browser
    在启动服务以后不在浏览器中打开一个窗口.
--allow-root
    允许notebook在root用户下运行.
-y
    Answer yes to any questions instead of prompting.
--no-mathjax
    Disable MathJax
    
    MathJax is the javascript library Jupyter uses to render math/LaTeX. It is
    very large, so you may want to disable it if you have a slow internet
    connection, or for offline use of the notebook.
    
    When disabled, equations etc. will appear as their untransformed TeX source.
--no-script
    DEPRECATED, IGNORED
--generate-config
    generate default config file
--certfile=<Unicode> (NotebookApp.certfile)
    Default: u''
    SSL/TLS 认证文件所在全路径.
--ip=<Unicode> (NotebookApp.ip)
    Default: 'localhost'
    notebook服务会监听的IP地址.
--pylab=<Unicode> (NotebookApp.pylab)
    Default: 'disabled'
    DISABLED: use %pylab or %matplotlib in the notebook to enable matplotlib.
--log-level=<Enum> (Application.log_level)
    Default: 30
    Choices: (0, 10, 20, 30, 40, 50, 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL')
    Set the log level by value or name.
--port-retries=<Integer> (NotebookApp.port_retries)
    Default: 50
    如果指定的端口不可用,则要尝试其他端口的数量.
--notebook-dir=<Unicode> (NotebookApp.notebook_dir)
    Default: u''
    用于笔记本和内核的目录。
--keyfile=<Unicode> (NotebookApp.keyfile)
    Default: u''
    SSL/TLS 私钥文件所在全路径.
--client-ca=<Unicode> (NotebookApp.client_ca)
    Default: u''
    用于ssl/tls客户端身份验证的证书颁发证书的完整路径.
--config=<Unicode> (JupyterApp.config_file)
    Default: u''
    Full path of a config file.
--port=<Integer> (NotebookApp.port)
    Default: 8888
    notebook服务会监听的IP端口.
--transport=<CaselessStrEnum> (KernelManager.transport)
    Default: 'tcp'
    Choices: [u'tcp', u'ipc']
--browser=<Unicode> (NotebookApp.browser)
    Default: u''
    Specify what command to use to invoke a web browser when opening the
    notebook. If not specified, the default browser will be determined by the
    `webbrowser` standard library module, which allows setting of the BROWSER
    environment variable to override it.

To see all available configurables, use `--help-all`

Examples
--------

    jupyter notebook                       # start the notebook
    jupyter notebook --certfile=mycert.pem # use SSL/TLS certificate
    jupyter notebook password              # enter a password to protect the server

[C 20:28:38.582 NotebookApp] Bad config encountered during initialization:
[C 20:28:38.583 NotebookApp] Could not decode '\xe6\x9c\xaa\xe5\x91\xbd\xe5\x90\x8d' for unicode trait 'untitled_notebook' of a LargeFileManager instance.

又一次报错,只不过报错的地方不一样了,貌似这次还是编码的问题。
这次继续求助万能的google,发现貌似是系统中文编码引起的错误
于是按照大神的指导尝试按照如下方式启动:

$ LANG=zn jupyter-notebook
[133761:~ tanglion$ LANG=zn jupyter-notebook
[I 11:05:27.001 NotebookApp] Writing notebook server cookie secret to /Users/tanglion/Library/Jupyter/runtime/notebook_cookie_secret
[I 11:05:27.429 NotebookApp] Serving notebooks from local directory: /Users/tanglion
[I 11:05:27.429 NotebookApp] The Jupyter Notebook is running at:
[I 11:05:27.429 NotebookApp] http://localhost:8888/?token=b08814b0fcd3455d46cb9b47059e157faccbac3c81125c34
[I 11:05:27.429 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 11:05:27.434 NotebookApp] 
    
    To access the notebook, open this file in a browser:
        file:///Users/tanglion/Library/Jupyter/runtime/nbserver-74422-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=b08814b0fcd3455d46cb9b47059e157faccbac3c81125c34

启动成功~!

参考连接:
[1]: https://www.jianshu.com/p/8d9f8ea8fd0c
[2]: https://github.com/jupyterlab/jupyterlab/issues/5345
[3]: https://blog.csdn.net/mr_sdhm/article/details/79116981

你可能感兴趣的:(python)