Section 9: Modules and Packages

  Python Bootcamp 0 to Hero

Section 9 Menu

< Section 8 | Section 10 >

  1. Pip Install and PyPi
  2. Modules and Packages
  3. __name__ and “__main__”

Pip Install and PyPi

https://www.udemy.com/complete-python-bootcamp/learn/lecture/9497628#questions

  • Pypi is a repository for open-sourced third party Python packages.
  • It is similar to RubyGems in the Ruby world, PHP’s Packagist, CPAN for Perl and NPM for Node.js
  • So far, we’ve only used the libraries that come internally with Python.
    • math
  • There are many other libraries available that people have open-sourced and shared on PyPi.
  • We can use pip install at the command line to install these packages.
  • There are packages already created for almost any use case you can think of
  • A quick google search will usually help you discover a link to the PyPi page for the package, or for the package documentation.

Following examples may require firewall rules to be altered to allow for pip installations.

requests

C:\Users\red01>pip install requests
Requirement already satisfied: requests in c:\programdata\anaconda3\lib\site-packages (2.21.0)
Requirement already satisfied: idna<2.9,>=2.5 in c:\programdata\anaconda3\lib\site-packages (from requests) (2.8)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\programdata\anaconda3\lib\site-packages (from requests) (3.0.4)
Requirement already satisfied: urllib3<1.25,>=1.21.1 in c:\programdata\anaconda3\lib\site-packages (from requests) (1.24.1)
Requirement already satisfied: certifi>=2017.4.17 in c:\programdata\anaconda3\lib\site-packages (from requests) (2019.3.9)

colorama

pip install colorama
Requirement already satisfied: colorama in c:\programdata\anaconda3\lib\site-packages (0.4.1)
python
>>> from colorama import init
>>> init()
>>> from colorama import Fore
>>> print(Fore.RED + "some red text")

some red text

Searching for packages

“python package for excel”

pip install openpyxl

 

Modules and Packages

https://www.udemy.com/complete-python-bootcamp/learn/lecture/9497632#questions

Writing your own modules and packages

  • Now that we understand how to install external packages, let’s explore how to create our own modules and packages.
  • Modules are just .py scripts that you cal in another .py script
  • Packages are a collection of modules.
    • Needs to be an __init__.py script in the folder to show python the collection of scripts needs to be treated as a package.
      • This file needs no contents!  It just needs to exist!

Example:

Create 2 files in the same folder. myprogram.py and mymodule.py

Create myprogram.py

from mymodule import my_func()
my_func()
python myprogram.py

Traceback (most recent call last):
File “myprogram.py”, line 1, in
from mymodule import my_func
ModuleNotFoundError: No module named ‘mymodule’

Create mymodule.py
mymodule.py

def my_func():
    print("You found mymodule.py!")
python myprogram.py

You found mymodule.py!

Create a package

Folders and file setup:

|- myprogram.py
+- MyMainPackage
   |- __init__.py
   |- mainpackage.py
   +- SubPackage
      |- __init__.py
      |- subpackage.py

subpackage.py

def sub_report():
    print("I'm in folder SubPackage")

mainpackage.py

def main_report():
    print("I'm in 'mainpackage.py' located in folder MyMainPackage")

myprogram.py

from MyMainPackage import mainpackage
from MyMainPackage.SubPackage import subpackage

Run the program

python myprogram.py

I’m in ‘mainpackage.py’ located in folder MyMainPackage
I’m in folder SubPackage

mainpackage.main_report()
subpackage.sub_report()

__name__ and “__main__”

https://www.udemy.com/complete-python-bootcamp/learn/lecture/9497634?start=0#questions

Sometimes when you are importing from a module, you would like to know whether a mdule’s function is being used as an import, or if you are using the original .py file of that module.

Example: if someone runs a script directly from the command line, Python creates the string __name__ and sets it to value ‘__main__’

# one.py
def func():
    print("func() in one.py")
print("Top level in one.py")
if __name__=='__main__':
    print("one.py is being ran directly")
else:
    print("one.py has been imported as a module")
# two.py
import one
print("Top level in two.py")
one.func()
if __name__=='__main__':
    print("two.py is being ran directly")
else:
    print("two.py has been imported as a module")
python one.py

Top level in one.py
one.py is being ran directly

python two.py

Top level in one.py
one.py has been imported as a module
Top level in two.py
func() in one.py
two.py is being ran directly

Typical Use

#somefile.py
def a():
    pass
def b():
    pass
def c():
    pass
if __name__=='__main__':
    # now start your coding
    # yes, below the functions.  Odd, I know.

 

 

LEAVE A COMMENT