avatarLaxfed Paulacy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1491

Abstract

Save this file as <code>main.py</code> in the parent folder of the package.</p><p id="ddff">Run <code>main.py</code> and observe that importing your package works. Then, add the following code to <code>main.py</code>:</p><div id="e527"><pre><span class="hljs-keyword">import</span> mypackage.<span class="hljs-keyword">module</span><span class="hljs-number">1</span> <span class="hljs-keyword">import</span> mypackage.<span class="hljs-keyword">module</span><span class="hljs-number">2</span>

mypackage.<span class="hljs-keyword">module</span><span class="hljs-number">1</span>.greet(<span class="hljs-string">"Cedar"</span>) mypackage.<span class="hljs-keyword">module</span><span class="hljs-number">2</span>.depart(<span class="hljs-string">"boredom"</span>)</pre></div><p id="9f9e">Save and run <code>main.py</code> again, and observe that both <code>greet()</code> and <code>depart()</code> get called.</p><p id="7139">The reason for the error in the previous step is that when you import the <code>mypackage</code> module, the <code>module1</code> and <code>module2</code> namespaces aren’t imported automatically. Therefore, you need to import them too.</p><p id="5704">Under the hood, modules are imported from packages using dotted module names with the following format: <code>import <package_name>.<module_name></code>.</p><p id="dee1">So, for both <code>import</code> statements, the package name is <code>mypackage</code>, followed by a dot, and then the name of t

Options

he module you want to import.</p><p id="1057">As with modules, there are several variations on the <code>import</code> statement that you can use when importing packages.</p><p id="69b0">By explicitly importing the sub-modules of a package, you can avoid <code>AttributeError</code> and successfully call the functions and classes defined in those modules.</p><p id="5934">Packages are a powerful way to organize your Python code. By understanding how to create and import packages, you can better structure your code and make it more modular and reusable.</p><p id="5fb4">In conclusion, working with packages in Python involves understanding how to import modules from packages using dotted module names and using various <code>import</code> statements to access the functionality within those modules.</p><div id="7475" class="link-block"> <a href="https://readmedium.com/python-introducing-python-packages-63404e69320a"> <div> <div> <h2>PYTHON — Introducing Python Packages</h2> <div><h3>Technology alone is not enough. It’s technology married with the liberal arts, married with the humanities, that yields…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*m7DrMmEFpQH3t7-y.jpeg)"></div> </div> </div> </a> </div></article></body>

PYTHON — Working with Python Packages

The best method for accelerating a computer is the one that boosts it by 9.8 m/s². — Anonymous

Working with Python Packages

Python packages are a way of structuring Python’s module namespace by using “dotted module names.” A package is a directory that can contain sub-packages and modules. In this tutorial, you’ll learn how to work with packages in Python.

To see how importing a module from a package works, create another IDLE editor window. You will create a calling module. At the beginning of the file, import the package with import mypackage. Save this file as main.py in the parent folder of the package.

Run main.py and observe that importing your package works. Then, add the following code to main.py:

import mypackage.module1
import mypackage.module2

mypackage.module1.greet("Cedar")
mypackage.module2.depart("boredom")

Save and run main.py again, and observe that both greet() and depart() get called.

The reason for the error in the previous step is that when you import the mypackage module, the module1 and module2 namespaces aren’t imported automatically. Therefore, you need to import them too.

Under the hood, modules are imported from packages using dotted module names with the following format: import <package_name>.<module_name>.

So, for both import statements, the package name is mypackage, followed by a dot, and then the name of the module you want to import.

As with modules, there are several variations on the import statement that you can use when importing packages.

By explicitly importing the sub-modules of a package, you can avoid AttributeError and successfully call the functions and classes defined in those modules.

Packages are a powerful way to organize your Python code. By understanding how to create and import packages, you can better structure your code and make it more modular and reusable.

In conclusion, working with packages in Python involves understanding how to import modules from packages using dotted module names and using various import statements to access the functionality within those modules.

Packages
Working
ChatGPT
Python
Recommended from ReadMedium