Configurar Visual Studio Code para ejecutar código Python y usar el depurador

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
We are going to see in this video how to use "Visual Studio Code" to program with Python. For that, first of course we will have to download this text editor, Visual Studio, from the web site code.visualstudio.com. We pick the appropriate download for the environment we are using. And of course we will also need Python. So we go to python.org and from "downloads" option we download python. We will generally download the newest version. If not, we can also choose another version, but normally we'll download from version 3 onwards, as version 2 has already been deprecated and has no more support. To verify that our Python installation is correct we can open a terminal and execute this command that will allow us to see that indeed we have Python 3 (in this case I have version 3.8.6) and verify that Python is correctly installed on the machine. Once we have both installed, we are going to open Visual Studio Code and we are going to, first, install an extension that even though it's not a "must", it'll be very useful. The extension is called "Code Runner". We can look for it here and then it will show everything it finds with that name. We need to select the first one that shows up. I already have it installed. What this does is it adds this button that allows to execute code. It's not the only way we can use to run code. That's why this extension is not crucial, but it's very useful. What we *will* need to execute our Python code is another extension, because Visual Studio Code is nothing more than a text editor with great coding tools added. So it's not like a mere notepad but it's not an IDE either. So we are going to give it extra functionality by adding extensions. In this case we're looking for the Python extension and it will also be the first one showing up here, which is the one made by Microsoft. So we are going to install this extension. We'll wait for it to finish... And with that we'll be able to execute Python code with Visual Studio Code. Now we need to start creating a project with Python. Let's create a small sample project. Here I already have a folder that I called "proyecto". That's the folder I'm going to open with Visual Studio Code. If we go here, in the explorer, we'll see that we don't have any open folders, so our next step is to open our project folder here. We can click on "open folder" or we can go to "file" and then "open folder". So here I have selected my folder called "proyecto" and I'm simply going to open it. Since this folder has nothing in it, I will only see this welcome page. I can close it and now I'm in the explorer view again. I can see that the open folder is now "proyecto". Then I will create a new file, either by going to "File" > "New file", or also it can be done by clicking here, on this icon, and I need to create a file with a .py extension, to identify it as a Python code file. So I could name it prueba.py, and with that we see that it automatically shows the Python icon, so it's asserting that we have the Python extension installed and that this is a Python file. We can close this, we can close this, because this extension for now will be enough. So we are going to type in any code, something small for test purposes only. And with this we are going to save. It can be done by pressing Ctrl+S in the keyboard like I just did, or we can go to "File" and "Save", which also saves the file for us. Also note that, as Python is already identified as the language, it highlights the syntax. Also, as we type, we have the auto-completion feature, so this extension helps us with all these things, not just to execute Python code. For example here, as "saludo" is a variable of the string type, it's telling us what type it is, it auto-detects that when hovering over it. And if I type "saludo" and I add a dot, it will suggest which methods I can use with a string. Which is indeed very useful. Now if we want to run this code, we can press this button and we'll automatically see that it gets detected. The only thing that it didn't know how to show was the opening exclamation mark (¡), but that doesn't mean our code can't be executed. Indeed, we see that it executed my prueba.py file with this Python command. There's another way we can use to execute the code if we do not have this extension with the "run" button. Right click and then choose this option: "Run Python file in terminal". Then this will also execute the code, only that this time it's using the default terminal that I have set. And a little perk is that this option also saves the file automatically, that is, I don't need to save before running. A third way would be to select the lines of code that I want to run. In this case I have only two, but I could select part of my code and press Shift+Enter, and we can see that it also ran the code, this time in the interactive mode. This would be equivalent to opening an interactive command line. Then we can type the instructions, so that's what it does. So this way if we have a long file with a lot of code, we could select part of it and run only those selected lines. Finally, we could also go here, to "View" > "Command Palette", and we are going to select "Python: start", and it shows these two options. We want the last one, and this will open a terminal in which we could type. And we have an interactive interpreter again. Now something else we can do is to "debug" code. For instance, suppose I want to add a break point here on this line. I can click anywhere on the line and press F9 and then this dot shows up. Or I could simply click here on this spot next to the code and this will toggle the break-point on and off. Now we are going to start the debugger by pressing F5, which will show us the options. There are some options that we can use to configure the debugger. For now, to keep it simple we are only going to use this "Python File". What it does is (here it explains what this does) is to debug the file currently active on the editor. Right now that's prueba.py. We select it and see that the debugger started and it's telling us where it stopped. This is obviously some very simple code, but even if we had more lines of code it would have stopped the execution on the line with the break-point. And this is showing us here, on the left, what are the variables we have (in this case, "saludo"), what data type they have, and well, with this we can analyze our code and run it step by step. The options to run the debugger are here. With this we would continue executing the code, we can go step by step, we can enter some call to a function or method, we can exit, we can restart everything and stop the debugger. Also, if we hover the mouse over it, it will give us information. For example, here "print" is telling us that it is a built-in function: that means print is built into the language. Or, if we go to "saludo" it tells us what value that variable has. Also the debugger allows us to play with these variables, meaning we can manipulate them. To do that we have to go to the debugging console and since we have a "saludo" variable that is a string, we can type, for example, saludo.split. And now we see what it did was, well, what the split method does: generate a list with each one of the words in "saludo". And this way we can run code that is not actually in our file but which can still be executed with the variables that the debugger has stored at this time. If I restart debugging, the execution starts again and stops again on the same line. If we want to stop the debugger and go back to our normal code, let's go here, "Stop", and the debugger just stopped and we can continue writing our code. If we no longer need the break-point, we can delete it like this. It's also important to know that, when we are coding with Python and we need to install packages that are not included in Python (they are external packages), it is a good practice not to install them at the machine level, but instead we should install them within something called "virtual environments". To do this we first need to have the virtual environments tool installed. So we are going to run this command. I already have it installed. This virtual environment is not limited to Visual Studio Code, on the other hand it's a Python tool that will be able to us, whatever IDE or editor we use. It's a good practise to always create a virtual environment, because then we can install the libraries, the packages that we need, and it will not affect other projects. Why is it useful? Because, for example, I might have a project that uses certain version of a package, and if I work for a long time on it that package will probably be updated; but maybe I don't want to use the updated version because it might break my code, or for whatever other reason I might not want to update it. But, at the same time, I want to start working on some other project where I do need to have an updated version. So, with virtual environments, each project can use a different version without interfering with each other: they are completely independent installations. For that reason we are going to create a virtual environment. And to do that, on my current directory, my project folder, I'm going to issue this command, which creates a virtual environment that in this case I've decided to call "my-venv", which would be short for "my virtual environment". So here we could use any name we want. To activate it, that is: to start working within that virtual environment, I'm going to run this other command. Then this is telling me that I'm now working within this virtual environment. Now everything I run will apply to this project only. So now we go back to the command palette, we type "Python: Select" and it shows us this: "Select interpreter". It will allow us to select the interpreter, and the option we need is the one with the virtual environment that we just created. This way we are going to work within this virtual environment and we'll be able to install everything we need. To make sure we are working in this virtual environment we can check down here where it tells us what Python version we are working with, and it shows the name we chose for our virtual environment. So, for example suppose we want to install "Flask" because we are going to work with Flask; we can run this command and it will start installing Flask, but only within my virtual environment. Sometimes it'll show this message, that's because there's an update available for "pip" and I don't have it installed, but it's not important now. It is simply letting us know we can install the newest version. But here it's saying that these packages have indeed been installed. So it installed Flask for us but only within this virtual environment. If we wanted to start a new project with another virtual environment, then we'd have to install Flask or whatever packages are required again. If we want to see what happened to our folder, our code, let's go here, to the explorer, and we see that now it has created two folders other than the .py file that I had created. One of them for the environment: this is where everything we are installing within this virtual environment will be stored. And also, the Visual Studio Code folder was created for us, which is the one that contains this file (created automatically as we add configurations). It's a JSON file where Visual Studio Code is storing our configuration options, which of course we can also modify manually. We could type something here, but we should know exactly what we are doing and why. So that's it with this video where we've learned how to code with Python in this widely used editor, Visual Studio Code. See you next time.
Info
Channel: Programación Desde Cero
Views: 190,388
Rating: undefined out of 5
Keywords: programación, programacion, programadores, programadoras, programar, python, visual studio code, debugger, depurador, ide, editor de codigo, entornos virtuales, venv, entorno virtual python
Id: eFThEXvuZaM
Channel Id: undefined
Length: 13min 55sec (835 seconds)
Published: Sat Mar 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.