我正在安装了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'
我可以通过以下方法在同一外壳中本地运行示例,而不会出现任何错误:
$> 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]
感谢您的帮助!谢谢。
答案 0 :(得分:2)
我设法重现了这种行为:
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 :
如图所示,它失败了,这是因为它是由 Anaconda 的默认 Python (未安装软件包)运行的。看了一下,发现 jupyter-notebook 可执行文件( Anaconda 的默认值)在 jupyter-notebook-script上启动了 Python 。 py (来自同一目录)。
可能的解决方案:
这是我想到的第一个 st :安装 PyGraphviz (以及所有其他必需的)。 没有尝试过,但是应该可以使用。我之所以没有尝试它,是因为我反对用软件包污染主要的 Python 。但是,由于它已经包含了大量的 site-packages ,因此值得商。
我尝试使 jupyter-notebook 启动当前环境 Python 安装,而是使用其配置或更改%CONDA_PYTHON_EXE%,但是没有成功(请注意,这是我第一次与 Jupyter 一起工作)。无论如何,经过一些调查,我意识到 jupyter-notebook 可执行文件会启动 Jupyter 所安装的 Python 。这是一种常见的技术,通过将 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
启动后,并选择新创建的内核(如下图所示)一切正常。
这基本上是@AndrásNagy在回答中也解释的内容。
尽管这是我一开始的第一选择,但使用当前环境 Python 将其元数据写入主 Python (和其他 Python 不一定在 Anaconda 中)可以读取它,对我来说似乎并不那么简单(尽管这可能是推荐的方法)。
我也从一开始就想到了这一点,但是由于先前的方法,我没有马上解决。我认为 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 安装为内核)。
答案 1 :(得分:0)
我想我和你有同样的问题! 我有一个问题,当我从conda环境启动jupyter笔记本时,anaconda环境内核没有显示在jupyter笔记本中!
请考虑以下内容:
conda activate YourEnvironmentName
pip install ipykernel
python -m ipykernel install --user --name=YourEnvironmentName
希望此后,您将能够使用以下方式启动Jupyter笔记本电脑
jupyter notebook --ip=0.0.0.0 --port=8080
并从已安装pyopencl的内核列表中选择conda环境
答案 2 :(得分:0)
我遇到了类似的问题,我创建了一个 conda 环境,激活了该环境,安装了一堆软件包,然后将内核添加到 ipython,但安装的软件包无法加载,并且出现导入错误。
>解决办法: 新内核指向错误的 python 可执行文件(对于基本环境)。我必须编辑 kernel.json 文件以指向适当环境中的 python 可执行文件。这是“argv”数组中的第一个字符串。