Summit Middle School classes for Andrew Busch
Andrew Busch - Summit
  • Home
  • Algebra 1
    • Alg 1B - Last Week
    • Alg1B 14 HW - Intro to Functions
    • Alg 1B 11 - Rational Expressions
    • Alg 1B 12 - Radical Expressions
    • Alg 1B 10 v2.0 - Quadratic Functions >
      • 10b Graphing with Pennies - Desmos Tutorial
      • 10i Snowboard Quadratic - Alg1B
      • 10 Quadratics Project
    • Alg 1B 10 Book - Factoring Quadratics
    • Alg 1B 9 - Exponential Functions
    • Alg 1B 8.5 - Representing Data
    • Alg 1B 13 - Inequalities
    • Alg 1B 8 - Best Fit Lines and Linear Regression
    • Alg 1B 7 - Linearity
  • Geometry
    • Geom Last Week
    • Geom 12 - Probability
    • Geom 11 - Circumference, Area, Volume
    • Geom 10-Circles
    • Geom 9 - Right Triangles and Trigonometry
    • Geom 8 - Similarity
    • Geom 7 - Quadrilaterals and Other Polygons
    • Geom 6 - Relationships Within Triangles
    • Geom 5 - Congruent Trianlges
    • Geom 4 - Transformations
    • Geometry 3.5 - Constructions
    • Geom 3 - Parallel and Perpendicular Lines
    • Geom 2 - Reasoning and Proofs
    • Geom 1 - Basics of Geometry
  • Programming
    • Directions for Sharing Programs with Me
    • Hour of Code
    • Intro to Python >
      • Installing and Using Portable Python
      • Introduction to Programming
      • Interactive Storyteller
      • Sophisticated Calculator
      • Getting Started with Games
      • Word Length Frequency
      • Substitution Cipher
      • Simple Game of Paddleball
      • Animating Many Objects
      • Accelerator
      • Applying Trigonometry
      • GIFs
      • Programmatic Art
      • Battleship
      • Pong
      • CodeCademy.com Suggested Work
      • Python Resources
    • Advanced Python >
      • Python Installation
      • Review of Intro to Programming
      • Objects and Classes >
        • More on Classes: Functions, Methods, Inheritance
        • Quadrilaterals
      • tkinter >
        • Paddle Ball
        • Light Bike
        • Frogger
        • Snake Game
        • Breakout
      • Reading and Writing Files
      • Directories and Importing Modules
      • Raspberry Pi
      • API's
      • Python Puzzles
  • Clubs
  • Graphing Calculator
  • PARCC Practice
--Note--
Because of the restrictions on the BVSD network, this works on your home computer but not on school computers. FYI. 

Directories and Importing Modules


Importing Modules from a Different Folder/Directory

Importing modules from Python is really nice when they are saved in the same place as your program (the current directory) or in the default system path. When they aren't--like when they're installed on a flash drive, or in a different folder, or a different network drive, or tkinter doesn't know where the .gif file is even though you're staring it in the face, or... well you get the picture. Finding the file can be a pain in the neck.
Finding the full path to the file or directory:
On a windows machine:
"If you need to see the full path displayed that way, click anywhere to the right of the path in the address bar or right-click any part of the address bar and choose Edit Address. 

The right-click shortcut menu includes additional options that allow you to copy the current address to the Clipboard. Click Copy Address to save the location in a format that is optimized for copying and pasting folders in Windows Explorer or use Copy Address As Text if you plan to paste the folder path into a document. 

To copy the full path for an individual file, hold down the Shift key as you right-click the file, and then choose Copy As Path. This option is especially useful if you’ve found a file in Windows Explorer and you want to upload it to a Web site or open it in another program without browsing to the same location in an Open dialog box. Copy the full path for the file, then paste it into the File Name box in the target program. "   (from https://technet.microsoft.com/en-us/magazine/ff678296.aspx)
On a mac: 
        Drag and drop the file into the terminal and it will give you the directory path. Copy the path name from the terminal but, if there are any spaces in the file names, you will need to remove the '\' from the path name. 
