LuckyPants
  • Home
  • Poems
  • Python
    • Python Interview Prep Notes for Students >
      • Merge Intervals Leetcode
      • Merge k Sorted Lists
    • Python Setup >
      • Pep 8 style guide
    • Lecture 1 - Basics
    • Lecture 2 - Loops, Lists >
      • Homework 2 Program 1 Step By Step
      • Homweork 2 Shuffle String Program
    • Lecture 3 - structures, packing, string functions >
      • Homework
      • Additional Exercises - you do not need to submit these
    • Lecture 4 - Dictionaries, Functions, Modules
    • Lecture 5 - functions, variable scope, modules >
      • Previous Exams
    • Lecture 6 - CLI, Files, Regex, pickle, shelve
    • Lecture 7 - capturing web content >
      • Processing XML data
    • Midterm Prep >
      • Programming assignment prep
    • Lecture 8 - Exceptions, Classes, Inheritance >
      • Exceptions Recording
      • Multiple Except Clauses Recording
      • Eating an Exception Recording
      • finally and else clause recordings
      • Exception Stack Trace Recording
      • exit() and CTRL+C
      • Class Variables Recording
      • Static Methods and Class Methods Recording
      • Inheritance Recordings
    • Section 9 - db, flask, pip >
      • Accessing Databases
      • HTTP Server
      • Web Applications
      • Finding and installing additional Libraries with pip
      • Web Services with Flask
      • Calling Web Services from Python
      • Placing your application on the web
    • Section 10 - flask, grpc, venv, docker >
      • Flask based Web Applications
      • gRPC
      • Packaging, Dependencies and Virtual Environments
      • Dockerized Python Application
    • Section 11 - Django, GUI >
      • Cross Site Scripting XSS and markupsafe
      • Django >
        • Cross Site Request Forgery CSRF
      • tkinter - frame and button
      • Toplevel
      • Frame
      • Mouse and Keyboard Events
      • Canvas
      • Menu
      • Radiobuttons and Checkboxes
      • Entry
      • ImageMagic
    • Lecture 12 - WebSockets, System Monitoring, PyGame >
      • Web Sockets
      • PyGame Platformer
    • Lecture 13 - Data Structure and Algorithms >
      • Arrays and Strings
      • Dictionaries
      • Linked Lists
      • Trees
      • Stack and Queue
      • Sets
      • Merge Sort
      • Coverage Reports
    • Lecture 14 - Subprocesses, Threads, Multiprocessing >
      • Graphs
      • Singleton Design Pattern
      • Subprocesses and Multithreading
      • Jupiter Notebooks
      • Pandas Operations
    • Extra >
      • Raspberry Pi
      • Raspberry PI home security
      • OpenCV and template matching
      • Compare SQLite DB's in Python
      • Natural Language Processing and nltk
      • Hackathon Projects
  • SaaS
    • Java Interview Prep Notes for Students
    • Setup >
      • SaaS and Cloud Computing
      • Eclipse set up
      • Building web application in Eclipse using Tomcat
    • Section2 >
      • Maven Intro
      • Set up a simple web app and deploy on Heroku
      • Add Jersey Dependencies
      • Dissecting our first REST service
    • Section3 >
      • Jackson intro
      • @POST, @PUT, @DELETE
      • Your Project
    • Section4 >
      • Rules of Injection
      • Root Resource, Subresource, Subresource Locator
      • @CONTEXT
    • Section 5 >
      • Heroku, Java, Jersey and Postgres
      • Music Store with Postgres
      • Working with Files
      • Storing files in Postgres
      • Homework
    • Section 6 >
      • Books Store with Mongo
      • Google APIs
      • Twitter APIs
      • Properties for a web application
      • Homework
    • Section 7 >
      • Jersey Client Famework
      • Java Jersey SalesForce and OAuth
      • Servlets and JSP
      • Data access examples
    • Section 8 >
      • MVC - Model View Controller
      • JSTL Core tags
      • Scopes
      • Building Services to work with forms
    • Section 9 >
      • Calling REST services with JQuery
      • Angular.js
    • Section 10 >
      • consuming SOAP services
      • Web Sockets
      • Websockets, continued
    • Section 11 - Software Development Methodologies, Testing, Performance and Security >
      • Software Development Methodologies
      • Testing, Performance and Security
    • Your Project
    • Previous Projects
    • Course Project Submission
    • SaaS Projects
    • Install Tomcat on Mac

