Check out a free preview of the full Practical Guide to Python course

The "Course Setup" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina creates a directory and virtual environment for the Python project. The virtual environment allows a specific version of Python to be used in a project without interfering with other versions required by the operating system.

Preview
Close

Transcript from the "Course Setup" Lesson

[00:00:01]
>> To check for Python 3.9, you'll want to pull up a terminal. So, if I run Python 3 -- version, I should see Python 3.9. You should see that as well. It's especially important to install the latest version of Python because your machine especially if you're running a Mac might already come with a version of Python.

[00:00:26]
Depending on your OS, it might be an older one. So we want to make sure that we're working with the latest and greatest. Now we want to make a project directory and create a virtual environment in it. So for this example, Let me start off from the same point that you're all starting off at.

[00:00:56]
Make a directory. Make sure that you're in your home directory. So in this case for Mac or Linux, just cd.home, and then make a directory called pyworkshop. And cd into it, should be an empty directory. Now we want to create a virtual environment, and activate it. In order to do that, we're gonna use Python 3 -m the dash m means run this next command as a module which is gonna be the vn pythons 3 built in virtual environments.

[00:01:36]
And the next command is gonna be a name. In this case, I'm just gonna call it the standard env. It's a pretty good idea to name your virtual environments env because if you're using a standard git ignore for Python, this is one of the folders that's gonna be automatically excluded.

[00:01:54]
And you're not going to want these auto generated files to end up in your git repository. Now if I take a look here, I should see a env directory. I'm not gonna talk about what's in this directory for the moment. We're gonna talk about that a little bit later on in the class but basically it contains a standalone Python version, a standalone Python interpreter.

[00:02:25]
And when we work in that interpreter, we are guaranteed that we are using the version of Python that we used when we created it. And any packages that we install when that virtual environment is activated are going to stay in that virtual environment and not pollute your system settings.

[00:02:43]
So working in a virtual environment for a project no matter how small it is, is generally a pretty good idea. In order to activate it on Mac, I'm gonna source env/bin/activate and your terminal might not be as fancy as mine, you'll probably see something that looks like this.

[00:03:06]
It'll say env in parentheses followed by the rest of your prompt. And that means that the virtual environment is now activated. Now I can type Python in here. I don't need the three anymore. And the Python executable points to that version of Python that we used when we created the virtual environment.

[00:03:30]
You also wanna make sure that you have VS code. If you are running on Mac and you recently installed VS code, you might not have the code executable. You can check if you have it by typing code --version. If you are on a Mac and you don't see anything here, you might have to follow an extra step.

[00:03:55]
Sorry, that is right here which is called launching from the command line, install code command on path, I'll show you how to bring up this dialog box in a second. But if you're not seeing that the instructions are right there. Now, we wanna do a little bit of configuration for VS code.

[00:04:18]
We wanna make sure that we have the Python extension installed. You're not gonna see anything from the Python extension until you open your very first Python file. I have a link here to some list of keymap extensions if you want to remap your key bindings in VS code to Emacs or Vim or Sublime or something else.

[00:04:37]
But while we're working through the class, there's only two keyboard shortcuts that you're going to need to remember in VS code. For Windows that is Ctrl + Shift + P and Command + Shift + P on Mac for show command palette. This is like your Swiss Army knife.

[00:04:59]
It's going to bring up a window where you can type and see all of the lists of different commands that VS code offers. So if I bring that up in here, if I type Python, we'll see that all of these commands are available because I've opened a Python file and I have the Python extension activated.

[00:05:26]
The other command that you need to know is Ctrl + P on Windows or Command + P on Mac, that's going to bring up the Fast File Finder. So you can type in any file name that's contained in your project and it will quickly bring it up. If you know these two keyboard shortcuts, just write them down on a post it note somewhere near your desk, you're gonna be using them a lot.

[00:05:50]
And that's really kind of all that you need to know to get productive in Python in VS code. Now, when you work with files when you bring up a new Python file if you have not done that before, if you don't have a VS code installation, you might see some various pop ups.

[00:06:10]
So let's go ahead and create a new file. It's just new file or command + N, Ctrl + N and we're going to save this file as my VS code is now angry that I have renamed my directory. We're gonna call this hi.py. And if you're opening up this file for the first time, you might see some pop ups here asking you whether you want to install a linter.

[00:06:46]
In which case, you do, click Yes on that. And if there are any other pop ups for things that are missing or not installed, go ahead and click Yes and install them. There's also an optional extension that you might want to install called pylons. And it is an additional feature rich language server for VS code.

[00:07:11]
You don't need to have it but I find it pretty useful. So you wanna go ahead and install that. Make sure that when you have this Python file opened, that the correct interpreter is opened as well, in this case mine is the last one. So it looks like VS code didn't automatically pick it up maybe because of my directory weirdness.

[00:07:42]
So make sure that you go ahead and select the right interpreter. Here you'll see that mine is in .n/bin/python. And now I'll see here at the bottom that I have the correct version of Python. I'm just gonna bump that up a little bit. Now, what do we need to do to run Python code in VS code?

[00:08:10]
We've already gone ahead and created a new file. Let's copy this into our new file and run it. There are a few different options for running your Python files. I'll show you how to run them both from within VS code and from the terminal. So I pasted that right in there.

[00:08:37]
If you're already in VS code, you can open up the command palette with Command + Shift + P or Ctrl + Shift + P and select Python run Python file in terminal. And this has the advantage of VS code being smart and if your virtual environment isn't activated, it will automatically select it and you will see that it output right in the included terminal window right here at the bottom.

[00:09:06]
You can also, Navigate to this file in your terminal and make sure that your virtual environment is activated and run Python, hi.py. So this is outside of VS code if you're more comfortable in a separate terminal or on the command line.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now