我知道__init__.py
对于导入和使python将文件夹视为包是必需的。
下面是我的目录结构
init_stuffs/
├── __init__.py
├── __init__.pyc
└── new_pack
├── __init__.py
├── __init__.pyc
├── animal.py
├── animal.pyc
├── bird.py
├── bird.pyc
└── common.py
当我cd
在目录中并尝试运行我的common.py
时,出现以下错误
(robo-fm) ~/Desktop/python_code/init_stuffs/new_pack $ python common.py
Traceback (most recent call last):
File "common.py", line 6, in <module>
from new_pack import xinit
ImportError: No module named new_pack
但是下面的方法工作正常:
(robo-fm) ~/Desktop/python_code/init_stuffs $ python -m new_pack.common
i am a bird class
i am a bird
i am in animal class
i am a lion
i am a bird class
i am a bird
i am in animal class
i am a lion
('x=', 5)
也ImportError: No module named package检查了该线程,但没有帮助
common.py
import sys
# import os
# sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))
# print sys.path
from new_pack import xinit
from bird import bird
from animal import animal
x=bird()
x.sparrow()
y=animal()
y.lion()
print("x=",xinit)
__ init __。py
# print("\n hi i am __init__")
xinit=5
我在python2.7上
问题
如何使用文件common.py
运行-m
而不是使用模块# 2D array
my @a = [1,2],[3,4];
say @a».Slip.flat; # prints (1 2 3 4)
# 3D array
my @b = [[1,2],[3,4]],[[5,6],[7,8]];
say @b».Slip».flat».Slip.flat; # prints (1 2 3 4 5 6 7 8)
# but needs to know how many dimensions
# there are to flatten
将其作为模块的一部分运行?
TIA