2. Python Program Units 🐍 - JulTob/Python GitHub Wiki

🐍 Python programs

Are text files.

🐢 Extension

program.py

program_name.py

🦎 They run with the command line

python program.py

python3 program.py

🐊 Writing Packages

Each package in Python is a directory which MUST contain a special file called init.py. This file can be empty, and it indicates that the directory it contains is a Python package, so it can be imported the same way a module can be imported.

If we create a directory called foo, which marks the package name, we can then create a module inside that package called bar. We also must not forget to add the init.py file inside the foo directory. To use the module bar, we can import it in two ways: import foo.bar or from foo import bar

In the first method, we must use the foo prefix whenever we access the module bar. In the second method, we don't, because we import the module to our module's namespace. The init.py file can also decide which modules the package exports as the API, while keeping other modules internal, by overriding the all variable, like so: init.py:

all = ["bar"]


🐉 Arguments

🐉 Los argumentos de llamada de un programa se manejan tal que:

from sys import argv

script_name, arg1 = argv[]

sys.argv[1] is the first command-line argument that you type after the program name, sys.argv[2] is the second command-line argument that you type after the program name, and so forth

python3 program.py argv1 argv2

sys.argv[0] is the name of the program

from sys import argv

script, first, second, third = argv

print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)