How to Create a Custom Module and Import It In the Python Repl
This is going to be one of my favorite sections of the entire course and I hope it will be yours as well because this is where we are going to start being able to access powerful outside code libraries and organize our code in a way where we can access all of these extra features and be able to bring those into our own programs.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In order to start this guide out the very first thing, I'm going to do is I'm going to review how to import a core library. And so there are libraries that you always have access to inside of Python such as when you call to string on some type of integer and it automatically converts an integer into a string.

You don't have to pull in any outside libraries in order to make that possible. However, there are other libraries that are built directly into the language but Python requires that you import those. So I'm going to start off here in the terminal if you're following along on Mac. You can open up your terminal application and if you are following on PC you can use power shell or command prompt. And so I'm going to say Python. If you do not have Python 3 set up is your default. Then you may have to type Python 3 but I have Python set up as my default here so I can type python and that is going to open up the REPL for me.

large

So here I can say import. And then the name of the library that I want to pull in. So I'm going to pull in the math library and this is one that we've brought on earlier on in the course so I want to review how you can do this. The syntax is to say import math and now that I hit return and it took me down to the next line. That means that it successfully imported the math library.

large

Now we can use it and so I can type math.sqrt and just pass in a numbers such as 25 and then if everything's working this should return 5. So as you can see that returns a float value thereof five point zero. So all of this is working properly and so that is how you can import a built-in library.

large

Now let's see how we can create our own custom module and then bring it in ourselves and so I'm going to hit control D to get out of this and I'm going to create a directory on my desktop. You can be anywhere in your application and I'm gonna create a directory here called python_import. You can call yours anything that you would like. I'm just doing this to have a place where we can go and add our custom library. I'm gonna say CD python_import and then this is going to change me into the directory as you can see right here. We have the desktop path and then python_import.

medium

If you are not familiar with how to navigate in the command line or the terminal like we have right here. Just please let me or an instructor know and they can point you to the right guides to help you Perform that type of navigation. You can also perform tasks such as creating a new directory and performing those kinds of tasks simply in the finder or the Windows Explorer.

So I don't feel like you need to use the terminal or the command prompt but that's why I'm doing it just so you can see everything that I'm doing and you can also type this code in exactly how I am. Now if you're following along in power shell the commands are slightly different, you can perform each one of these tasks in there as well if you're following along on Windows.

So now with all that being said, now that we're in this python_import directory I am going to create a new file. Now I'm going to be using the text editor vim in order to do this so that you can see everything that I'm working on right here. However, if you're not familiar with vim then you can use any kind of text editors such as visual studio code, Sublime Text, Atom anything like that and you can create your code snippets and then you can run them on the command line just like I'm doing right now.

I'm going to use VIM so you can see it all. I'm going to create a file here called helper.py and so to create a module in Python this is a first step which is to create the python file.

Now inside of this, I'm going to add a method. So it's going to be our familiar greeting function here, it's going to take in a first and a last set of function attributes of arguments and then on a new line I'm going to say return a string literal here where it's going to be formatted. And I'll say hi, pass in first and then last. So now if this is all working then all of this should return the values that we expect and we can test this out by calling print greeting and then pass in the values that we want. And that should be all we need.

I'm going to open up another terminal process here so that you can see the code and see this all workings going to go into that python import directory. And now if I call python and run the script by saying python helper.py then you can see that this prints out properly.

large

Our code is working exactly like we would expect.

Now what I want to do is I want to show you how we can actually import this greeting directly into the REPL which is pretty cool so if I start Python. Now we have started our Repl environment. Now if you remember when I said import math and then we could call math.sqrt paste in a value there and we get the result from that math library. Well, we can also now because we created our greeting module. We can go and we can perform the exact same task with our own custom module. So in order to do that we're going to follow the same syntax so I can say, import helper.

So what we want to import is the file name. If I would have named that file greeting.py then I would say import greeting.py, I'm gonna say import helper and then from there it instantly runs and everything works perfectly.

large

I'm going to get out of the session really quick and now I want to change this behavior I no longer want to simply print this value out, that works for debugging but I want to treat it like a real code library and so now I'm going to simply have a function that returns a value. Coming back down to the terminal, if I say Python here now I can import helper and then I can call helper.greeting and then pass in values just like we did in the file. If I run this you can see that it returns the formatted string that says Hi Christine Hudgens and so everything is working just like we had it in the file.

large

This is the one point that I really want to stress to you in this guide is showing you how all of the work that we've done throughout this entire course and we focus primarily just on the code and when it came to the development environment we worked with tools such as repl.it and that works for learning syntax but I wanted to bridge the gap and I wanted to show you how all of those kinds of processes, just like our function of greeting everything that we've done so far can be directly ported into real-world Python programs just like we have done in this guide.

If you were to go and build out a massive Django web application API you'd follow these exact same processes you'd import files you would create functions then you'd import them exactly like we've done right here.

So great job if you went through that! You now know how to create your own custom module and call it directly from the python repl.

Code

import math

math.sqrt(4)


# Place in file helper.py
def greeting(first, last):
  return f'Hi {first} {last}!'

# Call from repl
import helper

helper.greeting('Kristine', 'Hudgens')