DEV Community

EvanRPavone
EvanRPavone

Posted on

Learning Python - Week 5

This is week 5 of learning python and this week I learned about python packages, script files and exception handling. I will be going over briefly about each section.

Python packages are very useful. It keeps your code organized, especially if you have a lot of code to do. There are many packages online that you can download through your terminal. To install packages through your terminal you need to have python installed, that is a given, with python installed you can use the command pip3 which is a python package installer. The format you need to use to install packages is like so:

pip3 install <package name>
Enter fullscreen mode Exit fullscreen mode

The top 10 packages for python are listed on this site, KDNuggets, it will tell you what each package is useful for. Each folder in the package has a file called init.py, this is needed for each folder in a package. To use a package with its classes and methods, it is a lot like calling python code that is saved in another file:

From <package_name> import <module name>
Enter fullscreen mode Exit fullscreen mode

Inside the package folder you will most likely have a main module file and folders. The main module file would have to have imported all the other files within each folder to be able to use them in your application:

from <main_folder>.<sub_folder> import <module>,<another_module>
Enter fullscreen mode Exit fullscreen mode

You can also use * to get all the modules without listing them out.

Now when it comes to making scripts in python, it is pretty easy. Create a python file and save it wherever you want on your computer. In the file put whatever you want, let’s say print(“Hello There”). In your terminal cd to where the file is located and in the terminal do

python3 script_application.py
Enter fullscreen mode Exit fullscreen mode

This should print out “Hello There”. Instead of having to type python3 in the terminal you can actually specify within the file that you’d like to use the Python interpreter to run the code. To do this add the following to the top of the script file:

#! /usr/bin/env python3

print(“Hello There”)
Enter fullscreen mode Exit fullscreen mode

You might end up needing permission to run this file in the terminal now but that is an easy fix by running this command:

chmod 755 <file name>.py
Enter fullscreen mode Exit fullscreen mode

Now you can run your script with ease by typing ./<your script file>.py

For exception handling, there are many ways to do this. I learned two ways using try/except and if else statement. Exception handling is definitely needed in order to be a good software engineer, so I’m told. If a user on your web application runs into an error, you do not want them to see a bunch of code exposing the inner workings of your site, instead you should just have them receive an error message on the site. Lets make a very simple method called sum that passes 2 numbers:

def sum(num1, num2):
    print(num1 + num2)

number1 = input(“Enter a number:”)

sum(number1, 12)
Enter fullscreen mode Exit fullscreen mode

The code above will give you an error and show you a lot of information you do not want your user to see. To fix this, I learned two ways, one is using the try except and the other is using an if else statement using isinstance method.

# Try/Except way
def sum(num1, num2):
    try: 
        print(num1+num2)
    except: 
        print(“There was an error!”)
number1 = input(“Enter a number:”)

sum(number1, 12)
Enter fullscreen mode Exit fullscreen mode

The code under the try method is what you want to happen. If the code ends up being an error like last time (it will for this as well), it will print “There was an error!” and will not show any of your code. The other way of doing this I still need to work on and understand but this is what I did following the instructor on my udemy course.

def sumAgain(num1, num2):
        if isinstance(num1, int) and isinstance(num2, int):
             print(num1+num2)
        else:
           print("data type was not a number for the parameters")

number1Again = input("Enter a number: ")
sumAgain(number1Again, 12)
Enter fullscreen mode Exit fullscreen mode

I couldn’t really tell you what is going on here other than the fact that if the code ends up being an error it will print out “data type was not a number for the parameters”.

Python is getting better and better and I am still enjoying it. I will most likely say that each post but I don’t have much longer to go in my course. I will see you next week!

Top comments (0)