Modules, Packages and PIP Flashcards
What is a module?
a file containing Python definitions and statements, which can be later imported and used when necessary.
What is a namespace?
A space in which some names exists and the names don’t conflict with each other
difference between:
import math
from math import pi
import math imports all entities in math and they will be called like math.pi
from math import pi imports pi only and will be called like pi
from module import * is risky because…
it imports all entities from module as their name e.g. pi not math.pi. need to be careful or conflicting names in code already
Import module using alias?
import module as alias
What does the platform module allow you to do?
The platform module lets you access the underlying platform’s data, i.e., hardware, operating system, and interpreter version information.
from platform import platform
How do you find out the generic name of the processor which runs your OS together with Python and your code?
from platform import machine
print(machine())
What does the processor function do?
The processor() function returns a string filled with the real processor name (the name of the CPU) (if possible).
What does the system function do?
A function named system() returns the generic OS name as a string.
What does the version function do?
The OS version is provided as a string by the version() function.
What does python_implementation() do?
returns a string denoting the Python implementation (expect CPython here, unless you decide to use any non-canonical Python branch)
What does python_version_tuple() do?
returns a three-element tuple filled with:
the major part of Python’s version;
the minor part;
the patch level number.
(Complete the sentence) Setting the generator’s seed with the same value each time your program is run guarantees that…
.. the pseudo-random values emitted from the random module will be exactly the same.
functions make up modules and modules make up ……
Packages
What does the name variable do when run directly or imported as a module?
when you run a file directly, its __name__ variable is set to __main__;
when a file is imported as a module, its __name__ variable is set to the file’s name (excluding .py)
What does a name beginning with _ or __ mean?
Inform users of your module that this is your variable, that they may read it, but that they should not modify it under any circumstances.
The entity should be treated as PRIVATE
What does the #! line at the beginning of a module do?
For Unix and Unix-like OSs (including MacOS) such a line instructs the OS how to execute the contents of the file (in other words, what program needs to be launched to interpret the text). In some environments (especially those connected with web servers) the absence of that line will cause trouble;
The names shabang, shebang, hasbang, poundbang, and hashpling describe the digraph written as #!, used to instruct Unix-like OSs how the Python source file should be launched. This convention has no effect under MS Windows.
What does a doc-string do?
Explain the contents of the module as a comment
How do you specify the folder you want the module from?
from sys import path
path.append(‘C:\Users\user\py\modules’)
import module
If you want convince Python that it should take into account a non-standard package’s directory, its name needs to be inserted/appended into/to the import directory list stored in the path variable contained in the sys module.
What does the __init__.py file do?
The __init__.py file lets the Python interpreter know that a directory contains code for a Python module. An __init__.py file can be blank. Without one, you cannot import modules from another folder into your project.
A Python file named __init__.py is implicitly run when a package containing it is subject to import, and is used to initialize a package and/or its sub-packages (if any). The file may be empty, but must not be absent.