12 – Importing Files

In addition to reading in data from files, we can actually import Python code from other scripts. This is especially helpful if you’re working on a bigger project where you want to organize your code into multiple files and reuse code in those files. If the Python script you want to import is in the same directory as your current script, you just type import, followed by the name of the file without the dot PY extension. Imports statements are written at the top of a Python script, each one on a separate line. Python will still run if imports statements are included later in the script, but it’s common practice to always have these at the top. You’re only able to access what you’ve imported after this statement so, it’s just less confusing to have these written first. It’s also nice for readers to see what a script depends on before reading the rest of the code. Let’s start with a really simple example. Here, I have a Python file called other_script.py that just prints two plus three. In this file demo.py, I import other_script and then print four. When we run demo.py, this import statement tells Python to run code from that file which prints five. It then continues to execute the rest of the code in this file printing four. If instead this file had the line num equals two plus three, and we try to access this in demo.py, referencing it with just the name of the variable would return an error. To access objects in other_script.py, we need to use the name of the file followed by a dot, followed by the object to tell Python to look for this object in the other_script file we imported. Now, it accesses the variables successfully. When Python runs the script. It only has direct access to objects defined in the script. One of these objects is a module called other_script. A module is just a file with Python definitions and statements. When we import a Python file like this, it creates an object called other_script with a type module. Let’s see a more useful example of importing a module. Here, we have a Python file that contains useful functions we would like to use. One, that returns the mean of a list and one that adds five to each element of a list. We can import them into demo.py, and type useful_functions dot the name of the function to use them. You can imagine this would be very helpful if we had many functions used a lot in different files. Although, it seems a little annoying that we have to type out the whole name of the file each time we want to use a function from it. We can make this much simpler by adding an alias. Here, I made an alias for the useful_function module UF. Now, I can just type UF instead of the whole module name when calling functions from it. This is useful when we have objects we want to import from other Python scripts like functions. But, what if that script also includes executable statements in addition to function definitions that we don’t want to import. For example, what if useful_functions.py has code at the bottom of the script that tests its functions and prints the results? This code is nice if we run useful_functions.py to test out these functions, but unnecessary if we’re just trying to use these functions in another script. Here is where we can use this statement if name equals main. By including these executable statements inside this if main block, we tell Python to execute this code only when the main program being executed is this useful_functions.py. If we run this file the code inside this block is run. However, if we run another script that simply imports useful_function.py this code is not run. Generally, it’s good practice to write executable statements inside an if name block or alternatively include them in a function called Main and call this in the if name block. You’re probably wondering what this Name and Main stuff is. Whenever we run a script like this, Python actually sets a special built in variable called Name with two underscores before and after it for any module. Here, since we ran Python demo.py, Python recognizes this module as the main program and sets the name variable for this module to the string Main. For any modules that are imported in the script, this built-in name variable is just set to the name of that module.

Dr. Serendipity에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

Continue reading