Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python3:从分散的目录和文件导入文件/模块_Python 3.x - Fatal编程技术网

Python 3.x Python3:从分散的目录和文件导入文件/模块

Python 3.x Python3:从分散的目录和文件导入文件/模块,python-3.x,Python 3.x,我有以下目录结构: /home/pi - project/ - p1v1.py - tools1/ - __init__.py - tools1a/ - __init__.py - file1.py - file2.py - tools1a1/ - __ini

我有以下目录结构:

/home/pi
     - project/
          - p1v1.py
     - tools1/
          - __init__.py
          - tools1a/
               - __init__.py
               - file1.py
               - file2.py
               - tools1a1/
                    - __init__.py
                    - file3.py
                    - file4.py
               - tools1a2/
                    - __init__.py
                    - file5.py
                    - file6.py
我正在尝试将所有模块从
file1.py
导入我的项目文件
p1v1.py

从文件1导入*

但最终的结果是

ImportError:尝试在没有已知父包的情况下进行相对导入

ValueError:尝试在非包中进行相对导入

取决于我在
p1v1.py
中使用的内容,因为
file1.py
中的函数依赖于
file3.py
file4.py
。我希望使用显式导入(为了清楚起见),但我不确定如何做到这一点。如有任何建议,将不胜感激


谢谢大家!

通过反复试验,最终找到了解决方法:

import sys
sys.path.insert(0,'..')
from tools1.tools1a.file1 import function as f1

注意:为了使其工作,我需要在工作目录
/home/pi/project/
之外编辑和执行脚本
p1v1.py
。希望这能帮助其他有类似问题的人

谢谢,但是我看了很多遍那个教程,不明白它是如何工作的/该做什么。