[英]How to import a local module from other directory
I made a python module which contains "template" functions i want to use in other classes.我制作了一个 python 模块,其中包含我想在其他类中使用的“模板”函数。 My Problem is that VS Code says "unable to import module" ---> ModuleNotFoundError
我的问题是 VS Code 说“无法导入模块”---> ModuleNotFoundError
This is how i import:这就是我导入的方式:
from utils.util_embeds import Embeds
The File is in a subdirectory where the current file is located.该文件位于当前文件所在的子目录中。
Example:例子:
.
├── main.py
└── test2/
├── file.py
└── __init__.py
# main.py
from test2 import file
file.foo(10)
file.foo(None)
import test2
test2.file.foo(False)
test2.file.foo("hello world!")
from test2.file import foo
foo("Hello there")
# __init__.py
from . import file
# from . import * this would import everything from the directory
# where the file is placed instead of specifying
# every file
# file.py
def foo(x):
print("x is:", x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.