简体   繁体   English

即使使用__init__.py文件,Python导入错误

[英]Python Import error even with __init__.py file

I have 3 directories under S1 我在S1下有3个目录

S1
   - D1
       --python1.py
   - D2
       --python2.py
   - D3
       --python3.py

In python3.py module, I have a statement(below) to import python2.py 在python3.py模块中,我有一条声明(如下)以导入python2.py

import D2.python2

when I run python3.py I get a error " No module named D2.python2" 当我运行python3.py时,出现错误“没有名为D2.python2的模块”

How can I fix this issue? 如何解决此问题?

Note: I have __init__.py in all the directories to signify packages. 注意:我在所有目录中都有__init__.py来表示软件包。

You need to make sure your S1 directory is on your python module search path so that the interpreter knows how to load it. 您需要确保S1目录位于python 模块搜索路径上,以便解释器知道如何加载它。

The easiest way is to add it to your PYTHONPATH environment variable. 最简单的方法是将其添加到您的PYTHONPATH环境变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM