Transcript from the "Python Files & Modules" Lesson
>> Nina Zakharenko: So we've been working in the REPL this whole time, and the REPL is this incredibly useful, powerful tool. But sometimes we wanna save our Python code, right? We wanna run it again and again, we wanna save it into a project. We're gonna make files with the .py extension and then we're gonna see how to run them.
>> Nina Zakharenko: So just like variables in Python,
>> Nina Zakharenko: The convention for naming your files is with all lowercase and underscore. We're gonna run our Python files from VS Code, so let's create a new file. On Mac that's Cmd+N, on Windows it's Ctrl+N. And we're going to put a little bit of code in this file.
[00:00:53] We're gonna say, we have a few greetings.
>> Nina Zakharenko: And we're gonna loop over these greetings.
>> Nina Zakharenko: Oops, okay, so you might be looking at this and saying, using an ID, you promised me syntax highlighting, you promised me linting and error checking. This looks just like the REPL, where is it, right?
[00:01:33] So the key here is we need to save our file with the Python extension so that VS Code knows that it's a Python file. So I'll give you just a minute to finish typing this in. And then I'm gonna go ahead and save this file as hello.py. On Mac you just give it a .py extension.
[00:01:56] On Windows you might have to select Python file from the dropdown that appears. So Mac Cmd + S to save, Windows, CTRL + S to save. We're gonna save this as hello.py and there we go. There is our syntax highlighting. And look at that.
>> Nina Zakharenko: It's a linter.
[00:02:17] There's a red underscore here. And it's giving us a hint that we didn't type in something correctly, that we have a syntax error in our program. Can anyone spot it?
>> Speaker 2: Yeah, there's an extra quote.
>> Nina Zakharenko: That's right. So now if we take out that extra quote and we save our file, that linting error will go away.
[00:02:43] To run your Python code from the VS Code terminal, you're gonna use that command pallet that we learned about earlier in the day and you're going to hit Run Python File in Terminal, or narrow it down. And you should see the results here. Should say, hello world, bonjour world, hola world.
>> Nina Zakharenko: Notice that VS Code automatically picks up the virtual environment for us, that env environment that we saw earlier in the class. If we are in the right directory, so for me, I'm gonna have cd into here. If we wanna run our files from the terminal outside of VS Code, if we're in our inner virtual environment, we can just do that with the Python command.
[00:03:40] And then pass in the name of our Python file and it'll run it. Outside of the virtual environment you'll need to specify the correct version of python. And if you remember from the beginning, we checked our python version. That's the same command that you would use to run your Python program outside of the terminal.
[00:04:03] So just a few tips here.
>> Nina Zakharenko: I'm gonna make a new file and save it, called name.py, and I recommend you do as well. By saving it as a Python file first, I get the syntax highlighting right off the bat. So I'm gonna add this in. I'm gonna make a new variable with my name and then in the repo we were able to just kind of write variables and see the results right away.
[00:04:36] If we run this, we're not gonna see any output. It's because the REPL and running the Python programs works a little bit differently. So in your Python programs you have to explicitly print things out in order to see any output. So once we make that change we'll see the result right here.