我有一个
Python 3项目的以下文件夹结构,其中vehicle.py是主脚本,文件夹stats被视为包含几个模块的包:
汽车模块定义了以下功能:
def neon():
print('Neon')
print('mpg = 32')
def mustang():
print('Mustang')
print('mpg = 27')
使用Python 3,我可以从vehicle.py中访问每个模块中的函数,如下所示:
import stats.cars as c
c.mustang()
但是,我想直接访问每个模块中定义的函数,但在执行此操作时收到错误:
import stats as st
st.mustang()
# AttributeError: 'module' object has no attribute 'mustang'
我还尝试使用以下代码将__init__.py文件放在stats文件夹中:
from cars import *
from trucks import *
但我仍然收到一个错误:
import stats as st
st.mustang()
# ImportError: No module named 'cars'
我正在尝试使用与NumPy相同的方法,例如:
import numpy as np
np.arange(10)
# prints array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
如何在Python 3中创建像NumPy这样的包来直接访问模块中的函数?