sys.path和sys.executable在jupyter中不正确,但是没有可用的修复程序 [英] sys.path and sys.executable is incorrect in jupyter, but no applied fix is working
问题描述
我已将jupyter配置为可在远程计算机上使用,并在初始anaconda设置时为其设置了密码.然后,在解决此问题之后,我被困在了另一个问题中.在 jupyter 中,sys.path
和sys.executable
是不正确,但是在 python 和 ipython .请查看下面的详细信息. /opt/anaconda3
中的所有用户都安装了Anaconda3,我的程序正在使用环境zud
.
I've configured jupyter to be used from a remote computer and set a password to it while initial anaconda setup. Then after fixing this issue, I am trapped in another one. sys.path
and sys.executable
is incorrect in jupyter, but correct in python and ipython. Please see the details below. Anaconda3 is installed for all users in /opt/anaconda3
and I am using an environment zud
for my programs.
上述问题解决后,我尝试在 jupyter 中使用import igraph
,但它给出了ModuleNotFoundError
如下:
Once the above-mentioned problem is fixed, I tried to import igraph
in jupyter but it gave ModuleNotFoundError
as below:
import igraph
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-73ffca960e68> in <module>
----> 1 import igraph
ModuleNotFoundError: No module named 'igraph'
然后我迅速使用我的环境zud
检查了以下详细信息.
Then quickly I checked the following details using my environment zud
, which were correct.
(zud) zuddler@silp252-dl360:~$ which python
/home/zuddler/.conda/envs/zud/bin/python
(zud) zuddler@silp252-dl360:~$ which ipython
/home/zuddler/.conda/envs/zud/bin/ipython
(zud) zuddler@silp252-dl360:~$ which jupyter
/home/zuddler/.conda/envs/zud/bin/jupyter
(zud) zuddler@silp252-dl360:~$ which conda
/opt/anaconda3/bin/conda
(zud) zuddler@silp252-dl360:~$ conda env list
# conda environments:
#
zud * /home/zuddler/.conda/envs/zud
base /opt/anaconda3
然后我在 python , ipython 和 jupyter笔记本/实验室中检查了sys.path
和sys.executable
:
Then I checked sys.path
and sys.executable
in python, ipython, and jupyter notebook/lab:
在Python中:
>>> import sys
>>> sys.path
['', '/home/zuddler/.conda/envs/zud/lib/python39.zip', '/home/zuddler/.conda/envs/zud/lib/python3.9', '/home/zuddler/.conda/envs/zud/lib/python3.9/lib-dynload', '/home/zuddler/.conda/envs/zud/lib/python3.9/site-packages']
>>> sys.executable
'/home/zuddler/.conda/envs/zud/bin/python'
在ipython中:
**In [1]: import sys
In [2]: sys.path
Out[2]:
['/home/zuddler/.conda/envs/zud/bin',
'/home/zuddler/.conda/envs/zud/lib/python39.zip',
'/home/zuddler/.conda/envs/zud/lib/python3.9',
'/home/zuddler/.conda/envs/zud/lib/python3.9/lib-dynload',
'',
'/home/zuddler/.conda/envs/zud/lib/python3.9/site-packages',
'/home/zuddler/.conda/envs/zud/lib/python3.9/site-packages/IPython/extensions',
'/home/zuddler/.ipython']
In [3]: sys.executable
Out[3]: '/home/zuddler/.conda/envs/zud/bin/python'**
在Jupyter Notebook/实验室中:
[1]: import sys
[2]: sys.path
[2]:
['/home/zuddler/python scripts',
'/opt/anaconda3/lib/python38.zip',
'/opt/anaconda3/lib/python3.8',
'/opt/anaconda3/lib/python3.8/lib-dynload',
'',
'/opt/anaconda3/lib/python3.8/site-packages',
'/opt/anaconda3/lib/python3.8/site-packages/IPython/extensions',
'/home/zuddler/.ipython']
[3]: sys.executable
[3]: '/opt/anaconda3/bin/python'
sys.path
和sys.executable
在 Jupyter Notebook/实验室中不同,但在 Python 和 ipython 中正确.
sys.path
and sys.executable
were different in Jupyter Notebook/Lab but were correct in Python and ipython.
此外,要确认当前的jupyter笔记本是否正在运行,我在jupyter笔记本中运行了!which jupyter
并收到了/home/zuddler/.conda/envs/zud/bin/jupyter
,它确认正确的jupyter笔记本正在运行.
Also, to confirm if the current jupyter is running, I ran !which jupyter
in the jupyter notebook and received /home/zuddler/.conda/envs/zud/bin/jupyter
which confirmed that the correct jupyter notebook was running.
解决方法1:我尝试在此处和
Remedy 1: I tried the fix mentioned here and here and added the following line in the /home/zuddler/.ipython/profile_default/ipython_config.py
file
c.InteractiveShellApp.exec_lines = [
'import sys; sys.path.append("/home/zuddler/.conda/envs/zud/lib/python3.9/site-packages")'
]
然后重新启动jupyter笔记本并尝试再次导入igraph并得到一个稍有不同的 ModuleNotFoundError
then restarted the jupyter notebook and tried to import igraph again and got a slightly different ModuleNotFoundError
import igraph
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-73ffca960e68> in <module>
----> 1 import igraph
~/.conda/envs/zud/lib/python3.9/site-packages/igraph/__init__.py in <module>
32 # pylint: disable-msg=W0401
33 # W0401: wildcard import
---> 34 from igraph._igraph import *
35 from igraph.clustering import *
36 from igraph.cut import *
ModuleNotFoundError: No module named 'igraph._igraph'
我迅速检查了sys.path
和sys.executable
:
[1]: sys.path
[1]:
['/home/zuddler/python scripts',
'/opt/anaconda3/lib/python38.zip',
'/opt/anaconda3/lib/python3.8',
'/opt/anaconda3/lib/python3.8/lib-dynload',
'',
'/opt/anaconda3/lib/python3.8/site-packages',
'/opt/anaconda3/lib/python3.8/site-packages/IPython/extensions',
'/home/zuddler/.ipython',
'/home/zuddler/.conda/envs/zud/lib/python3.9/site-packages']
[2]: sys.executable
[2]: '/opt/anaconda3/bin/python'
这似乎是死路一条,所以我回滚了此修复程序.
It seems like a dead-end, so I rolled back this fix.
解决方法2:我尝试了此信息中给出的修复程序,并替换了正确的可执行文件kernel.json中的路径
Remedy 2: I tried the fix given on this post and replaced the correct executable path in kernel.json
列出的内核是:
(zud) zuddler@silp252-dl360:~$ jupyter kernelspec list
Available kernels:
python3 /opt/anaconda3/share/jupyter/kernels/python3
我在/opt/anaconda3/share/jupyter/kernels/python3/kernel.json
{
"argv": [
"/opt/anaconda3/bin/python", "/home/zuddler/.conda/envs/zud/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3",
"language": "python"
}
此后,我重新启动了jupyter笔记本/实验室,它在环境zud
中正常工作,但是我注意到,对于base
环境,它导入了igraph
,甚至没有安装在base
中.我从中检查了以下值,which jupyter
对于base
after that, I restarted the jupyter notebook/lab and it worked for the environment zud
, but I noticed that for the base
environment it imported igraph
which was not even installed in base
. I checked the following values from them, which jupyter
was correct for base
[1]: !which jupyter
[1]: /opt/anaconda3/bin/jupyter
但是它正在从zud
中拾取sys.path
和sys.executable
,这意味着每个人都将不得不使用zud
环境.
but it was picking up sys.path
and sys.executable
from zud
, and that means everone will be compelled to use zud
environment.
[2]: sys.path
[2]:
['/home/zuddler/python scripts',
'/home/zuddler/.conda/envs/zud/lib/python39.zip',
'/home/zuddler/.conda/envs/zud/lib/python3.9',
'/home/zuddler/.conda/envs/zud/lib/python3.9/lib-dynload',
'',
'/home/zuddler/.conda/envs/zud/lib/python3.9/site-packages',
'/home/zuddler/.conda/envs/zud/lib/python3.9/site-packages/IPython/extensions',
'/home/zuddler/.ipython']
[3]: sys.executable
[3]: '/home/zuddler/.conda/envs/zud/bin/python'
这又是一个死胡同,所以我回滚了此修复程序.
this was again a dead end, so I rolled back this fix.
解决方法3:从此帖子应用以下步骤也无法解决问题并给出了错误的sys.path
和sys.executable
Remedy 3: Applying the following steps from this post also did not solve the issue and gave the wrong sys.path
and sys.executable
- 从系统中删除IPython和Jupyter
-
rm -rf ~/.local/share/ipython
- 重新安装IPython和Jupyter
解决方法4:我尝试按照这篇文章中的建议打开~/.conda/envs/zud/bin/jupyter lab --no-browser
,但是它也给出了错误的sys.path
和sys.executable
.
Remedy 4: I tried opening ~/.conda/envs/zud/bin/jupyter lab --no-browser
as suggested in this post, but it too gave wrong sys.path
and sys.executable
.
其他补救措施::我阅读了此,但是没有找到解决办法.
Other Remedies: I read this, but found no solution.
PS: I noticed that this post is also mentioning a related issue as mine, but got no solution.
我将对此进行更多研究,以期找到解决方案,如果您能提供解决方案,我将不胜感激.
推荐答案
我也有一段时间了.这是我解决问题的方法.
I've had this issue for quite a while as well. Here's how I've solved it.
创建并进入虚拟环境后,您可能会做类似的事情
After you've created and entered the virtual environment, you probably did something like
pip install ipykernel
ipython kernel install --user --name=myenv
,以使其在jupyter实验室/笔记本中可见.
ipython kernel install --user --name=myenv
in order to make it visible in jupyter lab/notebook.
在/home/user/.local/share/jupyter/kernels/myenv/kernel.json
它应该看起来像这样:
{
"argv": [
"/usr/bin/python3",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"language": "python",
"display_name": "myenv"
}
这里的问题是虚拟env python由于某种原因映射到系统python.您想要更改python指针以指向您的虚拟env python,这类似于/home/user/.virtualenvs/myenv/bin/python3
.
The problem here is that the virtual env python is mapped to system python for some reason. You want to change the python pointer to point to your virtual env python, which would be similar to /home/user/.virtualenvs/myenv/bin/python3
.
TL; DR:
cat /home/user/.local/share/jupyter/kernels/myenv/kernel.json
如果其中包含/usr/bin/python3
,请将其更改为/home/user/.virtualenvs/myenv/bin/python3
if it has /usr/bin/python3
in it, change it to /home/user/.virtualenvs/myenv/bin/python3
您可能需要重新启动Jupyter Lab/笔记本才能使更改生效.
You might need to restart your Jupyter Lab/Notebook for changes to take effect.
我不完全理解为什么有时会发生这种情况,有时却不会发生,但是希望此修复程序也会对您有所帮助.
I don't fully understand why it sometimes happens and sometimes doesn't, but hopefully this fix will help you too.
这篇关于sys.path和sys.executable在jupyter中不正确,但是没有可用的修复程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!