我一直在我的 table 上敲我的头3天,我不知道该怎么办这个问题,所以如果你知道发生了什么,请告诉我 .
Issue: 当我运行全局安装(在虚拟环境之外)jupyter笔记本时,注册内核(ipykernel安装在虚拟环境中),其中虚拟环境的python3 -V为3.6.4,全局python3 -V为3.7 .0,当我激活venv内核时,jupyter笔记本(从活动的venv运行)崩溃,因为它试图从全局python 3.7.0中获取站点包
[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
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
from .connect import *
File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/lib/python3.7/site-packages/ipykernel/connect.py", line 18, in
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
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
import zmq
File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/vendor/lib/python3.7/site-packages/zmq/__init__.py", line 47, in
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
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
_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
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
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
这是我的设置:
pyenv用于管理python的多个版本
pipenv for virtualenvs(对这个还不是很满意)
我如何设置:
安装全局python(我不喜欢用我的默认python的pyenv搞乱,我保持最新版本)
> brew install python3 # This install 3.7.0
安装pyenv
> brew install pyenv
安装python 3.6.4(我需要这个用于tensorflow)
> pyenv install 3.6.4
安装pipenv(我这里没有使用pip安装程序)
> brew install pipenv
创建一个项目(我有一个WORKON_HOME设置,所以我的.venvs在我的〜/ .venvs中创建)
> cd Projects
> mkdire test_project
> cd test_project
> pipenv --python 3.6.4 #create the virtual env
启动虚拟环境
> 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
安装ipykernel并进行设置
> pipenv install ipykernel
> ipython kernel install --user --name=lastresort-yyl8tfk8 --display-name "Python (lastresort-yyl8tfk8)"
检查jupyter是否看到新内核(注意:我还在静脉中)
> jupyter kernelspec list
> lastresort-yyl8tfk8 /Users/cap/Library/Jupyter/kernels/lastresort-yyl8tfk8
运行jupyter笔记本并选择内核
jotebook运行正常,但当我进入并选择Python(lastresort-yyl8tfk8)时,内核重新启动以加载lastresort-yyl8tfk8内核,我从上面得到错误,它与3.7中的site-packages混淆 .
我尝试了许多带路径的东西 . 我甚至还使用了ipython配置文件更改,例如:iptyhon profile virtual env
我真的需要一些帮助,至少有一个如何解决这个问题的方向......
我的最终目标很简单:
能够有多个版本的python和相应的ipykernels,我可以在jupyter安装全局的jupyter注册后从虚拟环境中运行它们 . 通过这种方式,我可以将我的代码和笔记本保存在他们的项目文件夹中,包含venvs . 实际上这很简单,我只是不知道我搞砸了什么 .
Thank you all in advance!
UPDATE: 我刚刚确认,如果我删除全局jupyter笔记本并将其安装在venv中,它可以正常工作 . 我没想到它,但仍然 .
UPDATE: 这似乎是一些PYTHONPATH恶作剧,但我不知道如何解决它 .
UPDATE: 试图按照注释中的建议使用python -m运行,但它产生了相同的结果 . 出于某种原因,当我使用激活的虚拟环境(在ipython中正确设置python3.6)调用jupyter笔记本(安装在虚拟环境之外)时,它不会调用正确的3.6站点包 .
我见过很多人使用该设置 . 我真的不知道从哪里开始 .