具有多个版本的python 3(3.6和3.7)的jupyter选择了错误的网站程序包

时间:2018-07-27 18:34:55

标签: python ipython jupyter-notebook pipenv pyenv

我已经三天在办公桌前敲打头了,我不确定该怎么办,所以,如果您对发生的事情有任何了解,请告诉我。

问题::当我运行全局安装(在虚拟环境外部)的jupyter笔记本时,其注册内核(虚拟环境中安装的ipykernel)在其中,虚拟环境的python3 -V是3.6.4并且全局python3 -V为3.7.0,当我激活venv内核时,jupyter笔记本(从活动的venv运行)崩溃,因为它试图从全局python 3.7.0中获取站点软件包。 / p>

[I 11:11:13.221 NotebookApp] Serving notebooks from local directory: /Users/cap/Desktop/Projects/lastresort
[I 11:11:13.221 NotebookApp] 0 active kernels
[I 11:11:13.221 NotebookApp] The Jupyter Notebook is running at:
[I 11:11:13.221 NotebookApp] http://localhost:8888/?token=cc92b513df1e586ce592bfc4fe641b9f2d76fdde480c6f07
[I 11:11:13.221 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 11:11:13.222 NotebookApp]

    Copy/paste this URL into your browser when you connect for the first time,
    to login with a token:
        http://localhost:8888/?token=cc92b513df1e586ce592bfc4fe641b9f2d76fdde480c6f07&token=cc92b513df1e586ce592bfc4fe641b9f2d76fdde480c6f07
