Git Product home page Git Product logo

cssi-prework-python-modules-libraries's Introduction

Python - Importing Libraries and Modules

Objectives

  • Understand how to look for, import and use Python libraries

Importing a Module

As Python code gets larger and mode complex, developers may choose to group related code into individual files, called modules. Just like any script, a module can define functions, classes and variables.

Using modules makes code easier to work with because it adds an extra layer of organization.

To import a module, just use the import keyword followed by the name of the file you'd like to load. There is no need to use the .py extension.

import musician
from musician import *

The first line imports the musician.py file itself. The second line imports all the classes and functions contained in that musician.py file. However, if you are only going to use a few attributes (functions, variables, classes) from a module, you should specify which ones to import.

import musician
from musician import songs, play_music

Importing a Library

Once a module is available for public use, it is then generally referred to as a library. Libraries are Python scripts written to accomplish common tasks, and are widely shared by developers.

Using libraries requires three steps:

  1. Install pip, which is a tool that helps get and manage Python packages. You will only need to do this once. To install pip on a Mac, you can use easy_install in the command line
>>>sudo easy_install pip
  1. Install the library in your environment using the pip command. You will need to install any new libraries you want to use. If you don't have a library installed, when you try to use it, you'll get an NameError message: NameError: name 'library_name' is not defined. This message generally just means that you need to install that package locally.
>>pip install library_name
  1. Import the library at the top of your Python script.
import library_name

Here's a list of great Python modules you may want to play around with.

An example of a basic Python library is- random.py. Since many developers would want a way to generate random numbers, someone created and published code with a variety of methods that accomplishes this goal. (random.py does not need to be installed - it comes with Python). You can find documentation on this library here to learn about all the different functions you can call from it.

To use any of the methods in random.py, just import the random library, and then call the method by using dot notation:

import random
print random.randint(1,3)

There are a lot of libraries to explore, including this list of 20 common ones including Pyglet, a library for 3D animation and NumPy which allows for complext mathematical operations. Before writing methods or functions to do something routine, check to see if a library exists - it will make your life a lot easier.

Conclusion

Modules allow Python developers to keep code in distinct files, seperated by functionality. When those modules are shared with others, they are called libraries. This code is easy to import and easy to use in your own scripts.

cssi-prework-python-modules-libraries's People

Contributors

dfenjves avatar nanselmo avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cssi-prework-python-modules-libraries's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.