简体   繁体   English

如何从其他目录导入本地模块

[英]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:例子:

  • how files are structured文件的结构
.
├── main.py
└── test2/
    ├── file.py
    └── __init__.py
  • what files contain包含哪些文件
# 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")
  • This file is crucial, you cannot import directly from a subfolder this file has to be a proxy.此文件至关重要,您不能直接从子文件夹导入此文件必须是代理。
# __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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM