[英]Cannot import PyOpenCL in Juypter Notebook
I'm running inside an anacoda environment with pyopencl installed:我在安装了 pyopencl 的 anacoda 环境中运行:
$> conda list | grep pyopencl
pyopencl 2018.2.5 py37h9888f84_0 conda-forge
And from that same window I launch:我从同一个窗口启动:
$> anaconda3/bin/jupyter_mac.command
Which is:这是:
cat /Anaconda3/bin/jupyter_mac.command
DIR=$(dirname $0)
$DIR/jupyter-notebook
So, now we're running a notebook.所以,现在我们正在运行一个笔记本。 When I attempt to import pyopencl:当我尝试导入 pyopencl 时:
import pyopencl as cl
I get the following error:我收到以下错误:
ModuleNotFoundError: No module named 'pyopencl'
I am able to run the examples locally in the same shell w/out any errors via:我可以通过以下方式在同一个 shell 中本地运行这些示例而没有任何错误:
$> python test6.py
Choose platform:
[0] <pyopencl.Platform 'Portable Computing Language' at 0x11512cf00>
[1] <pyopencl.Platform 'Apple' at 0x7f984cd1e010>
Choice [0]:1
Choose device(s):
[0] <pyopencl.Device 'Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz' on 'Apple' at 0x7f984cc1f090>
[1] <pyopencl.Device 'Intel(R) UHD Graphics 630' on 'Apple' at 0x7f984cc19370>
[2] <pyopencl.Device 'AMD Radeon Pro 560X Compute Engine' on 'Apple' at 0x7f984cc19390>
Choice, comma-separated [0]:2
Set the environment variable PYOPENCL_CTX='1:2' to avoid being asked again.
PASSED
[-0.13433748]
[-0.13433748]
Any help is appreciated!任何帮助表示赞赏! Thanks.谢谢。
I managed to reproduce the behavior:我设法重现了这种行为:
code0.py :代码0.py :
#!/usr/bin/env python3
import sys
import os
import pprint
print(f"Python Executable: {sys.executable}")
print(f"Version {sys.version} on {sys.platform}\n")
conda_env_var = "CONDA_DEFAULT_ENV"
conda_env = os.environ[conda_env_var]
print(f"{conda_env_var}: {conda_env}\n")
sys_path = pprint.pformat(sys.path)
print(f"sys.path: {sys_path}\n")
path_var = "PATH"
env_path = pprint.pformat([item for item in os.environ[path_var].split(os.pathsep) if item.find(conda_env) > -1])
print(f"os.environ[\"{path_var}\"] (relevant): {env_path}\n")
import pygraphviz
print(pygraphviz)
Output :输出:
(py_064_030701_test0) [cfati@CFATI-5510-0:e:\\Work\\Dev\\StackOverflow\\q055251357]> python code0.py Python Executable: E:\\Install\\x64\\Anaconda\\Anaconda\\2018.12\\envs\\py_064_030701_test0\\python.exe Version 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] on win32 CONDA_DEFAULT_ENV: py_064_030701_test0 sys.path: ['e:\\\\Work\\\\Dev\\\\StackOverflow\\\\q055251357', 'E:\\\\Work\\\\Dev\\\\Utils', 'E:\\\\Install\\\\x64\\\\Anaconda\\\\Anaconda\\\\2018.12\\\\envs\\\\py_064_030701_test0\\\\python37.zip', 'E:\\\\Install\\\\x64\\\\Anaconda\\\\Anaconda\\\\2018.12\\\\envs\\\\py_064_030701_test0\\\\DLLs', 'E:\\\\Install\\\\x64\\\\Anaconda\\\\Anaconda\\\\2018.12\\\\envs\\\\py_064_030701_test0\\\\lib', 'E:\\\\Install\\\\x64\\\\Anaconda\\\\Anaconda\\\\2018.12\\\\envs\\\\py_064_030701_test0', 'E:\\\\Install\\\\x64\\\\Anaconda\\\\Anaconda\\\\2018.12\\\\envs\\\\py_064_030701_test0\\\\lib\\\\site-packages'] os.environ["PATH"] (relevant): ['E:\\\\Install\\\\x64\\\\Anaconda\\\\Anaconda\\\\2018.12\\\\envs\\\\py_064_030701_test0', 'E:\\\\Install\\\\x64\\\\Anaconda\\\\Anaconda\\\\2018.12\\\\envs\\\\py_064_030701_test0\\\\Library\\\\mingw-w64\\\\bin', 'E:\\\\Install\\\\x64\\\\Anaconda\\\\Anaconda\\\\2018.12\\\\envs\\\\py_064_030701_test0\\\\Library\\\\usr\\\\bin', 'E:\\\\Install\\\\x64\\\\Anaconda\\\\Anaconda\\\\2018.12\\\\envs\\\\py_064_030701_test0\\\\Library\\\\bin', 'E:\\\\Install\\\\x64\\\\Anaconda\\\\Anaconda\\\\2018.12\\\\envs\\\\py_064_030701_test0\\\\Scripts', 'E:\\\\Install\\\\x64\\\\Anaconda\\\\Anaconda\\\\2018.12\\\\envs\\\\py_064_030701_test0\\\\bin'] <module 'pygraphviz' from 'E:\\\\Install\\\\x64\\\\Anaconda\\\\Anaconda\\\\2018.12\\\\envs\\\\py_064_030701_test0\\\\lib\\\\site-packages\\\\pygraphviz\\\\__init__.py'> (py_064_030701_test0) [cfati@CFATI-5510-0:e:\\Work\\Dev\\StackOverflow\\q055251357]> where jupyter-notebook E:\\Install\\x64\\Anaconda\\Anaconda\\2018.12\\Scripts\\jupyter-notebook.exe (py_064_030701_test0) [cfati@CFATI-5510-0:e:\\Work\\Dev\\StackOverflow\\q055251357]> jupyter-notebook [I 01:16:10.345 NotebookApp] JupyterLab extension loaded from E:\\Install\\x64\\Anaconda\\Anaconda\\2018.12\\lib\\site-packages\\jupyterlab [I 01:16:10.346 NotebookApp] JupyterLab application directory is E:\\Install\\x64\\Anaconda\\Anaconda\\2018.12\\share\\jupyter\\lab [I 01:16:10.349 NotebookApp] Serving notebooks from local directory: e:\\Work\\Dev\\StackOverflow\\q055251357 [I 01:16:10.350 NotebookApp] The Jupyter Notebook is running at: [I 01:16:10.352 NotebookApp] http://localhost:8888/?token=14412a6d6d0c895d059a86bcd71e10cbface4a479c5843c2 [I 01:16:10.353 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [C 01:16:10.437 NotebookApp] To access the notebook, open this file in a browser: file:///C:/Users/cfati/AppData/Roaming/jupyter/runtime/nbserver-24700-open.html Or copy and paste one of these URLs: http://localhost:8888/?token=14412a6d6d0c895d059a86bcd71e10cbface4a479c5843c2 [I 01:17:18.569 NotebookApp] 302 GET /?token=14412a6d6d0c895d059a86bcd71e10cbface4a479c5843c2 (::1) 0.98ms [I 01:17:25.161 NotebookApp] Creating new notebook in [I 01:17:26.147 NotebookApp] Kernel started: 8b702b2d-97d0-40e3-bbca-42107efd1de5 [I 01:17:27.186 NotebookApp] Adapting to protocol v5.1 for kernel 8b702b2d-97d0-40e3-bbca-42107efd1de5
And the same script ran into the Jupyter Notebook :同样的脚本运行到Jupyter Notebook 中:
As seen, it fails and that is because it's ran by Anaconda 's default Python (which doesn't have the package installed).正如所见,它失败了,这是因为它是由Anaconda的默认Python (它没有安装包)运行的。 Took a look and noticed that the jupyter-notebook executable launches ( Anaconda 's default) Python on jupyter-notebook-script.py (from the same dir).看了一下,注意到jupyter-notebook可执行文件在jupyter-notebook-script.py (来自同一目录)上启动( Anaconda的默认) Python 。
Possible solutions:可能的解决方案:
This was the 1 st that came to my mind: installing PyGraphviz (and all the other required ones).这是第1次是来到我的脑海:安装PyGraphviz(和所有其他所需的)。 Didn't try it, but it should work .没试过,但应该可以。 The reason why I didn't try it, is because I am against polluting the main Python with packages.之所以没有尝试,是因为我反对用包污染主要的Python 。 But, since it already contains an awful amount of site-packages , things are debatable.但是,由于它已经包含了大量的site-packages ,事情是有争议的。
I tried to make jupyter-notebook launching the current environment Python installation instead, using its configuration, or altering %CONDA_PYTHON_EXE% , but no success (note that it's my 1 st time working with Jupyter ).我试图让jupyter-notebook启动当前环境Python安装,使用其配置,或更改%CONDA_PYTHON_EXE% ,但没有成功(请注意,这是我第一次使用Jupyter )。 Anyway after some investigations, I realized that jupyter-notebook executable launches the Python that Jupyter is installed in. This is a common technique, and it's done by hardcoding the Python path into the executables (although strangely, looking at it with a hex editor didn't find it).无论如何,经过一些调查,我意识到jupyter-notebook可执行文件会启动安装了Jupyter的Python 。这是一种常用技术,它是通过将Python路径硬编码到可执行文件中来完成的(虽然奇怪的是,用十六进制编辑器查看它并没有没找到)。
While searching, I ran into [SO]: Changing Python Executable (@Matt's answer) and from there to [ReadTheDocs.IPython]: Installing the IPython kernel , and gave that a shot:在搜索时,我遇到了[SO]:Changing Python Executable (@Matt's answer)并从那里到[ReadTheDocs.IPython]: Installation the IPython kernel ,并试了一下:
py_064_030701_test0) [cfati@CFATI-5510-0:e:\\Work\\Dev\\StackOverflow\\q055251357]> where pip E:\\Install\\x64\\Anaconda\\Anaconda\\2018.12\\envs\\py_064_030701_test0\\Scripts\\pip.exe E:\\Install\\x64\\Anaconda\\Anaconda\\2018.12\\Scripts\\pip.exe (py_064_030701_test0) [cfati@CFATI-5510-0:e:\\Work\\Dev\\StackOverflow\\q055251357]> pip freeze certifi==2019.3.9 pygraphviz==1.5 wincertstore==0.2 (py_064_030701_test0) [cfati@CFATI-5510-0:e:\\Work\\Dev\\StackOverflow\\q055251357]> pip install ipykernel Collecting ipykernel ... # Some pip useless output ... Installing collected packages: tornado, colorama, six, ipython-genutils, decorator, traitlets, backcall, pygments, pickleshare, wcwidth, prompt-toolkit, parso, jedi, ipython, jupyter-core, python-dateutil, pyzmq, jupyter-client, ipykernel Successfully installed backcall-0.1.0 colorama-0.4.1 decorator-4.4.0 ipykernel-5.1.0 ipython-7.4.0 ipython-genutils-0.2.0 jedi-0.13.3 jupyter-client-5.2.4 jupyter-core-4.4.0 parso-0.3.4 pickleshare-0.7.5 prompt-toolkit-2.0.9 pygments-2.3.1 python-dateutil-2.8.0 pyzmq-18.0.1 six-1.12.0 tornado-6.0.2 traitlets-4.3.2 wcwidth-0.1.7 (py_064_030701_test0) [cfati@CFATI-5510-0:e:\\Work\\Dev\\StackOverflow\\q055251357]> where python E:\\Install\\x64\\Anaconda\\Anaconda\\2018.12\\envs\\py_064_030701_test0\\python.exe E:\\Install\\x64\\Anaconda\\Anaconda\\2018.12\\python.exe (py_064_030701_test0) [cfati@CFATI-5510-0:e:\\Work\\Dev\\StackOverflow\\q055251357]> python -m ipykernel install --name %CONDA_DEFAULT_ENV% Installed kernelspec py_064_030701_test0 in C:\\ProgramData\\jupyter\\kernels\\py_064_030701_test0
After launching it, and selecting the newly created kernel (like in the image below) things went fine.启动它并选择新创建的内核(如下图所示)后一切正常。
This is basically what @AndrásNagy also explained in his answer.这基本上是@AndrásNagy 在他的回答中也解释的内容。
Although this was my 1 st choice at the beginning, using current environment Python to write its metadata in a location where main Python (and other Python s not necessarily inside Anaconda ) could read it from, doesn't seem so straightforward to me (although it might be the recommended approach).虽然这是我一开始的第一个选择,但使用当前环境Python将其元数据写入主Python (以及其他Python不一定在Anaconda内部)可以从中读取它的位置,对我来说似乎并不那么简单(尽管这可能是推荐的方法)。
I also thought of this from the beginning, but I didn't get to it right away because of the previous approach.一开始我也想到了这个,但是因为之前的方法我没有马上想到。 I thought that Jupyter has a lot of dependencies (which it's true), but so does IPyKernel .我认为Jupyter有很多依赖项(这是真的),但IPyKernel也是如此。 However, now I think it's the the simplest way.但是,现在我认为这是最简单的方法。
(py_064_030701_test0) [cfati@CFATI-5510-0:e:\\Work\\Dev\\StackOverflow\\q055251357]> pip install jupyter Collecting jupyter ... # Some pip useless output ... Installing collected packages: qtconsole, testpath, defusedxml, entrypoints, webencodings, bleach, mistune, MarkupSafe, jinja2, pandocfilters, attrs, pyrsistent, jsonschema, nbformat, nbconvert, Send2Trash, prometheus-client, pywinpty, terminado, notebook, widgetsnbextension, ipywidgets, jupyter-console, jupyter Successfully installed MarkupSafe-1.1.1 Send2Trash-1.5.0 attrs-19.1.0 bleach-3.1.0 defusedxml-0.5.0 entrypoints-0.3 ipywidgets-7.4.2 jinja2-2.10 jsonschema-3.0.1 jupyter-1.0.0 jupyter-console-6.0.0 mistune-0.8.4 nbconvert-5.4.1 nbformat-4.4.0 notebook-5.7.8 pandocfilters-1.4.2 prometheus-client-0.6.0 pyrsistent-0.14.11 pywinpty-0.5.5 qtconsole-4.4.3 terminado-0.8.2 testpath-0.4.2 webencodings-0.5.1 widgetsnbextension-3.4.2 (py_064_030701_test0) [cfati@CFATI-5510-0:e:\\Work\\Dev\\StackOverflow\\q055251357]> where jupyter-notebook E:\\Install\\x64\\Anaconda\\Anaconda\\2018.12\\envs\\py_064_030701_test0\\Scripts\\jupyter-notebook.exe E:\\Install\\x64\\Anaconda\\Anaconda\\2018.12\\Scripts\\jupyter-notebook.exe
Needless to say that launching jupyter-notebook ( notice that it's a different executable ) did the trick (as installing Jupyter also registers the Python installation as a kernel).不用说,启动jupyter-notebook (注意它是一个不同的可执行文件)就成功了(因为安装Jupyter也会将Python安装注册为内核)。
I think I had the same problem as you!我想我和你有同样的问题! I had a problem that the anaconda environment kernel did not show up in my jupyter notebook when I started that jupyter notebook from my conda environment!我有一个问题,当我从 conda 环境启动 jupyter notebook 时,anaconda 环境内核没有出现在我的 jupyter notebook 中!
Please consider the following:请考虑以下事项:
conda activate YourEnvironmentName
pip install ipykernel
python -m ipykernel install --user --name=YourEnvironmentName
Hopefully after this you will be able to start your Jupyter notebook with希望在此之后,您将能够使用以下命令启动您的 Jupyter Notebook
jupyter notebook --ip=0.0.0.0 --port=8080
And select the conda environment from the list of kernels where you have installed pyopencl并从安装 pyopencl 的内核列表中选择 conda 环境
I had a similar issue, where I created a conda environment, activated the environment, installed a bunch of packages, then added the kernel to ipython, but the installed packages wouldn't load and I was getting import errors.我有一个类似的问题,我创建了一个 conda 环境,激活了环境,安装了一堆包,然后将内核添加到 ipython,但安装的包无法加载,我收到了导入错误。
Solution: The new kernel was pointing to the wrong python executable (for the base environment).解决方案:新内核指向错误的 python 可执行文件(对于基础环境)。 I had to edit the kernel.json file to point to the python executable in the appropriate environment.我必须编辑 kernel.json 文件以指向适当环境中的 python 可执行文件。 This was the first string in the "argv" array.这是“argv”数组中的第一个字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.