gpt4 book ai didi

python - 无法在 Jupyter Notebook 中导入 PyOpenCL

转载 作者:太空狗 更新时间:2023-10-29 21:55:57 26 4
gpt4 key购买 nike

我在安装了 pyopencl 的 anacoda 环境中运行:

$> conda list | grep pyopencl
pyopencl 2018.2.5 py37h9888f84_0 conda-forge

然后我从同一个窗口启动:

$> anaconda3/bin/jupyter_mac.command

这是:

cat /Anaconda3/bin/jupyter_mac.command

DIR=$(dirname $0)

$DIR/jupyter-notebook

所以,现在我们正在运行笔记本。当我尝试导入 pyopencl 时:

import pyopencl as cl

我收到以下错误:

ModuleNotFoundError: No module named 'pyopencl'

我可以通过以下方式在同一个 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]

感谢任何帮助!谢谢。

最佳答案

背景

我设法重现了这个行为:

  • Win 上 - 但这应该不是问题,因为(我假设)OSX
  • 上的情况是一样的
  • 来自(当前)Anaconda 环境的 PythonAnaconda 的默认环境(对应于 base 环境)是截然不同的。如果不是这种情况,答案(或部分答案)可能不正确。请注意,只有 2 个 Python 是我要引用的,它们都是 Anaconda 的内部(即使 >Anaconda“关键字”不会出现)
  • 我在测试场景中使用了 PyGraphviz(而不是 PyOpenCL)

code0.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)

输出:

(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

同样的脚本运行到 Jupyter Notebook 中:

Img0

正如所见,它失败了,这是因为它由 Anaconda 的默认 Python(未安装包)运行。看了一下,注意到 jupyter-notebook 可执行文件在 jupyter-notebook-script 上启动(Anaconda 的默认设置)Python。 py(来自同一目录)。

可能的解决方案:

1。在主 Python

中安装缺少的包

这是我想到的第一个st:安装PyGraphviz(以及所有其他必需的)。 没试过,但应该可以。我之所以没有尝试,是因为我反对用包污染主要的 Python。但是,由于它已经包含了大量的站点包,因此值得商榷。

2。将当前环境 Python 注册为内核

我试图让 jupyter-notebook 启动当前环境 Python 安装,使用它的配置,或改变 %CONDA_PYTHON_EXE%,但是没有成功(请注意,这是我第一次st 使用 Jupyter)。无论如何,经过一些调查,我意识到 jupyter-notebook 可执行文件会启动安装了 JupyterPython。这是一种常见的技术,它是通过将 Python 路径硬编码到可执行文件中来完成(尽管很奇怪,用 hex 编辑器查看它并没有找到它)。

在搜索时,我遇到了 [SO]: Changing Python Executable (@Matt's answer)从那里到[ReadTheDocs.IPython]: Installing 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

启动后,并选择新创建的内核(如下图所示)一切正常。

Img1

@AndrásNagy 在他的回答中也基本上解释了这一点。

尽管这是我一开始的第一个st选择,但使用当前环境 Python 将其元数据写入主要 Python (和其他不一定在 Anaconda 中的 Python)可以从中读取它,对我来说似乎不是那么简单(尽管这可能是推荐的方法)。

3。在当前环境Python

安装 Jupyter

我一开始也想到了这个,但是因为之前的做法,没能马上上手。我认为 Jupyter 有很多依赖项(这是真的),但 IPyKernel 也是如此。但是,现在我认为这是最简单的方法。

(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

不用说,启动 jupyter-notebook(注意它是一个不同的可执行文件)就成功了(因为安装 Jupyter 也会注册Python 作为内核安装)。

关于python - 无法在 Jupyter Notebook 中导入 PyOpenCL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55251357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com