python导入模块函数_直接从Python 3模块导入函数

我有一个

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这样的包来直接访问模块中的函数?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值