Jupyter packages

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

Jupyter packages

问题

我尝试导入一些包,因为我正在使用Jupyter笔记本文件,但似乎大多数包都丢失了,尽管我已经安装了它们。例如,当我执行命令:from bs4 import BeautifulSoupimport requests 时,我收到了错误消息 ModuleNotFoundError: No module named 'bs4',第一个包导入出现了类似的错误,对于导入requests也是如此。我尝试了 pip install requestspip install bs4,但问题仍然存在。我已经在以下位置安装了它们:

" (base) aminnazemzadeh@amins-MacBook-Pro ~ % " 这似乎是我的主目录,我还安装了anaconda3和python3。我不能导入这些模块的问题是什么。

如果有区别的话,我正在使用Visual Studio。

一旦我添加:

!pip install requests
!pip install bs4

我得到:

/Users/aminnazemzadeh/.zshenv:.:1: no such file or directory: /Users/aminnazemzadeh/.cargo/env
Requirement already satisfied: requests in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (2.28.1)
Requirement already satisfied: charset-normalizer<3,>=2 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from requests) (2.0.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from requests) (1.26.11)
Requirement already satisfied: idna<4,>=2.5 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from requests) (3.3)
Requirement already satisfied: certifi>=2017.4.17 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from requests) (2022.9.24)
/Users/aminnazemzadeh/.zshenv:.:1: no such file or directory: /Users/aminnazemzadeh/.cargo/env
Requirement already satisfied: bs4 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (0.0.1)
Requirement already satisfied: beautifulsoup4 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from bs4) (4.11.1)
Requirement already satisfied: soupsieve>1.2 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from beautifulsoup4->bs4) (2.3.1)

然后出现以下警告:

ModuleNotFoundError                       Traceback (most recent call last)
Cell In[7], line 4
      2 get_ipython().system('pip install bs4')
      3 from urllib.request import urlopen
----> 4 from bs4 import BeautifulSoup

ModuleNotFoundError: No module named 'bs4'

谢谢。

英文:

I am trying to import certain packages as I am working with Jupyter notebook files, and most of the packages seem to be missing, even though I have installed them. For example, when I do the command: from bs4 import BeautifulSoup or import requests
I get the error saying ModuleNotFoundError: No module named 'bs4' for the first one and a similar one for importing requests as well. I have tried pip install requests and pip install bs4, but same issue persists. I have installed them on:
"(base) aminnazemzadeh@amins-MacBook-Pro ~ % " which seems to be my home directory, and I also have anaconda3 installed alongside python3. What is the issue that I cannot import these modules.

I am using visual studio if it makes any difference

Once I add :

!pip install requests
!pip install bs4

I get:

/Users/aminnazemzadeh/.zshenv:.:1: no such file or directory: /Users/aminnazemzadeh/.cargo/env
Requirement already satisfied: requests in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (2.28.1)
Requirement already satisfied: charset-normalizer<3,>=2 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from requests) (2.0.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from requests) (1.26.11)
Requirement already satisfied: idna<4,>=2.5 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from requests) (3.3)
Requirement already satisfied: certifi>=2017.4.17 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from requests) (2022.9.24)
/Users/aminnazemzadeh/.zshenv:.:1: no such file or directory: /Users/aminnazemzadeh/.cargo/env
Requirement already satisfied: bs4 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (0.0.1)
Requirement already satisfied: beautifulsoup4 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from bs4) (4.11.1)
Requirement already satisfied: soupsieve>1.2 in /Users/aminnazemzadeh/opt/anaconda3/lib/python3.9/site-packages (from beautifulsoup4->bs4) (2.3.1)

followed by this warning:

ModuleNotFoundError                       Traceback (most recent call last)
Cell In[7], line 4
      2 get_ipython().system('pip install bs4')
      3 from urllib.request import urlopen
----> 4 from bs4 import BeautifulSoup

ModuleNotFoundError: No module named 'bs4'

Thanks

答案1

得分: 1

可能你正在在一个与 VS Code 使用的环境不同的地方安装这些包。你可以尝试在 Jupyter 笔记本中直接安装这些包,通过在一个笔记本单元中运行以下代码。目前在笔记本中运行安装的最佳实践是使用魔术命令 %pip%conda

%pip install requests beautifulsoup4

# 或者

%conda install requests beautifulsoup4

这将在笔记本所在的相同环境中安装这些包。

请注意,你可能需要重新启动内核以使用受影响的包。

英文:

probably you're installing the packages on an environment other than the one vs code is using. you can try installing the packages directly from your jupyter notebook by running the following code in a notebook cell. the current best practice to be running installs in the notebook is using the magic commands %pip or %conda:

%pip install requests beautifulsoup4

# or

%conda install requests beautifulsoup4

this should install the packages in the same environment that the notebook is running on.

note that you may need to restart the kernel to use the affected packages.

sources:

ps: thanks @wayne for the comments regarding the current best practices for installing on the current running environment.

答案2

得分: 0

  1. 如果你在使用conda,尽量在可能的情况下通过conda安装。当你使用pip安装时,conda会失去一些管理依赖版本的能力。

  2. 尝试创建一个新的conda环境,通过conda安装所需的包,然后在VS Code中将内核设置为新环境。为特定项目分配conda环境。可以有一个用于玩耍的默认/通用环境,但对于任何重要的工作,最好不要使用,因为如果依赖项更改为不兼容的版本,容易在其他工作中出现错误。

如果需要,可以参考Conda速查表:https://docs.conda.io/projects/conda/en/4.6.0/_downloads/52a95608c49671267e40c689e0bc00ca/conda-cheatsheet.pdf

如果尚未安装,你需要在VS Code中安装Jupyter扩展。

你还需要在conda环境中安装相应的jupyter包。

英文:
  1. If you're using conda, you should install via conda whenever possible. When you install via pip, conda loses some of its ability to manage dependency versions.

  2. Try creating a new conda environment, install the needed packages via conda, then set the kernel to your new environment in vscode. Dedicate conda environments to specific projects. It is okay to have a default/generic environment for playing around but not for any significant work as you can easily create errors in your other work if a dependency changes to an incompatible version.

Conda cheet sheet for reference if you need it: https://docs.conda.io/projects/conda/en/4.6.0/_downloads/52a95608c49671267e40c689e0bc00ca/conda-cheatsheet.pdf

You will need the Jupyter extension in vscode if you do not already have it installed.

You will also need to install the corresponding jupyter package in your conda environment.

huangapple
  • 本文由 发表于 2023年2月16日 05:40:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465692.html
匿名

发表评论

匿名网友

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

确定