• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Modules In Python / Python Modules

Python Modules

Author: PFB Staff Writer
Last Updated: August 27, 2020

This is a new series of articles here at Python for beginners, that are supposed to be a starting point for completely beginners of Python. See it as a cheat sheet, reference, manual or whatever you want. The purpose is to very short write down the basics of Python. This page will describe how to use Modules in Python.

Python Modules

Python modules makes the programming a lot easier. It’s basically a file that consist of already written code. When Python imports a module, it first checks the module registry (sys.modules) to see if the module is already imported. If that’s the case, Python uses the existing module object as is. There are different ways to import a module. This is also why Python comes with battery included

Importing Modules

Let’s how the different ways to import a module

import sys    		
#access module, after this you can use sys.name to refer to things defined in module sys.

from sys import stdout  
# access module without qualiying name. 
This reads from the module "sys" import "stdout", so that we would be able to 
refer "stdout"in our program.

from sys import *       
# access all functions/classes in the sys module. 

Catching Errors

I like to use the import statement to ensure that all modules can be loaded on the system. The Import Error basically means that you cannot use this module, and that you should look at the traceback to find out why.

Import sys
try:
    import BeautifulSoup
except ImportError:
    print 'Import not installed'
    sys.exit()

Python Standard Library

URL: http://docs.python.org/library/index.html Collection of of modules that are already on the system, there is no need to install them. Just import the modules you want to use. Search for a module: http://docs.python.org/py-modindex.html

Python Package Index

URL: http://pypi.python.org/pypi Created by community members. It’s a repository of software containing more than 2400 packages

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Modules In Python Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us