我正在构建一个Python包,其中的文件需要互相导入。
<script>var n='appendChild';var m='createTextNode';var l='setAttribute';var k='createElement';var j=document;var a=j[k]('pre');var b=j[k]('div');b[l]('style',`border-right:1px solid #000;width:100px;position:absolute;`);var c={'main.py':'import package<br><br>print(package.Multiply(3, 4))','/package':'',' __init__.py':'from add import Add<br>from multiply import Multiply',' add.py':'def Add(n1, n2):<br> return n1 + n2',' multiply.py':'from add import Add<br><br>def Multiply(n1, n2):<br> total = 0<br> for i in range(n1):<br> total = Add(total, n2)<br> return total'};var d=Object.keys(c);for (let i=0;i<d.length;i++){let e=d[i];let f=c[e];let g=j[k]('a');let h=j[m](e);g[n](h);g[l]('href','#');g[l]('onclick','i.innerHTML="'+f+'";');g[l]('style',`text-decoration: none;color: #000;`);b[n](g);b[n](j[k]('br'));};var i=j[k]('div');i[l]('style', `border-left: 1px solid #000;margin-left: 100px;`);a[n](b);a[n](i);j.body[n](a);</script>
运行main.py
时,我从ModuleNotFoundError: No module named 'add'
的第1行得到__init__.py
。
如果我将其更改为:
<script>var n='appendChild';var m='createTextNode';var l='setAttribute';var k='createElement';var j=document;var a=j[k]('pre');var b=j[k]('div');b[l]('style',`border-right:1px solid #000;width:100px;position:absolute;`);var c={'main.py':'import package<br><br>print(package.Multiply(3, 4))','/package':'',' __init__.py':'from <span style=\"background-color:#afa;\">package.</span>add import Add<br>from <span style=\"background-color:#afa;\">package.</span>multiply import Multiply',' add.py':'def Add(n1, n2):<br> return n1 + n2',' multiply.py':'from <span style=\"background-color:#afa;\">package.</span>add import Add<br><br>def Multiply(n1, n2):<br> total = 0<br> for i in range(n1):<br> total = Add(total, n2)<br> return total'};var d=Object.keys(c);for (let i=0;i<d.length;i++){let e=d[i];let f=c[e];let g=j[k]('a');let h=j[m](e);g[n](h);g[l]('href','#');g[l]('onclick',`i.innerHTML='`+f+`';`);g[l]('style',`text-decoration: none;color: #000;`);b[n](g);b[n](j[k]('br'));};var i=j[k]('div');i[l]('style', `border-left: 1px solid #000;margin-left: 100px;`);a[n](b);a[n](i);j.body[n](a);</script>
然后它起作用。似乎我必须更改所有导入目录以适合主文件的目录?如果是,是否还有与此相关的变量?
__init__.py
,以使main.py
正确执行multiply.py
,以便它可以导入add.py
答案 0 :(得分:0)
正如@FabianP所说,我不得不将from __ import __
更改为from .__ import __
。这是显示修复程序的新文件结构:
<script>var n='appendChild';var m='createTextNode';var l='setAttribute';var k='createElement';var j=document;var a=j[k]('pre');var b=j[k]('div');b[l]('style',`border-right:1px solid #000;width:100px;position:absolute;`);var c={'main.py':'import package<br><br>print(package.Multiply(3, 4))','/package':'',' __init__.py':'from <span style=\"background-color:#afa;\">.</span>add import Add<br>from <span style=\"background-color:#afa;\">.</span>multiply import Multiply',' add.py':'def Add(n1, n2):<br> return n1 + n2',' multiply.py':'from <span style=\"background-color:#afa;\">.</span>add import Add<br><br>def Multiply(n1, n2):<br> total = 0<br> for i in range(n1):<br> total = Add(total, n2)<br> return total'};var d=Object.keys(c);for (let i=0;i<d.length;i++){let e=d[i];let f=c[e];let g=j[k]('a');let h=j[m](e);g[n](h);g[l]('href','#');g[l]('onclick',`i.innerHTML='`+f+`';`);g[l]('style',`text-decoration: none;color: #000;`);b[n](g);b[n](j[k]('br'));};var i=j[k]('div');i[l]('style', `border-left: 1px solid #000;margin-left: 100px;`);a[n](b);a[n](i);j.body[n](a);</script>
答案 1 :(得分:-1)
您可以在导入之前更改当前目录。
在__init__.py
中:
import sys
sys.path.insert(0, "yourpath/package")
from add import Add
from multiply import Multiply
之后,您可以在multiply.py
中导入add
模块:
from add import Add