2024-06-09 02:02:20 发布
网友
我有这样一个目录结构:
dir/ frontend.py dir1/main.py dir2/backend.py
尝试过Stackoverflow上的所有答案。似乎什么都不管用。在
在要从中导入源文件的任何文件夹中,都需要有现有的init.py文件。在
我建议结构如下:
dir/ main.py dir1/frontend.py dir1/__init__.py dir2/backend.py dir2/__init__.py
然后以以下方式导入它们(在主.py)公司名称:
在Python项目中导入文件时,只有一条规则。在
您必须将包relative导入到运行项目的directory中。在
relative
directory
例如,在问题main.py中应该有如下内容:
main.py
from dir.frontend import * from dir.dir2.backend import *
但是你必须在dir/下有一个类似main.py的东西,它导入dir/dir1/main.py,然后运行{}。在
dir/
dir/dir1/main.py
因此,请尝试将main.py始终放在head directory中,这样就不必担心上述情况。在
head directory
只有一条规则:Everything has to be imported relatively to the directory from where the project is run.
Everything has to be imported relatively to the directory from where the project is run.
在要从中导入源文件的任何文件夹中,都需要有现有的init.py文件。在
我建议结构如下:
然后以以下方式导入它们(在主.py)公司名称:
^{pr2}$在Python项目中导入文件时,只有一条规则。在
您必须将包
relative
导入到运行项目的directory
中。在例如,在问题
main.py
中应该有如下内容:但是你必须在}。在
dir/
下有一个类似main.py
的东西,它导入dir/dir1/main.py
,然后运行{因此,请尝试将
main.py
始终放在head directory
中,这样就不必担心上述情况。在只有一条规则:
Everything has to be imported relatively to the directory from where the project is run.
相关问题 更多 >
编程相关推荐