简体   繁体   English

从包中导入所有模块

[英]Import all modules from a package

I have setup the following structure: Python 201: Creating Modules and Packages . 我已经设置了以下结构: Python 201:创建模块和包 The mymath package is available at that link. 该链接提供了mymath软件包。

When you run the code below: 当您运行以下代码时:

import sys
sys.path.append('G:\MyPython\Package')
import mymath
print (mymath.add(4,5))
print (mymath.division(4, 2))
print (mymath.multiply(10, 5))
print (mymath.fibonacci(8))
print (mymath.squareroot(48))

Python Version: 3.4 Python版本:3.4

outer __init__.py contents: 外部__init__.py内容:

from add import add
from divide import division
from multiply import multiply
from subtract import subtract
from adv.fib import fibonacci
from adv.sqrt import squareroot

My goal is to call division , add , subtract , etc, but if I try to call the module I get: 我的目标是调用divisionaddsubtract等,但是如果我尝试调用模块,则会得到:

Traceback (most recent call last):
File "G:\MyPython\Package\myscript.py", line 1, in <module>
import mymath
File "G:\MyPython\Package\mymath\__init__.py", line 1, in <module>
from add import add
ImportError: No module named 'add'

Python 3.x has changed import resolution. Python 3.x更改了导入分辨率。 You must now specify a full relative import if you want to perform a relative import. 现在,如果要执行相对导入,则必须指定完整的相对导入。

from .add import add

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

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