Importing Modules

Importing Single Module

PowerShell provides an Import-module cmdlet to import modules in the current session.

# syntax: Import-Module <name of module>
Import-Module ActiveDirectory

Whereas, Python has a keyword import to load modules into the current Python Session, but the import statement does not make contents in the module directly accessible to the caller and are only accessible when they are prefixed with <module name> followed by a (’ . ‘) dot notation: <module name>.<property/method> like in the following example.

import os # importing module
print(os.getcwd()) # gets current working directory

Importing Multiple Modules

Both PowerShell and Python allows you to import multiple modules in the session by referencing the module names separated by (’ , ‘) comma(s), for example in Python the syntax would look something like import <module1>, <module2> ...

import time, sys # importing multiple modules
time.sleep(2) # sleeps for 2 seconds
print("Platform:",sys.platform) # platform identifier, like Win32

Similarly, in PowerShell Import-Module cmdlet also follows almost the same syntax: Import-Module <module1>, <module2> ...

Import-Module PSReadLine, AzureRM, Posh-Git

Importing Sub-Modules

Python allows us to import only the sub-module(s) of a module, which are basically packages inside a Python module using the from ... import ... keywords.

# SYNTAX: from <module>import <submodule1, sunmodule2, ...>
from random import shuffle 
array = [1,2,3,4]
shuffle(array)
print('shuffled array:', array)

or importing multiple sub-modules separated by commas.

from statistics import mean, variance, median, stdev

Programmers are also allowed to import all sub modules from a module, using the following syntax: from <module name> import *

from random import *

Module Aliases

Python also allows programmers to define alternate names or aliases of the modules. While importing a module use the as keyword in following syntax import <module name> as <alternate name> to define an alias or an alternative names, these are easy to use and you don’t have to type the complete name of the module while accessing module objects.

import math as m
print(m.pi)
print(m.e)

Just to summarize everything, following are number of ways to import a module in python:

import matplotlib 
import matplotlib.pyplot # import sub-module: pyplot
import matplotlib.pyplot as plt # import sub-module: pyplot with alias `plt`
from matplotlib import pyplot # import sub-module: pyplot
from matplotlib import pyplot as plt # import sub-module: pyplot with alias `plt`

 


NOTE: 

This is just a teaser content from my book in form of a blog post!

If you want to know more about Installing modules, Importing Module Aliases, Creating new Modules in Python and PowerShell and other more advanced topics on Modules?

Then read my book (below) which is still in progress, on lean publishing format.

Buy early, pay less, free updates!


My new book :  PowerShell Scripting Guide to Python

This PowerShell Scripting guide to Python is designed to make readers familiar with syntax, semantics and core concepts of Python language, in an approach that readers can totally relate with the concepts of PowerShell already in their arsenal, to learn Python fast and effectively, such that it sticks with readers for longer time.

“Use what you know to learn what you don’t. ” also known as Associative learning.

Book follows a comparative method to jump start readers journey in Python, but who is the target audience? and who should read this book –

  • Any System Administrator who want to step into Development or Programming roles, and even if you don’t want to be a developer, knowledge of another scripting language will make your skill set more robust.
  • Python Developers who want to learn PowerShell scripting and understand its ease of user and importance to manage any platform.

Python is one of the top programming languages and in fast changing IT scenarios to DevOps and Cloudto the future – Data ScienceArtificial Intelligence (AI) and Machine Learning Python is a must know.

But this PowerShell Scripting guide to Python would be very helpful for you if you already have some knowledge of PowerShell

NOTE! This is a Leanpub “Agile-published” book. That means the book is currently unfinished and in-progress. As I continue to complete the chapters, we will re-publish the book with the new and updated content. Readers will receive an email once a new version is published!

While the book is in progress, please review it and send any feedback or error corrections at [email protected]

Optical Character Recognition

Subscribe to our mailing list

* indicates required