Python导入提供了即使在初始化之后也找不到的包__

2024-04-26 01:20:12 发布

您现在位置:Python中文网/ 问答频道 /正文

Test
|   
|   __init__.py
|   
\---Mypackage
    |   qqq.py
    |   __init__.py
    |   
    +---effects
    |       abc.py
    |       
    \---Sound
        |   __init__.py
        |   
        \---formats
                xyz.py

当我尝试导入时xyz.py公司“从Mypackage.effects文件包“导入abc”

我明白了

C:\Test>python C:\Test\Mypackage\Sound\formats\xyz.py
Traceback (most recent call last):
  File "C:\Test\Mypackage\Sound\formats\xyz.py", line 1, in <module>
    from Mypackage.effects import abc
ModuleNotFoundError: No module named 'Mypackage'

Tags: 文件pytestmostinit公司moduleabc
1条回答
网友
1楼 · 发布于 2024-04-26 01:20:12

您需要在中添加以下行xyz.py公司你知道吗

import sys
sys.path.append('path_to_your_Test_folder')
from Mypackage.effects import abc

然后像你在问题中展示的那样运行这个脚本。你知道吗

注意:上面的解决方案是使用python3.5和ubuntu16.04测试的

相关问题 更多 >