python - VS Code:相对导入 - 没有名为 x 的模块
问题描述
我知道这是关于“没有名为 x 的模块”的另一个问题。但是我已经尝试了从stackoverflow(以及互联网)中找到的所有东西,但它仍然不适合我。
我创建项目的步骤:
- 创建一个名为
my_project
then的项目cd my_project
- 创建一个虚拟环境
python -m venv .env
并使用此文件夹打开一个 VS Code 工作区 - 这是我的文件夹结构
my_project
├── __init__.py
├── folder_1
│ ├── file_1.py
│ └── folder_2
│ └── file_2.py -- this is where the function my_function is written
但是,file_1.py
当我尝试folder_2/file_2.py
如下导入函数时
# file_1.py
from folder_1.folder_2.file_2 import my_function
然后我遇到了以下问题:
ModuleNotFoundError: No module named 'folder_1'
希望有人可以提供帮助。
更新: CSBigSur 是正确的。就我而言,这只是Pylance的问题
解决方案
尝试这个:
import folder2.file2 as f2
# your code
f2.your_func()
或者
from folder2.file2 import your_func
# your code
your_func()
推荐阅读
- c# - SelectNodes 使用特定的类和设置为特定值的属性
- android-studio - (Ctrl + Z = 上一步)下一步呢?
- sql - 在 SQL 中显示 DateDiff 函数的小数点
- javascript - 在属性类型不同的 Javascript 对象中查找错误值?
- java - Java:反序列化和搜索对象时出现Arraylist序列化错误
- javascript - 使用 javascript 只阻止一个事件
- django - Heroku 无法识别模块 WSGI
- c# - 如何在 C# 的 COM PORT 中发送 ASCII?
- shell - 如何在 Powershell 中断开网络驱动器?
- coldfusion - 如何在 ColdFusion 中为 Google Maps 地理编码设置 API 密钥