[英]How do I import module?
The directory structure is as follows: 目录结构如下:
folder1
__init__.py
file1.py
folder2
file2.py
If I write file2.py as follows: 如果我将file2.py编写如下:
from folder1 import file1
I get the error No module named 'folder1'
. 我收到错误No module named 'folder1'
。
If I write file2.py as follows: 如果我将file2.py编写如下:
from ..folder1 import file1
I get the error ValueError: attempted relative import beyond top-level package
. 我收到错误ValueError: attempted relative import beyond top-level package
。
How can I import file1 from file2? 如何从file2导入file1?
Try the following: 请尝试以下操作:
import sys
sys.path.append("..")
from .. import file1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.