import student_class_mainRunning python Scripts
At times we are going to want to be able to execute python scripts (generally a .py file). That is, run all of the code and output something while not being in a JupyterHub, Colab, or interactive python environment.
We’ll focus on how to execute scripts from our JupyterHub.
Executing a Script from the Command Line
Consider our python script that defined our class from the previous notes (available here. We can execute this script from the command line pretty easily! If you look at the code you’ll see a number of things should print out when we do so.
First, log in on our
JupyterHuband upload the file there so you see it locally.Click on File -> New -> Terminal. Or, head to the ‘Launcher,’ scroll down, and click on “Terminal”.
This should bring up a terminal window at looking at your ‘root’ directory. That is, it is looking in your main folder by default.
We can run a
.pyfile usingpython path_to_file.py- If our file exists there, we can just type
python student_class.py- I have my file in a sub-directory so I can just tell it where to find it. For instance,
python 02_Big_Data_Management/student_class.pyIf it finds the file, you should see something like:
554
Justin Post
jbpost2
('Justin', 'Post')
[3, 10, 40]
[3, 10, 40, 12, 11]
Student(name=Justin Post, unity_id=jbpost2, grades=[3, 10, 40, 12, 11])
We just executed our first file! Woo.
Importing a File
Sometimes we’re going to want to import the code from a file but not execute it (similar to reading in a module). To do this, we need only a few changes to our .py file.
- We add
def main():around the code that creates a new object and prints out information. We then just tab that code in four spaces - Then, at the bottom of the file, we add
if __name__=="__main__":
main()
- If we run the code as we did above, it still executes (as it was the file being executed)
- However, the code won’t run if we
importthe file into apythonsession - This is super useful when we want to set up a sequence of files to run (a pipeline)
Download the file above and put it in your working directory. You can then either execute the code below in a python interpreter or in a notebook.
- We can
importthis file and it will give us access to the class but won’t execute the code to print things out.
- Nothing prints out! But we can create an instance of the class.
y = student_class_main.student("Jake Plummer", "jeplummer")
print(y)Student(name=Jake Plummer, unity_id=jeplummer)
- We can execute the code in the
main()part via
student_class_main.main()554
Justin Post
jbpost2
('Justin', 'Post')
[3, 10, 40]
[3, 10, 40, 12, 11]
Student(name=Justin Post, unity_id=jbpost2, grades=[3, 10, 40, 12, 11])
Sweet! This is going to help us out when we start dealing with data and/or model pipelines and when we get into streaming data!
Recap
It is quite useful to be able to execute a python script from the command line. Also, it is super useful to be able to read in a python script as a module!
Use the table of contents on the left or the arrows at the bottom of this page to navigate to the next learning material!