Python import

https://realpython.com/python-import/#basic-python-import

Basic Python import

Python code is organized into both modules and packages. This section will explain how they differ and how you can work with them.

The Python.org glossary defines module as follows:

An object that serves as an organizational unit of Python code. Modules have a namespace containing arbitrary Python objects. Modules are loaded into Python by the process of importing. (Source)

dummy

Packages

You can use a package to further organize your modules. The Python.org glossary defines package as follows:

A Python module which can contain submodules or recursively, subpackages. Technically, a package is a Python module with an __path__ attribute. (Source)

Note that a package is still a module. As a user, you usually don’t need to worry about whether you’re importing a module or a package.

In practice, a package typically corresponds to a file directory containing Python files and other directories. To create a Python package yourself, you create a directory and a file named __init__.py inside it. The __init__.py file contains the contents of the package when it’s treated as a module. It can be left empty.

Absolute and Relative imports

You’ve already seen from…import statements such as from math import pi, but what does the dot (.) in from . import africa mean?

The dot refers to the current package, and the statement is an example of a relative import. You can read it as “From the current package, import the subpackage africa.”

There’s an equivalent absolute import statement in which you explicitly name the current package:

from . import africa

Relative imports must be in the form from…import, and the location you’re importing from must start with a dot.

The PEP 8 style guide recommends using absolute imports in general. However, relative imports are an alternative for organizing package hierarchies. For more information, see Absolute vs Relative Imports in Python.

Python’s Import path

How does Python find the modules and packages it imports? You’ll see more details about the mechanics of the Python import system later. For now, just know that Python looks for modules and packages in its import path. This is a list of locations that are searched for modules to import.

Note: When you type import something, Python will look for something a few different places before searching the import path.

In particular, it’ll look in a module cache to see if something has already been imported, and it’ll search among the built-in modules.

You can inspect Python’s import path by printing sys.path. Broadly speaking, this list will contain three different kinds of locations:

The directory of the current script (or the current directory if there’s no script, such as when Python is running interactively) The contents of the PYTHONPATH environment variable Other, installation-dependent directories

Typically, Python will start at the beginning of the list of locations and look for a given module in each location until the first match. Since the script directory or the current directory is always first in this list, you can make sure that your scripts find your self-made modules and packages by organizing your directories and being careful about which directory you run Python from.

Create and Install a Local Package

When you install a package from PyPI, that package is available to all scripts in your environment. However, you can also install packages from your local computer, and they’ll also be made available in the same way.

Creating a local package doesn’t involve much overhead. First, create minimal setup.cfg and setup.py files in the outer structure directory:

# setup.cfg

[metadata]
name = raspberry_sensing
version = 0.1.0

[options]
packages = raspberry_pi_x

# setup.py

import setuptools

setuptools.setup()

In theory, the name and version can be whatever you like. However, they’ll be used by pip when referring to your package, so you should choose values that are recognizable and don’t collide with other packages you use.

One tip is to give all such local packages a common prefix like local_ or your username. packages should list the directory or directories containing your source code. You can then install the package locally using pip:

(Monitoring-ue6dFoDU) fabgt@fabgt-SAT:~/gitclone/Monitoring$ python -m pip install -e raspberry
Obtaining file:///home/fabgt/gitclone/Monitoring/raspberry
Installing collected packages: raspberry-sensing
Running setup.py develop for raspberry-sensing
Successfully installed raspberry-sensing

This command will install the package to your system. structure will then be found on Python’s import path, meaning you can use it anywhere without having to worry about the script directory, relative imports, or other complications. The -e option stands for editable, which is important because it allows you to change the source code of your package without reinstalling it.

Note

This kind of setup file works great when you’re working with projects on your own. However, if you plan to share the code with others, then you should add some more information to your setup file.

For more details on setup files, check out : https://realpython.com/pypi-publish-python-package/

Now that raspberry is installed on your system, you can use the following import statement:

from raspberry.raspberry_pi_x import ds12b20