为什么在Jupyterlab的虚拟环境中能够访问未安装的软件包?

huangapple go评论69阅读模式
英文:

Why does the virtual environment in Jupyterlab has access to packages that were not installed?

问题

我的问题是,为什么我能在Jupyter Notebook中访问pandas,即使它没有安装在所使用的环境中。

我创建了一个虚拟环境:
python -m venv ml_skt_book
然后激活它:
ml_skt_book\Scripts\activate.bat
然后安装了numpy:
pip install numpy
现在我有三个包:numpy、pip和setuptools
为什么在Jupyterlab的虚拟环境中能够访问未安装的软件包?

然后我将它链接到Jupyter Notebook内核:
python -m ipykernel install --name=ml_skt_book

这个方法有效。然后我在命令行中检查了numpy的工作情况,但pandas没有。这是成功的。
为什么在Jupyterlab的虚拟环境中能够访问未安装的软件包?

然后我打开了Jupyter Lab:
jupyter-lab
用环境"ml_skt_book"启动了一个新的笔记本。这个方法也有效。

然后我检查了包:
为什么在Jupyterlab的虚拟环境中能够访问未安装的软件包?

在右边,你可以看到"ml_skt_book"环境被使用。因此,pandas被导入是没有意义的。

英文:

My question is why I can access pandas in a jupyter notebook even though it was not installed in the used environment.

I created a virtual environment:
python -m venv ml_skt_book
I then activated it:
ml_skt_book\Scripts\activate.bat
I then installed numpy:
pip install numpy
I have three packages now: numpy, pip and setuptools
为什么在Jupyterlab的虚拟环境中能够访问未安装的软件包?

I then link it to the jupyter notebook kernel:
python -m ipykernel install --name=ml_skt_book

That worked. I then checked in the command line if numpy works and pandas not. This was successfull.
为什么在Jupyterlab的虚拟环境中能够访问未安装的软件包?

I then opened jupyterlabs:
jupyter-lab
started a new notebook with the environment "ml_skt_book". That worked.

I then checked the packages:
为什么在Jupyterlab的虚拟环境中能够访问未安装的软件包?

On the right you can see that the "ml_skt_book" environment is used. It therefore doesn't make sense that pandas can be imported.

答案1

得分: 1

我认为venv默认情况下可以访问其“基础”Python的包,除非你通过将--system-site-packages设置为false来禁用它。

详细信息请参阅文档

2023年3月1日编辑

我终于弄清楚了整个过程是如何工作的。

一旦你运行python -m ipykernel install --name=ml_skt_book,它实际上会在你的主目录路径或Jupyter路径下生成一个名为ml_skt_book的kernelspec文件夹(控制台输出会在你运行时告诉你)。如果你忘记了,它默认应该在C:\ProgramData\jupyter\kernels(Windows)或~/.local/share/jupyter/kernels(Linux)中。文件夹里有一个kernel.json,看起来像这样:

{
 "argv": [
  "/usr/bin/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "ml_skt_book",
 "language": "python",
 "metadata": {
  "debugger": true
 }

argv中的python是用于内核的python。

所以你需要检查一下你的kernel.json中是什么,并看看它是否指向你的ml_skt_book venv中的python可执行文件,还是指向基础环境。

根据我的实验,你需要在激活了你的venv ml_skt_book的情况下运行python -m ipykernel install --name=ml_skt_book,以便在argv中生成带有你的venv python的kernelspec。如果你在基础环境下运行该命令,它只会生成名为"ml_skt_book"的kernelspec,但python可执行文件仍然是基础环境中的那个,也就是说,毫无意义。

另外,如果你的venv中没有安装ipykernel,当你在其中运行python -m ipykernel install --name=ml_skt_book时,它会抱怨,因为venv不与基础环境共享包。所以你需要在venv中重新安装它。

英文:

I think the venv has access to the packages of its 'base' python by default unless you disable it by setting --system-site-packages to false.

See the doc for details.

Edit Mar 1st 2023

I finally figured out how this whole thing works.

Once you run python -m ipykernel install --name=ml_skt_book, it actually generate a kernelspec folder named ml_skt_book in your home path or jupyter path (the console output tells when you run that). If you forgot, by default it should be in C:\ProgramData\jupyter\kernels (windows) or ~/.local/share/jupyter/kernels (linux). Inside the folder there is a kernel.json which looks like

{
 "argv": [
  "/usr/bin/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "ml_skt_book",
 "language": "python",
 "metadata": {
  "debugger": true
 }

and that python in argv is the python used for the kernel.

So you'll need to check what's in your kernel.json and see if it points to the python executable in your ml_skt_book venv, or to the base environment.

According to my experiment, you'll need to run python -m ipykernel install --name=ml_skt_book with your venv ml_skt_book activated to generate kernelspec with your venv python in argv. If you run that command under base environment, it generates kernelspec with name "ml_skt_book" only, but the python executable still the one in base env, that is to say, meaningless.

Also, if you didn't have ipykernel installed in your venv, it will complains when you run python -m ipykernel install --name=ml_skt_book under it, as the venv doesn't share packages with the base environment. So you need to install again in venv.

答案2

得分: 0

你是否尝试在你期望的虚拟环境中安装Jupyter(pip install jupyter),然后像往常一样从这个虚拟环境启动Jupyter?<br>
我通常使用PyCharm创建虚拟环境,并在其控制台中执行pip命令。如果我在这些环境中使用Jupyter,那么只有在这个虚拟环境中安装的包才可用。<br><br>
我认为主要问题是你链接到了Jupyter Notebook的内核,这听起来像是系统上基本安装的Python的一个内核。

英文:

Did you tried to install Jupyter (pip install jupyter) in your desired venv, and then start Jupyter as usual from this venv?<br>
I usually use PyCharm to create venvs and its console for the pip-commands. If I use then Juypter inside those environments, there are only the packages according avaible, which are installed in this venv.<br><br>
I think the main issue is because you link to the jupyter notebook kernel, which sounds like one from the basic installation of Python on your system.

huangapple
  • 本文由 发表于 2023年2月27日 18:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定