Python Modules

So far, we've been creating programs that have only one file. In this section, you'll see how to add other files to your project.

Start a new project for this. Call it PythonModules. Click File > New and add a Python file to the project. Call this file FirstFile. Click File > New again and add a second Python file. Call this file SecondFile. (To add a new file, you can also right-click your project name, PythonModules in the image below. Select New > File from the menu that appears.)

You should now have two blank Python files open in the PyCharm editor. Have a look at the Project area on the left. (If you can't see a Project area, from the menus at the top of PyCharm, click View. From the View menu, click Tool Windows > Project.) You should see this:

Python Project files in PyCharm

If you close down a file in the main editor window, you can double click a file here, in the Project window, to open it up again.

The reason you'd want other files in your project is because your code can get very long. It's a good idea to take some of it out of your main program and move it to separate files. For example, you may have a lot of code that checks for errors. Rather than this error checking code clogging up a single file, it's much better to move it to a file of its own. This other file can be imported quite easily. Let's see how.

Make sure your SecondFile is displayed in the PyCharm editor. Now add some functions to the file:

def student_name(student):

print(student)

 

def subject_taken(subject):

print(subject)

 

def exam_grade():

print("GRADE NOT AVAILABLE")

You file should look like this:

A second Python file showing three functions set up

Now click on the FirstFile tab. It should be blank.

So we have three functions set up in a second file. The question is, how can we access them from the first file? Well, we can't just do this from the first file:

student_name("Jessica Jones")

This would work OK if the student_name function was in the FirstFile code. But it's not, it's in the SecondFile code. To get at the function in SecondFile, we need to import it. In your FirstFile, then, add this line:

import SecondFile

What this import statement does is to make available all the functions from SecondFile. However, you need to refer to them in this format:

file_name.function_name

To print out a student name, then, we'd do this:

SecondFile.student_name("Jessica Jones")

To print out a grade, it would be this:

SecondFile.exam_grade()

Try it out. After your import statement, add the line SecondFile.student_name("Jessica Jones"). Your code should look like this:

Python coding to reference another module

Run the program and you should find that "Jessica Jones" prints out.

You may not want to import all the functions from your other files, though. In which case, you can just import the function or functions you need. The format is this:

from module_name import function_name/s

Change your import line to this:

from SecondFile import student_name

Pycharm should now show an error, and the second line in your code will be underlined in red. This is because you can just go ahead and use the function, without adding the module name. So this:

student_name("Jessica Jones")

and not this:

SecondFile.student_name("Jessica Jones")

Change your code to this first one:

Importing a specific Python function

If you need other functions, just add them after a comma:

from SecondFile import student_name, subject_taken

We're now importing two of our functions, student_name and subject_taken.

You can use the * symbol to import all functions from your module. Like this:

from SecondFile import *

But the practice is frowned upon as you could be slowing down your program, or it might not import all the function in certain circumstances.

You can use the as keyword, if you want to store your imports under a variable name:

import SecondFile as sF

You can then do this when you call a function:

sF.student_name("Jessica Jones")

Another way to access functions is to store them in a variable. This is useful if you're going to be using that function a lot:

import SecondFile

studentName = SecondFile.student_name

Notice that you don't need the round brackets after the function name, student_name.

You can then just use the studentName variable with a pair of round brackets:

studentName("Jessica Jones")
studentName("Luke Cage")
studentName("Kamala Khan")

In the next lesson, you'll learn how to import inbuilt and external Python libraries.

Import Python Libraries >