Once you have the file path:
Let's say you want to import the file Awesome.py as a module and your directory structure is:
YourName/Programming/MyFiles/Awesome.py

Using the entire path (replacing slashes with dots) you can do a couple of different things:
1) You can modify where Python looks for files to import. (I'm not a fan of this one)
#import the system module
import sys

#change the system path to tell python where to look
sys.path.insert(0, '/YourName/Programming/MyFiles') ​

#now import the file you want
import Awesome

2) This one is my favorite option. You can add a file to the folder your importing from telling python this folder contains stuff to check out (i.e the folder is a python package)

Add a __init__.py file to the folder. Remember, this has two underscores before and after the "init".

#now either use:
from YourName.Programming.MyFiles.Awesome import *
#or
import YourName.Programming.MyFiles.Awesome
Here's a conversation about this from stackoverflow.

To * or not to * when importing a module.
While we're on the subject of modules... We didn't talk about this in the intro class, but now that we're talking about objects and classes, it's high time we had this conversation. Let's talk about the difference between:
    import random   (option 1)
        and 
    from random import *      (option 2)
Option 1: 
import  random
With this method, all functions and variables you want to use from random must be called using the dot-method.

If, in random, we want use the randint() function to generate a random integer between 1 and 10. With this option:
    import random
    #then we would access the randint() function by
    x = random.randint(1,10)


Option 2:
from random import *
With this option, you have access to all of the functions and variables defined in the module random without having to use dot notation. Except this can be VERY problematic if you don't know exactly what's in the module. For example, say you want to define your own function randint(). This would cause problems because there are now two different functions named the same thing in your program. One defined in random and one defined in the program you're writing. 

If, in random, we want use the randint() function to generate a random integer between 1 and 10. With this option:
    from random import *
    #we access the randint() function by
    x = randint(1,10)

The python documentation website considers it bad form to use the * because it can cause confusion regarding what is defined and what is not. I just want you to know the difference between the two options.
https://docs.python.org/3/tutorial/modules.html
Spaces in the file name:
By the way, in the terminal, if you have spaces in a file name, use the backslash character "\" in front of the spaces. For example, if you have the folder "Programming Course Documents", you would type in "Programming\ Course\ Documents"


Exercises:
1) Rewrite one of your previous programs using import sgfx rather than from sgfx import *.
2) Now save that program from step 1 to a different folder on your computer (not in your MyPrograms folder). Hopefully, your program doesn't know where to find sgfx.py. Use the full path of the sgfx.py file make the program run correctly.



Directories

We are still working on granting you access to the Command Prompt. Until then, we need to skip this section.
Up to this point, we've bypassed the need to understand the file system of the computer by using Portable Python. It's time to take off the training wheels. A "directory" is a fancy word for a "folder" on your computer. A great way to learn directories is to get used to using the terminal on your computer. In windows, this is called the "command prompt". It's not super complicated, but it is super easy to mess around with a computer. Most often, schools simply lock students out of the command prompt because they don't want to try and figure out what you messed up.

Not here.

At Summit, we're going to trust you. We're going to trust that you want to learn how to program and don't have malicious intent toward the school. We're going to trust that you aren't trying to teach us a lesson about giving students too much access. Please, don't make us regret trusting you. Please.

All that said, we are part of a larger school district that doesn't always jump at the opportunity to give students access to the soft underbelly of the network. Go figure. This year we moved all the computer labs. I don't know where the less restricted computers went. How much we can get done using the command prompt will depend on what kind of computers we can find.

If things don't work out, I'll post some stuff you can go through at home. You pretty much stop feeling like a n00b in less than 10 minutes.
This is a great little tutorial for the command prompt. We don't have all the files they're using, but you can get the jist of what they're doing.
Some more stuff I might be able to use if I can figure out how to get around the restrictions on what students can and cannot access on the BVSD network.
python_file_directories.ppt
File Size: 229 kb
File Type: ppt
Download File

Powered by Create your own unique website with customizable templates.