[I 11:11:13.438 NotebookApp] Accepting one-time-token-authenticated connection from ::1
[I 11:11:16.385 NotebookApp] Kernel started: 2ad09f31-36f6-48cf-b859-7f78fdb6adb8
[I 11:11:17.014 NotebookApp] Adapting to protocol v5.1 for kernel 2ad09f31-36f6-48cf-b859-7f78fdb6adb8
[I 11:11:19.398 NotebookApp] Starting buffering for 2ad09f31-36f6-48cf-b859-7f78fdb6adb8:cda1e6c165f3482482d80df7d2664e9b
[I 11:11:19.609 NotebookApp] Kernel shutdown: 2ad09f31-36f6-48cf-b859-7f78fdb6adb8
[I 11:11:19.630 NotebookApp] Kernel started: 9b90f202-97bf-43b1-ba50-64d6a1323405
Traceback (most recent call last):
  File "/Users/cap/.pyenv/versions/3.6.4/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/Users/cap/.pyenv/versions/3.6.4/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/lib/python3.7/site-packages/ipykernel_launcher.py", line 15, in <module>
    from ipykernel import kernelapp as app
  File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/lib/python3.7/site-packages/ipykernel/__init__.py", line 2, in <module>
    from .connect import *
  File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/lib/python3.7/site-packages/ipykernel/connect.py", line 18, in <module>
    import jupyter_client
  File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/lib/python3.7/site-packages/jupyter_client/__init__.py", line 4, in <module>
    from .connect import *
  File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/lib/python3.7/site-packages/jupyter_client/connect.py", line 23, in <module>
    import zmq
  File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/vendor/lib/python3.7/site-packages/zmq/__init__.py", line 47, in <module>
    from zmq import backend
  File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/vendor/lib/python3.7/site-packages/zmq/backend/__init__.py", line 40, in <module>
    reraise(*exc_info)
  File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/vendor/lib/python3.7/site-packages/zmq/utils/sixcerpt.py", line 34, in reraise
    raise value
  File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/vendor/lib/python3.7/site-packages/zmq/backend/__init__.py", line 27, in <module>
    _ns = select_backend(first)
  File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/vendor/lib/python3.7/site-packages/zmq/backend/select.py", line 26, in select_backend
    mod = __import__(name, fromlist=public_api)
  File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/vendor/lib/python3.7/site-packages/zmq/backend/cython/__init__.py", line 6, in <module>
    from . import (constants, error, message, context,
ImportError: cannot import name 'constants'

从上面几行中可以看到,它调用了错误的站点程序包:

Traceback (most recent call last):
  File "/Users/cap/.pyenv/versions/3.6.4/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/Users/cap/.pyenv/versions/3.6.4/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/lib/python3.7/site-packages/ipykernel_launcher.py", line 15, in <module>
    from ipykernel import kernelapp as app
  File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/lib/python3.7/site-packages/ipykernel/__init__.py", line 2, in <module>

这是我的设置:

  • pyenv用于管理多个版本的python
  • 用于virtualenvs的pipenv(对此还不是很满意)

我如何设置:

  1. 安装全局python(我不喜欢将pyenv用作我的默认python,我保持最新版本)

    > brew install python3 # This install 3.7.0

  2. 安装pyenv

    > brew install pyenv

  3. 安装python 3.6.4(我需要这个用于tensorflow)

    > pyenv install 3.6.4

  4. 安装pipenv(我在这里不使用pip安装程序)

    > brew install pipenv

  5. 创建一个项目(我有一个WORKON_HOME设置,所以我的.venvs 在我的〜/ .venvs中创建)

      > cd Projects
      > mkdire test_project
      > cd test_project
      > pipenv --python 3.6.4 #create the virtual env
    
  6. 启动虚拟环境

      > pipenv shell 
      > python3 -V
      > Python 3.6.4 # I have validated in another shell that global is still 3.7
    
      > pyenv which python3
      > /Users/cap/.pyenv/versions/3.6.4/bin/python3
    
  7. 安装ipykernel并进行设置

      > pipenv install ipykernel
      > ipython kernel install --user --name=lastresort-yyl8tfk8 --display-name "Python (lastresort-yyl8tfk8)"
    
  8. 检查jupyter是否看到新内核(注意:我仍然在venv中)

      > jupyter kernelspec list
      > lastresort-yyl8tfk8    /Users/cap/Library/Jupyter/kernels/lastresort-yyl8tfk8
    
  9. 运行jupyter笔记本并选择内核
    jotebook运行正常,但是当我进入并选择Python(lastresort-yyl8tfk8)时,内核重新启动以加载lastresort-yyl8tfk8内核,并且从上面得到了错误,该错误与3.7中的站点程序包混淆了。

我尝试了一堆带有路径的东西。我什至还对ipython配置文件进行了更改,例如:iptyhon profile virtual env

至少在如何解决此问题的方向上,我真的需要一些帮助。

我的最终目标很简单:
在具有jupyter安装是全局的jupyter注册它们之后,能够具有对应版本的ipykernels的多个版本的python,我可以从它们的虚拟环境中运行它们。这样,我可以将代码和笔记本通过venvs保留在其项目文件夹中。实际上,这很简单,我只是不知道自己在搞什么。

谢谢大家!

更新: 我刚刚验证了一下,如果我删除了全球jupyter笔记本并将其安装在venv中,它将正常工作。我没想到不是,但是仍然如此。

更新: 似乎有些PYTHONPATH恶作剧,但我不知道如何解决。

更新: 尝试按照注释中的建议使用python -m运行,但是产生了相同的结果。出于某些原因,当我使用已激活的虚拟环境(在ipython中正确设置了python3.6)调用jupyter笔记本(安装在虚拟环境外部)时,不会调用正确的3.6网站程序包。

我已经看到很多人使用该设置。我真的不知道从这里去哪里。

2 个答案:

答案 0 :(得分:0)

TLDR

对我来说,它可以通过以下命令在jupyter创建的虚拟环境中安装pipenv

pipenv run pip install jupyter

太短了,想读!

我在使用python 3版本时遇到了同样的问题,因此我开始使用pipenv,如所示,它具有一个简单的Pipfile配置,如下所示:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
numpy = "*"
pandas = "*"
matplotlib = "*"

[requires]
python_version = "3.7"

我认为,当我在该配置文件中将python版本指定为3.7时,只要我想运行pipenv run jupyter notebook,内核就会在Python 3.7下运行... 但是,当导入时,笔记本中的库文件会显示在库中:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

我收到“未安装模块错误” ModuleNotFoundError: No module named 'matplotlib'

然后在同一笔记本中,我使用以下代码检查了内核在哪个版本中运行:

import sys

print(sys.version)

输出显示我正在Python 3.6下运行,因此在该版本中我没有安装那些模块,因为pipenv将它们安装为3.7版本。

因此,我尝试再次安装jupyter,但是这次是在pipenv创建的虚拟环境下,使用run命令和pip工具:

pipenv run pip install jupyter

对我有用!

答案 1 :(得分:0)

要从任意路径运行pipenv时使用Pipfile,请在~/.bashrc~/.profile中添加以下env变量:< / p>

export PIPENV_PIPFILE="<path_to_the_Pipefile_you_want>"

否则,pipenv将尝试在运行路径中使用Pipefile