python - 解决自己模块中的相对导入
问题描述
helloPython
__init__.py
myutil
__init__.py
mymaths.py
service
__init__.py
cal.py
mymaths.py
def myadd(a, b):
return a+b
cal.py
from ..myutil import mymaths #or any other similar import statement
sum = mymaths.myadd(3, 4)
在这里,在 cal.py 中,我想使用上面 mymaths.py 中定义的方法但是当我尝试导入时,当我在 VSCode 中“在终端中运行 Python 文件”时出现以下错误尝试了多种方法
第一种方法
从 ..myutil 导入 mymaths
ImportError:尝试在没有已知父包的情况下进行相对导入
第二种方法
从 helloPython.myutil 导入 mymaths
ModuleNotFoundError:没有名为“helloPython”的模块
解决方案
推荐阅读
- flutter - 如何在 dart 中使用具有 CBC 模式和 PCS5Padding 状态的 AES 加密数据?
- reactjs - 添加时出现“TypeError:children.props is undefined”错误到 NextJS 组件返回语句
- python - python中是否有一个函数可以轻松绘制这样的子图?
- environment-variables - 当环境变量更改时,如何更新我的 zsh 提示?
- javascript - Hashtag 未通过 Angular HttpClient
- javascript - 使用实时数据库更新和增加值
- asp.net-core - Nginx 上的 ASP.NET Core 应用程序是否需要双 HTTPS?
- gmail - 如何绕过电子邮件黑名单?
- android - “无法打开应用程序‘Android Studio’。255”在 Mac 上
- playframework - 在 Play Framework 中为不同的请求使用不同的端口