How to Set Up Python in Visual Studio Code on Mac | VSCode Python Development Basics On MacOS (2024)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video I'm going to show you how you can set up your python development environment in your Visual Studio code editor on Mac operating system so the prerequisite of setting up the python environment on Visual Studio code is obviously you should have the python installed on your Mac operating system so if you don't have the python installed you should install it if you don't know how to install python on Mac I have already created a video about it and the second requirement is you should have Visual Studio code installed so I'm assuming you have already installed the visual studio code editor if you haven't and if you don't know how to set up visual studio code on Mac I have also created the video about it now once you have Python and visual studio code installed let's start with the setting up of python on Visual Studio code so we are going to start with the terminal so I'm going to open the spotlight and here I'm going to search for the terminal and then let's say I want to create a python project in some directory on my Mac operating system so I have all these directories and I want to create the python project in my documents folder so I'm going to CD into my documents folder and here I'm going to create one more directory and and I'm going to name it a sample so to create a directory you can uh give this command which is mkdir and the name of the directory which is sample right and when I press enter it's going to create this sample directory you can see this directory is created now I'm going to CD into uh this sample directory so let me CD into the sample directory and here I can type code space Dot and then press enter which is going to to open the sample directory in Visual Studio code now this is the command line way of opening the sample directory in your Visual Studio code if you don't want to do this the other way is simple you just open your Visual Studio code and then click on open here and then just navigate to the folder where your python project is there or where you want to create your python projects just select any fold folder where you want to save your python files and then click on open and it's also going to open the folder in which you want to create your python project now here to create a python script I can click on uh this uh plus button which says new file or I can click on file and then click on new file option so let's click on the new file option and then I'm going to name my file as let's say test. py file and and then press enter as soon as I create a test.py file Visual Studio code is going to recognize that I want to create a python script and it gives me this kind of popup which says do you want to install recommended python extension from Microsoft for the Python language so visual studio code provides different kind of extensions for different kind of programming languages you can directly click on the install button which is going to install the extension or what you can do is you can uh click on the extensions section here and then search for python here when you search for python you will see the most popular extension here is for python which is from Microsoft itself and it has been downloaded around 106 million times so it's the most popular and the official extension for python on Visual Studio code we are going to click on uh this install button here which is going to install this extension on Visual Studio code so just wait for a few seconds and in sometime this uh python extension will be installed once the python extension is installed you will see the disable button or the uninstall button so if you want to uh disable the python extension just click on disable if you want to uninstall this extension totally you can press on the uninstall button let me close this window and then close this welcome window also and you can see you also have other popular python extensions like python indent python extension pack from the other developers and you can see all these python extension so if you want you can install these extensions later so let me close this and then go to the Explorer section here now in this uh python file let me just copy and paste a very simple python script so what uh this script does is we have defined two variables num one and num two and we have assigned the value of five and 10 to both both of them and then we are just adding these two uh variables and assigning it to the third variable which is called sum result and we are printing the value of uh the num one num two and the sum result result here so this is a very simple python script once you uh write your python script in the python file to run your python script you have few options one is the obvious one which is this one where you will see this uh run button for your python file when you click on this drop down menu you have other options like uh run python file in dedicated terminal and debug python file so you can run your python file you can run your python uh file in a dedicated terminal or you can just debug your python script using this option similar kind of option you will find here when you click on run here you can uh start the debugging of your python script you can run your python script without debugging and all the other options are available here if you right click on your python script you can also see other options to run your python script for example when you hover over the Run python button you have the option to uh run the python in the terminal and run the python in the uh python terminal also you will see the python interpreter at the bottom so here at the bottom of your Visual Studio code editor you can see if you have installed python then you will see which kind of interpreter you can use for your python script so so I have installed this Python 3.2.1 and visual studio recog recognizes this python path and it shows me this python path here at the bottom now if you have installed multiple versions of python for example you have U Anaconda version of python or you have different versions of python you can choose between those versions using this selection so you can see I have 3.9.5 version and I have 3.2.1 version so I can use either one of them and whenever I choose a different version I will be able to see the version right so you can switch those python versions using the switch here also when you press command shift p on your Visual Studio code it's going to open this kind of pallet and here when you type python you will be able to see different kind of uh commands so you can see create terminal create environment configure tests launch tensor board so all these options are available for running your Python scripts right so these are some of the options you can use later now if you want to open a new terminal you can click on this terminal option and then click on new terminal and it's going to open a new terminal here on Mac OS if you using the newest version of Mac the default terminal is zsh but if you want to open separate terminal let's say bash or uh the JavaScript debug terminal you will be able to see different type of terminal which are supported by your Mac operating system here so let's say I want to open the bash terminal I can click on bash here if I want to open uh zsh terminal I can click on zsh here and all the terminals I can switch between those terminals using this uh menu here right now let's run our code so I'm going to just click on this run button which is going to run our script and it's going to print the value of uh the numbers you can see the output is printed here which is the sum of five and 10 is 15 which is printed using this print line now if you want to debug your code you can set the breakpoint at different uh lines so I'm going to set the breakpoint at line number two line number five line number eight and let's say line number number 11 so if I want to debug my code I can either click on this Arrow here and then I can click on this option which says debug python file or I can press on uh the Run button and then I'm I can just click on uh the start debugging button so I'm going to click on start debugging and then I can choose the debugger in my case so you can see I have I have different kind of debuggers here python file module remote attach all these kind of options I'm going to choose the first one which is python files because I want to debug the python file once I click on the debug option you will be able to see that my interface for visual studio code changes and my um highlighted area here is now this debug section right here on the top you will see different buttons for continue step over step into step out and I can restart the debugging and I can stop the debugging using this red button now when I start the debugging my program execution stops at the first break point also I can see the list of all the breakpoints here so you can see on line number two line number five 8 and 11 I have the breakpoint I can toggle all these breakpoint by clicking on the toggle active breakpoints which is going to toggle all the breakpoints once again when I click on toggle it's going to enable the break points I can also disable those break points from here or directly in the file itself now when I click on step over my program execution stops at the second breakpoint and you can see the assignment is done for number one one so I can see num one is assigned the value of five but still we are on the line number five so the execution on this line haven't uh happened so I don't see the num two is equal to 10 so when I step over once again you will see num two is assigned the value of 10 I can also see the global uh variables right now so you can see all the Go Global variables here I can also see the special variables which are some internal uh variables in Python script if I press on continue then my program execution will stop at the next uh break point here now I can see the sum result is equal to 15 so you will be able to see all the uh breakpoint value and the uh variable values in the variables section also if you want to stop the debugging just click on this stop debugging or if you want to start just click on the restart so let me click on the restart and the debugging will start from the starting point of the breakpoint right I can close the debugging like this also now if you want to create a launch. Json file for your debugging so right now my debugging is done by the visual studio code but let's say if you want to share this debugging environment with some other developer and if you are using the Version Control then you can commit this launch. Json file and then other developer can pull the git uh repository and then use the same configuration so if I click on create a launch. Json file it's going to create a launch. Json file select python file here and once I click on launch. Json file you can see there is a folder which is vs code which is created for me and then I have the launch. Json file where the configuration related to my debugging is there and it can be used later by the other developers once I push my changes to a git repository now let's say you want to create a script which have some kind of external python package dependency let's see how we can uh run that also so let me just create one more script here so let me click on uh new file and then I'm going to uh just type test 2. py file okay and here I'm going to just uh write a very simple code which uses an external package which is called bcrypt okay so this is an external package and when you hover over bcrypt it says import bcrypt could not be resolved because we need to install the bcrypt package using pip then only this script will run properly right so let me open a new uh terminal here so when I type Python 3 space the name of the file which is test 2. py and then run this uh script it's going to complain that this import is not available so how we can solve this problem so let me clear the terminal and then first of all let's create a virtual en for our project to create the virtual en just write Python 3 space hyphen M VMV and then the name of your environment Let's uh name it uh and itself and then I'm going to press enter now once my virtual environment is created I can also see this directory here with the name en andv and then I have this bin directory and I have this activate file here so to activate my virtual environment I can give this command on Mac OS which is source and then the name of your EnV directory which is EnV in my case then I have this bin so/ bin SL activate so basically I need to call this script to activate my virtual end so write Source space uh this command and most probably you will also require the Pudo Command right so just write Pudo space Source space n slash bin slash activate and then press enter it says uh pseudo Source command not found let's try the source command without pseudo and then press enter and you can see the environment is activated so pseudo is not required I was expecting that it might fail due to the permissions but uh pseudo is not required so just run this command without pseudo and and once your virtual en is activated you will see this en in front of your command line okay this means that your virtual environment is activated all this creation and activation of virtual environment we have uh done so that we can install the bcrypt uh package for this environment and I don't want it to install bcrypt globally right so to install the bcrypt package just just write pip command on your Mac OS most probably if you have installed Python 3 then the command name for pip will be pip 3 so just try pip or pip 3 here whatever works for you then install and then the name of the package which is bcrypt in my case okay when I press enter it's going to install this bcrypt pip package and and once this package is installed you can see this squiggly line on top of uh this bcrypt package is gone and you can see the installation is also successful now I can just run my uh script let me clear the terminal and then I can run the script by using Python 3 and the name of the file which is test 2. py and then press enter and it's going to run my script which have the external pip package so I have already installed the package and and it shows me the output here so this is how you can create the virtual environment also on your Mac operating system using visual studio code also once I'm done with the virtual environment I can also disable it by giving this command which is deactivate and then press enter and I'm out of my virtual environment right so I think that's it for this video this is how you can set up the python development environment with your Visual Studio code editor on your Mac operating system I hope you've enjoyed this video and I will see you in the next video
Info
Channel: ProgrammingKnowledge
Views: 14,784
Rating: undefined out of 5
Keywords: How do I set up VS Code in Python Mac?, How do I run a Python program in Visual Studio for Mac?, How to set Python path in Visual Studio Code Mac? How do I get Python setup on my Mac?, How to install Python in terminal Mac?, How do I start Python in Mac terminal?, How do I know if Python is installed on my Mac?, Is Python automatically installed on Mac?, How do I install Python 3 on my Mac?, How do I install and set up Python?, How do I get Python on terminal?
Id: mtCqZZ-7jfg
Channel Id: undefined
Length: 18min 36sec (1116 seconds)
Published: Fri Jan 19 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.