Create module - Lab

Step 1: Run following command in your terminal
$ python3 -c "import site; print(site.getsitepackages())" 

You should get an output that looks like this:
['/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages', '/Library/Frameworks/Python.framework/Versions/3.4/lib/site-python', '/Library/Python/3.4/site-packages']


Step2: Download the file below
mysearch.py
File Size: 13 kb
File Type: py
Download File

Open the file and understand the content - there is: 

data_list variable, containing quotes,

search function, that performs search over data_list

and we also execute the search function for a hardcoded* string (if you don't know what hardcoded means, please ask to explain)

Run the file and see how it works

python3 mysearch.py

Step3: Go to the Python library folder, use the path you got above:

['/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages', '/Library/Frameworks/Python.framework/Versions/3.4/lib/site-python', '/Library/Python/3.4/site-packages']

Copy your mysearch.py there or place it in the same directory where your program is

We just created a Python module, now we will try to use it. 


Step4: Open python in terminal, follow the blue italic commands

We will now try to use our module, 

Let's try to call data_list variable:

>>> data_list
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'data_list' is not defined


It doesn't work, because we did not explain where we are getting the data_list from to Python, 

so let's do that:

>>> import mysearch
Found at  10 If you love a flower that lives on a star, it is sweet to look at the sky at nig
Found at  31 For millions of years flowers have been producing thorns. For millions of years 
Found at  44 Of course, I love you,' the flower said to him. 'If you were not aware of it, it
Found at  77 If someone loves a flower of which just one exists among all the millions and mi
Found at  81 If you love a flower that lives on a star, then it's good at night, to look up a
None



You should see the output you saw earlier when you ran the mysearch.py, this is because Python now loaded/executed your module and should now be aware of it's content. Let's test that:

>>> data_list
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'data_list' is not defined


Still doesn't work - this is because Python is not sure which data_list we are talking about - we have to specify the module name we are importing it form:

>>> mysearch.data_list
['And now here is my secret, a very simple secret: It is only with the heart that one can see rightly; what is essential is invisible to the eye.', 'All grown-ups were once children... but only few of them remember it.', 'People  ...


>>> mysearch.search("someone")
Found at  20 When someone blushes, doesn't that mean 'yes'?
Found at  31 For millions of years flowers have been producing thorns. For millions of years 
Found at  77 If someone loves a flower of which just one exists among all the millions and mi
>>> 

Exit out of Python to clear it's working memory:>>> exit()

Go back to continue:
>>> python3

We can also explicitly import just a specific component, make sure Python has no memory of data_list

>>> data_list
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'data_list' is not defined

>>> mysearch.data_list
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'mysearch' is not defined
>>> 


Now, run:

>>> from mysearch import data_list
Found at  10 If you love a flower that lives on a star, it is sweet to look at the sky at nig
Found at  31 For millions of years flowers have been producing thorns. For millions of years 
Found at  44 Of course, I love you,' the flower said to him. 'If you were not aware of it, it
Found at  77 If someone loves a flower of which just one exists among all the millions and mi
Found at  81 If you love a flower that lives on a star, then it's good at night, to look up a
None



>>> data_list
['And now here is my secret, a very simple secret: It is only with the heart that one can see rightly; what is essential is invisible to the eye.', 'All grown-ups were once children... but only few of them remember it.', 'People have ...


Because we did not import search function, Python has no knowledge of it

>>> search("something")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'search' is not defined

>>> mysearch.search("something")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'mysearch' is not defined


Let's import both search and data_list:
>>> from mysearch import data_list, search
>>> search("draw")
Found at  79 Will you draw me a sheep?

>>> exit()

Return back to Python:
python3

>>> from mysearch import *


Call your search function and print out data_list

Step5. Create a new module - create a new module, name it to your liking, place a simple variable and a simple function in it, follow same steps above to import it and to use your variable and function


Step6. Run following

>>> import sys
>>> sys.path


You should see something similar to output below, those are the paths where Python will be looking for the libraries

['', '/usr/local/lib/python2.7/site-packages', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages']



  • Home
  • Poems
  • Python
    • Python Interview Prep Notes for Students >
      • Merge Intervals Leetcode
      • Merge k Sorted Lists
    • Python Setup >
      • Pep 8 style guide
    • Lecture 1 - Basics
    • Lecture 2 - Loops, Lists >
      • Homework 2 Program 1 Step By Step
      • Homweork 2 Shuffle String Program
    • Lecture 3 - structures, packing, string functions >
      • Homework
      • Additional Exercises - you do not need to submit these
    • Lecture 4 - Dictionaries, Functions, Modules
    • Lecture 5 - functions, variable scope, modules >
      • Previous Exams
    • Lecture 6 - CLI, Files, Regex, pickle, shelve
    • Lecture 7 - capturing web content >
      • Processing XML data
    • Midterm Prep >
      • Programming assignment prep
    • Lecture 8 - Exceptions, Classes, Inheritance >
      • Exceptions Recording
      • Multiple Except Clauses Recording
      • Eating an Exception Recording
      • finally and else clause recordings
      • Exception Stack Trace Recording
      • exit() and CTRL+C
      • Class Variables Recording
      • Static Methods and Class Methods Recording
      • Inheritance Recordings
    • Section 9 - db, flask, pip >
      • Accessing Databases
      • HTTP Server
      • Web Applications
      • Finding and installing additional Libraries with pip
      • Web Services with Flask
      • Calling Web Services from Python
      • Placing your application on the web
    • Section 10 - flask, grpc, venv, docker >
      • Flask based Web Applications
      • gRPC
      • Packaging, Dependencies and Virtual Environments
      • Dockerized Python Application
    • Section 11 - Django, GUI >
      • Cross Site Scripting XSS and markupsafe
      • Django >
        • Cross Site Request Forgery CSRF
      • tkinter - frame and button
      • Toplevel
      • Frame
      • Mouse and Keyboard Events
      • Canvas
      • Menu
      • Radiobuttons and Checkboxes
      • Entry
      • ImageMagic
    • Lecture 12 - WebSockets, System Monitoring, PyGame >
      • Web Sockets
      • PyGame Platformer
    • Lecture 13 - Data Structure and Algorithms >
      • Arrays and Strings
      • Dictionaries
      • Linked Lists
      • Trees
      • Stack and Queue
      • Sets
      • Merge Sort
      • Coverage Reports
    • Lecture 14 - Subprocesses, Threads, Multiprocessing >
      • Graphs
      • Singleton Design Pattern
      • Subprocesses and Multithreading
      • Jupiter Notebooks
      • Pandas Operations
    • Extra >
      • Raspberry Pi
      • Raspberry PI home security
      • OpenCV and template matching
      • Compare SQLite DB's in Python
      • Natural Language Processing and nltk
      • Hackathon Projects
  • SaaS
    • Java Interview Prep Notes for Students
    • Setup >
      • SaaS and Cloud Computing
      • Eclipse set up
      • Building web application in Eclipse using Tomcat
    • Section2 >
      • Maven Intro
      • Set up a simple web app and deploy on Heroku
      • Add Jersey Dependencies
      • Dissecting our first REST service
    • Section3 >
      • Jackson intro
      • @POST, @PUT, @DELETE
      • Your Project
    • Section4 >
      • Rules of Injection
      • Root Resource, Subresource, Subresource Locator
      • @CONTEXT
    • Section 5 >
      • Heroku, Java, Jersey and Postgres
      • Music Store with Postgres
      • Working with Files
      • Storing files in Postgres
      • Homework
    • Section 6 >
      • Books Store with Mongo
      • Google APIs
      • Twitter APIs
      • Properties for a web application
      • Homework
    • Section 7 >
      • Jersey Client Famework
      • Java Jersey SalesForce and OAuth
      • Servlets and JSP
      • Data access examples
    • Section 8 >
      • MVC - Model View Controller
      • JSTL Core tags
      • Scopes
      • Building Services to work with forms
    • Section 9 >
      • Calling REST services with JQuery
      • Angular.js
    • Section 10 >
      • consuming SOAP services
      • Web Sockets
      • Websockets, continued
    • Section 11 - Software Development Methodologies, Testing, Performance and Security >
      • Software Development Methodologies
      • Testing, Performance and Security
    • Your Project
    • Previous Projects
    • Course Project Submission
    • SaaS Projects
    • Install Tomcat on Mac