jupyter notebook 添加kernel时,报错:ImportError: cannot import name 'create_prompt_application'

0x00

最近在学tensorflow风格转换时,用到了jupyter,不过遇到了一些错误问题,写此文章,以备后患。

我在卸载重新安装ipython后,发现prompt-toolkit-1.0.15变成了prompt-toolkit-2.0.9。

0x01

jupyter notebook 添加kernel时发现没有ipykernel这个模块,安装它。如果想了解、掌握以下一些命令,可参考:https://blog.youkuaiyun.com/yumengzth/article/details/95629460

(image_shift) zth@yixiang:~$ python -m ipykernel install --user --name image_shift --display-name "image_shift"
/home/zth/anaconda3/envs/image_shift/bin/python: No module named ipykernel.__main__; 'ipykernel' is a package and cannot be directly executed
(image_shift) zth@yixiang:~$ jupyter kernelspec list
[ListKernelSpecs] WARNING | Native kernel (python3) is not available
[ListKernelSpecs] WARNING | Native kernel (python3) is not available
No kernels available
(image_shift) zth@yixiang:~$ conda install -n image_shift ipykernel
Solving environment: done


==> WARNING: A newer version of conda exists. <==
  current version: 4.5.11
  latest version: 4.7.5

Please update conda by running

    $ conda update -n base -c defaults conda



## Package Plan ##

  environment location: /home/zth/anaconda3/envs/image_shift

  added / updated specs: 
    - ipykernel


The following NEW packages will be INSTALLED:

    ipykernel: 4.10.0-py35_0
    ipython:   6.5.0-py35_0 

Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done

0x02

再次执行添加kernel语句时,又报新错。

(image_shift) zth@yixiang:~$ python -m ipykernel install --user --name image_shift --display-name "image_shift"
Traceback (most recent call last):
  File "/home/zth/anaconda3/envs/image_shift/lib/python3.5/runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/home/zth/anaconda3/envs/image_shift/lib/python3.5/runpy.py", line 142, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/home/zth/anaconda3/envs/image_shift/lib/python3.5/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/home/zth/anaconda3/envs/image_shift/lib/python3.5/site-packages/ipykernel/__init__.py", line 2, in <module>
    from .connect import *
  File "/home/zth/anaconda3/envs/image_shift/lib/python3.5/site-packages/ipykernel/connect.py", line 13, in <module>
    from IPython.core.profiledir import ProfileDir
  File "/home/zth/anaconda3/envs/image_shift/lib/python3.5/site-packages/IPython/__init__.py", line 55, in <module>
    from .terminal.embed import embed
  File "/home/zth/anaconda3/envs/image_shift/lib/python3.5/site-packages/IPython/terminal/embed.py", line 16, in <module>
    from IPython.terminal.interactiveshell import TerminalInteractiveShell
  File "/home/zth/anaconda3/envs/image_shift/lib/python3.5/site-packages/IPython/terminal/interactiveshell.py", line 22, in <module>
    from prompt_toolkit.shortcuts import create_prompt_application, create_eventloop, create_prompt_layout, create_output
ImportError: cannot import name 'create_prompt_application'

在网上查的原因是Jupyter-console与prompt-toolkit 2.0.9 不兼容,参考:https://github.com/jupyter/jupyter_console/issues/158

0x03

我将prompt-toolkit版本又降级到1.0.15,jupyter再次工作。Jupyter控制台需要一些爱啊。

(image_shift) zth@yixiang:~$ pip install 'prompt-toolkit==1.0.15'

虽然ipython 7.6.1要求prompt-toolkit的版本在2.0.0以上,但是这不影响。

可以看到jupyter notebook 添加kernel 成功。

0x04

运行jupyter notebook ,查看新建中已经有anaconda的虚拟环境image_shift。(下图中因为Python [conda env:image_shift]与image_shift是同一环境只是名字不一样而已,所以我在我的电脑上把image_shift删除了,嘻嘻嘻)

可以看到下图中右上角Python [conda env:image_shift]后有一个小圆圈,这表示可以使用虚拟环境,若是小黑点表示与虚拟环境没有连接好,不能使用该虚拟环境。

 

