Practical Guide to Python Running py Files
Transcript from the "Running py Files" Lesson
>> Functions are a way to help us organize our code in a way that is reusable. It lets us accept arguments like we saw earlier on in the class. It also lets us provide defaults where needed and loops. Let us take actions on collections of items that we've built up our lists or dictionaries or tuples.
[00:00:21] And then we can use Boolean logic to control the flow of the program. Now, we've been working in the repo so far, it's time to move on to working in Python files. We wanna be able to save, and reuse, and run again. So, if you need to create a Python file, it ends in a .py extension.
[00:00:49] There are some naming conventions that you can find in pep eight guidelines, but for the most part Python file names should be all in lowercase. They should be separated. The words should be separated with underscores. File names should be as short as possible. And they should end in .py.
[00:01:07] We're gonna talk about modules a little bit later, but you don't want to. It's called Smurf. Name your files. So you don't wanna say my _libAPIs, my_libhelpers, my_libutilities. Instead, you would put all of those in a folder called my_lib and just name them the simple files names, just APIs.
[00:01:36] Just exceptions just blog.py. Here are some bad example file names. Don't name your Python files in all caps. Don't use camel case. So capitalizing the first letter of each word that is a best practice or standard from a different programming language. And don't use smurfy really_long_ever_descriptive_project_file_name.py. You might see .pyc files in your directory as you start running your Python projects, those are.
[00:02:16] Complied intermediary python files they are for optimization. You don't have to worry about them. But you do need to be careful about not committing them to get. So those pyc files are just they don't matter. They don't need to be included with your program. And sometimes, stale versions of these files can cause problems.
[00:02:41] For example, if you're checking out a different branch in git, and your pi c files don't really match. So if you're running into weird problems when working with Python files, you can use this command on Mac or Linux to. Find all of those pyc files and just blow them away all at once.
[00:03:03] We already saw how to run Python programs from VS code and how to create new Python files. If you want to run that same file from your terminal, you just need to make sure that you have your Virtual Environment activated, and let's see what I have in this file.
[00:03:25] So again, when your virtual environment is activated, you can just use Python, not Python 3 or Python 3.9. Python is the executable in your virtual environment that you know maps to the right place. And then you would just pass in the file name. Or envious code We would want to make sure to exit our ripple and then run python file in terminal.
[00:03:58] And I will go ahead and do the same thing. So those are the different options. Once we move on to files, remember earlier, I said that the repo, when you type a variable or a string, it'll just show you the value right then and there. That functionality does not exist in files.
[00:04:16] So if you want to see the output in Python files, you're gonna need to start putting print statements in them. So make sure that you print the values of your strings if you want to see them. If not, that's totally fine as well. You can have print statements in while you debug and then comment them out once your program is ready.
[00:04:38] Or once you start writing more advanced programs, you might use the logger module. Which lets you configure how output from your Python program is presented. That is outside of the scope of the course. So I won't be covering it today. So if you're going to be sharing your code with a team, or you plan on sharing it, or you're working in production.
[00:05:04] You generally, don't want to leave print statements littered in your code, those are for your own debugging, so something to be mindful of. Output formatting tips, remember earlier I showed you the backslash n signified a new line, backslash T is for tab. You can use the special control characters for your advantage when you're printing stuff out, and you want to make sure that it is readable.
[00:05:34] You can also pretty print with something called the pretty print module. And I wanna show that to you quickly because I find it super useful. So, opening up my repple here, I'm going to create a long list if I tried to print this, it's not very readable. Pretty print is included in the Python standard library.
[00:06:05] So I can say from p print, import p print And that will let me access this. This module, because p print is not one of those built-in functions that we saw. It's not len it's not print where we can just use it without importing it. This is kind of a special case.
[00:06:28] And once you have imported it, we can pass in the long list here, and it will go ahead and print it out with line breaks. So if you're looking at complex code, this is a great tool to use to make it more human readable