python 在包中导入包_Python包| 如何创建和导入它们?

绝地灬酷狼 2023-03-05 10:59 87阅读 0赞

python 在包中导入包

什么是Python包? (What are packages in Python?)

Packages in Python are similar to directories or folders. Just like a directory that can contain subdirectories and folders and sub-folders, a Python package can have sub-packages and modules (modules are similar to files, they have .py extension).

Python中的软件包类似于目录文件夹 。 就像可以包含子目录文件夹子文件夹目录一样, Python程序包可以具有子程序包和模块 (模块与文件相似,扩展名为.py)。

Python has a hierarchical directory structure, with multiple sub-packages, sub-sub packages, and so on.

Python具有分层目录结构,其中包含多个子程序包,子子程序包等。

A directory in python (package) must contain a file named __init__.py in order for Python to consider it as a package. This file can be left empty but we usually prefer to place the initialization code for that package in this file.

python( )中的目录必须包含一个名为__init__.py的文件,Python才能将其视为包。 该文件可以保留为空,但我们通常更喜欢将该程序包的初始化代码放入此文件中。

包裹为什么重要? (Why are packages important?)

As we build more complex programs that are huge with a large number of modules, we look for a better way to organize it! Here these packages come as a solution. We place modules that are similar in one package. This way we can create multiple packages with multiple modules in them. Thus making our project more organized and to manage.

随着我们构建包含大量模块的庞大且复杂的程序,我们寻求一种更好的组织方式! 这些软件包是作为解决方案而来的。 我们将相似的模块放在一个包中。 这样,我们可以创建包含多个模块的多个包。 从而使我们的项目更具组织性和可管理性。

Let’s have a look at an example to get a clear idea.

让我们看一个例子来弄清楚一个想法。

python packages example

如何在python程序中访问包和模块? (How to access packages and modules in a python program?)

  • To access the module ‘hello’, simply call:

    要访问模块“ hello”,只需调用:

    1. import Robot.Actions.hello
  • Another way to call module ‘hello’ is:

    调用模块“ hello”的另一种方法是:

    1. from Robot.Actions import hello
  • To access all the module and sub-packages from a package:

    要从包访问所有模块和子包:

    1. from Robot import *

如何在Python中创建包? (How to create a Package in Python?)

Now that we have understood what a package is and what are modules it can and should contain it is very easy to create packages in python. Let’s summarize it with the following steps,

既然我们已经了解了什么是包,包可以包含哪些模块,应该包含哪些模块,因此使用python创建包非常容易。 让我们用以下步骤总结一下:

  • Step 1: Create a Directory (Package). Here we have created ‘Robot’.

    步骤1:创建目录( Package )。 在这里,我们创建了“机器人”。

  • Step 2: Create a file __init__.py in the directory

    步骤2:在目录中创建文件__init__.py

  • Step 3: Create subdirectories or modules in the main directory.

    步骤3:在主目录中创建子目录或模块。

Let’s look at a few examples,

我们来看几个例子

Define the function ‘nose’ in module ‘Face’

在模块“面部”中定义功能“鼻子”

  1. def nose():
  2. print ("I'm 'nose' from 'Face'")

Define the function ‘snooze’ in module ‘Alarm’

在模块“报警”中定义功能“暂停”

  1. def snooze():
  2. print ("'snooze' from 'Alarm' in sub-package 'Sound'")

Now let’s see what happens when we load the package and call the functions?

现在,让我们看看加载程序包并调用函数时会发生什么?

  1. import Robot
  2. from Robot import Design, Actions, Sound
  3. # (Note that here, we could also use from Robot import *)
  4. # Calling functions,
  5. Design.Face.nose()
  6. # Output would be: I'm 'nose' from 'Face'
  7. Sound.Alarm.snooze()
  8. # Output would be: 'snooze' from 'Alarm' in sub-package 'Sound'

Learn with a complete example – how to create and import a package and how to call its function? Example of Packages

通过完整的示例学习–如何创建和导入包以及如何调用其功能? 包示例

I hope this helps you try implementing this with multiple examples, and next time try to manage your project by creating appropriate packages and modules.

我希望这可以帮助您尝试使用多个示例来实现此目标,并且下次尝试通过创建适当的程序包和模块来管理项目。

Remember it doesn’t matter how small your project is currently, because you never know when you may have to extend it. And extending it systematically always helps.

请记住,当前项目的大小无关紧要,因为您永远不知道何时需要扩展它。 而系统地扩展它总是有帮助的。

Feel free to comment on questions or suggestions below!

随时对以下问题或建议发表评论!

翻译自: https://www.includehelp.com/python/packages.aspx

python 在包中导入包

发表评论

表情:
评论列表 (有 0 条评论,87人围观)

还没有评论,来说两句吧...

相关阅读