<think>嗯,用户遇到了导入ResNet18_Weights报错的问题,显示ImportError: cannot import name &#39;ResNet18_Weights&#39;。我需要帮他们解决这个问题。根据他们提供的引用信息,这可能与版本兼容性或导入方式有关。 首先,看看引用[1]中的问题,ResNet50_Weights的导入错误,解决方法是升级torchvision版本。同样,ResNet18_Weights可能也需要较新的版本。所以第一步应该建议用户检查他们的torch和torchvision版本是否足够新。比如,ResNet的权重类可能是在某个特定版本之后引入的,比如0.13版本,像引用[3]提到的VGG16_Weights那样。 然后,引用[2]提到导入路径的问题,可能需要更改导入语句。但ResNet的情况可能不同,因为ResNet18_Weights应该位于torchvision.models中,而不是子模块。不过,用户可能需要确认导入语句是否正确,比如是否使用了正确的模块路径。 另外,引用[3]展示了如何正确导入权重,使用类似vgg16的方法。所以用户可能需要将旧的pretrained参数改为新的weights参数,并使用ResNet18_Weights.DEFAULT或其他指定版本。 可能的解决方案步骤包括:1. 升级torch和torchvision到兼容版本;2. 修改导入语句,正确引用ResNet18_Weights;3. 修改模型加载方式,使用weights参数代替pretrained;4. 如果版本确实无法升级,可能需要直接加载预训练模型而不使用权重枚举。 还需要考虑用户是否可能混淆了ResNet18和ResNet的其他版本,或者是否有拼写错误。此外,检查官方文档确认ResNet18_Weights的存在和用法也很重要。如果用户的环境无法升级,可能需要替代方法,比如直接加载URL或本地文件。 总结一下,解决方案应分步骤,先检查版本,再调整导入和模型加载代码,最后提供替代方案和验证方法。同,生成相关问题帮助用户深入理解类似问题。</think>### 解决 `ResNet18_Weights` 导入错误的步骤 #### 1. **检查版本兼容性** `ResNet18_Weights` 是 PyTorch 和 Torchvision 更新版本中引入的权重枚举类。旧版本中可能不存在此名称,需确保以下最低版本: - **PyTorch ≥ 2.0** - **Torchvision ≥ 0.13** **升级命令**: ```bash pip install --upgrade torch torchvision ``` 若版本已升级仍报错,需检查代码语法是否符合新版本规范。 --- #### 2. **修改导入语句** 旧版本中可能直接使用 `pretrained=True` 加载预训练权重,而新版本要求通过 `weights` 参数指定。需调整代码: **旧语法(已废弃)**: ```python from torchvision.models import resnet18 model = resnet18(pretrained=True) # 触发 ImportError ``` **新语法**: ```python from torchvision.models import resnet18, ResNet18_Weights # 指定权重类型(默认权重或特定版本) model = resnet18(weights=ResNet18_Weights.DEFAULT) # 正确方式 ``` --- #### 3. **验证导入路径** 确保从 `torchvision.models` 直接导入 `ResNet18_Weights`,而非子模块: ```python # 正确 from torchvision.models import ResNet18_Weights # 错误(可能导致 ImportError) from torchvision.models.resnet import ResNet18_Weights # 不适用 ``` --- #### 4. **替代方案(无法升级版本)** 若环境限制无法升级,可手动加载预训练权重: ```python from torchvision.models import resnet18 import torch # 直接加载预训练模型(旧方式,兼容性差) model = resnet18(pretrained=True) # 可能触发警告但仍可用 # 或通过 URL 加载 weights_url = "https://download.pytorch.org/models/resnet18-f37072fd.pth" model = resnet18() model.load_state_dict(torch.hub.load_state_dict_from_url(weights_url)) ``` --- #### 5. **验证代码** 完整示例代码: ```python import torch from torchvision.models import resnet18, ResNet18_Weights # 使用新语法 model = resnet18(weights=ResNet18_Weights.IMAGENET1K_V1) print(model) # 输出模型结构 ``` --- ### 原因分析 - **版本不匹配**:Torchvision < 0.13 未定义 `ResNet18_Weights` 类[^3]。 - **语法过**:新版本弃用 `pretrained=True`,改用 `weights` 参数[^1]。 - **路径错误**:错误地从子模块导入(如 `models.resnet`)[^2]。 --- ### 相关问题 1. **如何查看当前安装的 Torchvision 版本?** ```bash pip show torchvision ``` 2. **其他模型(如 MobileNet)出现类似导入错误如何解决?** 需检查对应模型的权重类命名(如 `MobileNet_V2_Weights`)并升级版本[^2]。 3. **如何指定特定版本的预训练权重?** 使用 `weights=ResNet18_Weights.IMAGENET1K_V1` 或 `DEFAULT`(最新默认权重)[^3]。 [^1]: 权重类需通过 `weights` 参数加载,旧版 `pretrained` 已废弃 : 导入路径错误可能导致 `cannot import name` : 模型权重枚举类需 Torchvision ≥ 0.13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值