自定义包的导入问题,from 绝对路径 import 方法

问题描述

今天写代码时碰到一个有趣的报错:"ModuleNotFoundError: No module named '__main__.xxx'; '__main__' is not a package"
问题发生在包内引用中。我的目录结构如下:
在这里插入图片描述
Wheel.py中定义了一个Wheel类,在Parser.py中我想导入这个类,于是写了这样一句:

line7:	from .Wheel import Wheel

   
   
  • 1

Parser.py中点击Run之后,解释器提示以下错误:
在这里插入图片描述

根源定位

经过一番百度,在Stack Overflow中找到了答案——相对导入只有在父模块已经在当前运行环境中被导入过才有用
在这里插入图片描述
对于这一问题,官方文档intra-package-references也给出了解释:

Note that relative imports are based on the name of the current module. Since the name of the main module is always “main”, modules intended for use as the main module of a Python application must always use absolute imports.

这里揭示了报错的缘由,相对导入基于当前模块的名称,因为主模块总被命名为"__main__"。当我们从主模块启动时,Python就识图用"__main__"替换".",于是那句话实际便成了from __main__.Wheel import Wheel,这当然是找不到的。

解决办法

有两种:
一、使用绝对导入,这种绝逼不会错(也是官方文档给出的办法),即:

	from Core.Wheel import Wheel

   
   
  • 1

二、保留当前写法,但不再以当前模块为主模块启动应用,例如:

	在Core同级目录下定义一个外部py文件,并在其中引入要引用的模块,此时目录变成:
	|---run.py
	|---Core
		|---__init__.py
		|---Wheel.py
		|---Parser.py

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

run.py中我们引入Parser类,因为此时的主模块变成了run,并且父模块已经被引入过,所以不再报错。

	# run.py
	from Core import Parser
	if __name__=='__main__':
		t = Parser.Parser()

   
   
  • 1
  • 2
  • 3
  • 4

关于这一类问题的更多阐述请参考我的另一篇博客中关于包引入的章节 https://blog.csdn.net/nuaa_llf/article/details/91431555

                                </div><div><div></div></div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-60ecaf1f42.css" rel="stylesheet">
                            </div>
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
unplugin-auto-import 是一个用于自动导入模块的 Vite 插件,可以帮助我们快速导入组件、函数、变量等等。以下是在 Vue 3 + TypeScript 项目中使用 unplugin-auto-import 自定义导入路径的步骤: 1. 安装所需依赖: ```bash npm install -D vite-plugin-auto-import ``` 2. 在构建配置中添加插件,并配置自定义导入: ```javascript import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import AutoImport from 'unplugin-auto-import/vite' export default defineConfig({ plugins: [ vue(), AutoImport({ // 配置需要自动导入的模块 imports: [ { // 导入组件 path: 'vue', // 定义导入的名称 imports: ['defineComponent'], }, { // 导入自定义组件 path: '@/components/MyCustomComponent.vue', // 定义导入的名称 imports: ['MyCustomComponent'], }, { // 导入函数 path: 'lodash/debounce', // 定义导入的名称 imports: ['debounce'], }, { // 导入变量 path: '@/config', // 定义导入的名称 imports: ['appConfig'], }, ], }), ], }) ``` 3. 在代码中使用自定义导入的模块: ```vue <template> <MyCustomComponent /> </template> <script lang="ts"> import { defineComponent } from 'vue' import { debounce } from 'lodash' import { appConfig } from '@/config' import { MyCustomComponent } from '@/components/MyCustomComponent.vue' export default defineComponent({ components: { MyCustomComponent, }, data() { return { searchText: '', } }, methods: { search: debounce(() => { console.log('searchText:', this.searchText) }, 500), }, }) </script> ``` 注意:在使用 unplugin-auto-import 插件时,需要在 tsconfig.json 文件中开启 "esModuleInterop": true,以支持模块导入

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值