首页 > 编程语言 >python import

python import

时间:2024-02-05 10:12:29浏览次数:24  
标签:europe __ python py africa world import

python import

Reference

world/
│
├── africa/
│   ├── __init__.py
│   └── zimbabwe.py
│
├── europe/
│   ├── __init__.py
│   ├── greece.py
│   ├── norway.py
│   └── spain.py
│
└── __init__.py

in this case, if every country file prints out a greeting, when __init__.py files selectively import some of the subpackages and submodules:

# world/africa/__init__.py  (Empty file)

# world/africa/zimbabwe.py
print("Shona: Mhoroyi vhanu vese")
print("Ndebele: Sabona mhlaba")

# world/europe/__init__.py
from . import greece
from . import norway

# world/europe/greece.py
print("Greek: Γειά σας Κόσμε")

# world/europe/norway.py
print("Norwegian: Hei verden")

# world/europe/spain.py
print("Castellano: Hola mundo")

# world/__init__.py
from . import africa

Note that world/__init__.py imports only africa and not europe. Similarly, world/africa/__init__.py doesn’t import anything, while world/europe/__init__.py imports greece and norway but not spain. Each country module will print a greeting when it’s imported.

>>> import world
>>> world
<module 'world' from 'world/__init__.py'>

>>> # The africa subpackage has been automatically imported
>>> world.africa
<module 'world.africa' from 'world/africa/__init__.py'>

>>> # The europe subpackage has not been imported
>>> world.europe
AttributeError: module 'world' has no attribute 'europe'

When europe is imported, the europe.greece and europe.norway modules are imported as well. You can see this because the country modules print a greeting when they’re imported:

>>> # Import europe explicitly
>>> from world import europe
Greek: Γειά σας Κόσμε
Norwegian: Hei verden

>>> # The greece submodule has been automatically imported
>>> europe.greece
<module 'world.europe.greece' from 'world/europe/greece.py'>

>>> # Because world is imported, europe is also found in the world namespace
>>> world.europe.norway
<module 'world.europe.norway' from 'world/europe/norway.py'>

>>> # The spain submodule has not been imported
>>> europe.spain
AttributeError: module 'world.europe' has no attribute 'spain'

>>> # Import spain explicitly inside the world namespace
>>> import world.europe.spain
Castellano: Hola mundo

>>> # Note that spain is also available directly inside the europe namespace
>>> europe.spain
<module 'world.europe.spain' from 'world/europe/spain.py'>

>>> # Importing norway doesn't do the import again (no output), but adds
>>> # norway to the global namespace
>>> from world.europe import norway
>>> norway
<module 'world.europe.norway' from 'world/europe/norway.py'>

The world/africa/__init__.py file is empty. This means that importing the world.africa package creates the namespace but has no other effect:

>>> # Even though africa has been imported, zimbabwe has not
>>> world.africa.zimbabwe
AttributeError: module 'world.africa' has no attribute 'zimbabwe'

>>> # Import zimbabwe explicitly into the global namespace
>>> from world.africa import zimbabwe
Shona: Mhoroyi vhanu vese
Ndebele: Sabona mhlaba

>>> # The zimbabwe submodule is now available
>>> zimbabwe
<module 'world.africa.zimbabwe' from 'world/africa/zimbabwe.py'>

>>> # Note that zimbabwe can also be reached through the africa subpackage
>>> world.africa.zimbabwe
<module 'world.africa.zimbabwe' from 'world/africa/zimbabwe.py'>

when executing python modules:

using python -m [submodules] to solve the import issues

标签:europe,__,python,py,africa,world,import
From: https://www.cnblogs.com/holylandofdora/p/18007455

相关文章

  • [转]gdb源码安装,指定使用的python版本
    转自:https://www.cnblogs.com/shengulong/p/8053370.html gdb调试python的时候,需要根据不同的python版本2.6、2.7、3.x安装相应的gdb;如何指定关联的python版本?下面gdb源码,解压后,进入目录:./configure-h并没有发现--with-python的选项。没有也没有问题,没有也可以自己加:whi......
  • 利用Python进行数据分析 pdf下载
    本书由Pythonpandas项目创始人WesMcKinney亲笔撰写,详细介绍利用Python进行操作、处理、清洗和规整数据等方面的具体细节和基本要点。第2版针对Python3.6进行全面修订和更新,涵盖新版的pandas、NumPy、IPython和Jupyter,并增加大量实际案例,可以帮助你高效解决一系列数据分析问题。......
  • Decorations in Python
    DecorationsinPythonReferences:ref1,ref2,ref3AdecoratorisadesignpatterninPythonthatallowsausertoaddnewfunctionalitytoanexistingobjectwithoutmodifyingitsstructure.FunctionsinPythonarefirst-classcitizens.Thismeansthatt......
  • 深度学习-DNN深度神经网络-反向传播02-python代码实现nn-41
    目录1.举例2.python实现1.举例2.python实现importnumpyasnpfromsklearn.datasetsimportfetch_mldatafromsklearn.utils.extmathimportsafe_sparse_dotdeftrain_y(y_true):y_ohe=np.zeros(10)y_ohe[int(y_true)]=1returny_ohemnist......
  • Python 机器学习 数据集分布可视化
    ​  Python的机器学习项目中,可视化是理解数据、模型和预测结果的重要工具。通过可视化可以观察数据集的分布情况,了解数据的特征和规律,可以评估模型的性能,发现模型的优缺点,分析预测结果,解释模型的预测过程。可视化数据集的分布和预测结果是整个过程中一个重要的步骤。通常可视......
  • 工作安排-od-python
    题目:小明每周上班都会拿到自己的工作清单,工作清单内包含n项工作,每项工作都有对应的耗时时长(单位h)和报酬,工作的总报酬为所有已完成工作的报酬之和。那么请你帮小明安排一下工作,保证小明在指定的工作时间内工作收入最大化。输入描述:输入的第一行为两个正整数T,n。T代表工作时长(单......
  • redis+python练习小问题
     1、“cannot import name 'Redis' from 'redis'"//python文件名用了“redis.py”,改成其他的就好了。这个一定要注意,很容易犯这种错,想要做什么功能,就用这个功能命名。2、NameError:name 'redis' is not defined//我开始是fromredisimportRedis,改成importredis,......
  • python 决策曲线 DCA
    importnumpyasnpimportmatplotlib.pyplotaspltfromsklearn.metricsimportconfusion_matrixdefcalculate_net_benefit_model(thresh_group,y_pred_score,y_label):net_benefit_model=np.array([])forthreshinthresh_group:y_pred_lab......
  • Python实现给视频添加字幕
    主要思路:1.用moviepy库处理视频文件;用pysrt库处理字幕。2.由于moviepy依赖名为ImageMagick免费开源图片编辑软件,所以要先安装ImageMagick开始:1.安装ImageMagick到官网 https://www.imagemagick.org/script/download.php#windows下载我这里选择ImageMagick-7.1.1-27-Q16-......
  • 11 - 初步了解Python
    初步了解Python参考资料:菜鸟教程:Python3基础语法PEP8:StyleGuideforPythonCodePythonDocs:SourceCodeEncoding菜鸟教程:Python3命令行参数PythonDocs:ExecutablePythonScripts知乎:#!/usr/bin/envpython有什么用?编程规范:PEP8在没有额外编程规范的前提下,建议翻阅并......