sys.path.insert无法导入其他python文件

sys.path.insert无法导入其他python文件,python,python-3.x,import,python-import,importerror,Python,Python 3.x,Import,Python Import,Importerror,标题上写着什么。我在路径C:\Users\User\Desktop\all python file\5.0.8中有以下文件结构: 5.0.8\ tree one\ little main.py sample_tree1.py tree two\ sample_tree2.py 下面是小main.py中的内容: import sys import sample_tree1 sys.path.insert(0, r'C:\Users\User\D

标题上写着什么。我在路径
C:\Users\User\Desktop\all python file\5.0.8
中有以下文件结构:

5.0.8\
   tree one\
      little main.py
      sample_tree1.py
   tree two\
       sample_tree2.py
下面是小main.py中的内容:

import sys
import sample_tree1

sys.path.insert(0, r'C:\Users\User\Desktop\all python file\5.0.8\tree two\sample_tree2.py')

import sample_tree2
Traceback (most recent call last):

  File "<ipython-input-1-486a3fafa7f2>", line 1, in <module>
    runfile('C:/Users/User/Desktop/all python file/5.0.8/tree one/little main.py', wdir='C:/Users/User/Desktop/all python file/5.0.8/tree one')

  File "C:\Users\User\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "C:\Users\User\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/User/Desktop/all python file/5.0.8/tree one/little main.py", line 12, in <module>
    import sample_tree2

ModuleNotFoundError: No module named 'sample_tree2'
我想导入
sample\u tree2.py
,但在运行
little main.py
时,就会出现错误:

import sys
import sample_tree1

sys.path.insert(0, r'C:\Users\User\Desktop\all python file\5.0.8\tree two\sample_tree2.py')

import sample_tree2
Traceback (most recent call last):

  File "<ipython-input-1-486a3fafa7f2>", line 1, in <module>
    runfile('C:/Users/User/Desktop/all python file/5.0.8/tree one/little main.py', wdir='C:/Users/User/Desktop/all python file/5.0.8/tree one')

  File "C:\Users\User\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "C:\Users\User\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/User/Desktop/all python file/5.0.8/tree one/little main.py", line 12, in <module>
    import sample_tree2

ModuleNotFoundError: No module named 'sample_tree2'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
runfile('C:/Users/User/Desktop/all python file/5.0.8/tree one/little main.py',wdir='C:/Users/User/Desktop/all python file/5.0.8/tree one')
文件“C:\Users\User\Anaconda3\lib\site packages\spyder\u kernels\customize\spyderrcustomize.py”,第786行,在runfile中
execfile(文件名、命名空间)
文件“C:\Users\User\Anaconda3\lib\site packages\spyder\u kernels\customize\spyderrcustomize.py”,第110行,在execfile中
exec(编译(f.read(),文件名,'exec'),命名空间)
文件“C:/Users/User/Desktop/all python File/5.0.8/tree one/little main.py”,第12行,在
导入示例树2
ModuleNotFoundError:没有名为“sample\u tree2”的模块
发生了什么事?为了从路径的另一个分支导入一个文件,我遵循了这个命令,但它不起作用

先谢谢你


编辑:

我正在寻找一种解决方案:

1) 不需要更改当前工作目录

2) 不需要更改文件的名称

3) 不需要更改python终端的配置


编辑2: 添加了一些文件和文件夹结构的屏幕,以及错误消息

使用
sys.path.insert()
命令将路径插入系统路径,并且不应包含文件名

请使用您的结构尝试以下操作:

little main.py:

导入系统 导入样本树1 sys.path.insert(0,r'/my/absolute/path/5.0.8/treetwo') 打印(系统路径)#查看路径并验证插入 导入示例树2 打印(示例_tree2.tree2_func()) treetwo中的示例_tree2.py

def tree2_func():
返回'called tree2'
产出:

['/my/absolute/path/5.0.8/treetwo',“…其他路径”]

叫做tree2


请尝试使用不带空格的目录。@ipaleka这需要更改目录的名称,这是不允许的。但是,即使将
树2
更改为
treetwo
,并使用
导入treetwo.sample\u tree2
,仍会出现错误
没有名为“treetwo”的模块。抱歉,发现该情况为时已晚。尝试创建os.path.join对象,而不是字符串,因为这一直是您的问题:看看堆栈跟踪中显示路径的方式与上面的路径不同。@ipaleka您可以将它们向下键入答案,以便我可以运行代码而不会误解您的答复吗?@ipaleka I used
os.path.exists(r'C:\Users\User\Desktop\all python file\5.0.8\treetwo\sample\u tree2.py')==True
要检查绝对路径字符串中是否有键入错误,结果证明我是正确的;绝对路径是正确的,因此问题不在于路径字符串是否正常工作。在此之前,我尝试了导入treetwo
导入treetwo.sample_tree2
from treetwo导入sample_tree2
,并且ehow错过了最明显的一个。我真是太傻了。非常感谢你的跟进