Installing and Executing Python on Visual Studio Code || Introduction to Programming

   

          I'm a big believer in open-source and material that is free to everyone. So, in the same sort of spirit of free material, I want to hopefully help those who are interested in Computer Science by teaching you what I learn. This may not be perfect as I will be teaching you what a learn from a student's perspective and not from a professionals. I have some basic knowledge already, but this is really going to break things down for a beginner, like me! Plus, this doubles as revision for me, but my main goal is to really help others. Also, if you have more experience than I do on anything that I post and want to correct me or give me advice, please feel free to! Anyway, let's get on to the tutorial. 

What to Install?
    For my particular course I needed to download Python 3.6.2 (www.python.org)  and Visual Studio Code (https://code.visualstudio.com) for my mathematics class as the goal of this class is to truly apply mathematics into programming instead of spending too much time on theory. Anyway, go over to both Python and Visual Studio Code and download both, however when installing Python make sure that you select "Add Python 3.6 to PATH". This will save you a small headache of fixing it, and allow you to open Python in your Command Prompt.

Help! I forgot to select add to PATH! 
    For those of you who have successfully installed Python and made sure to add Python to PATH, skip this section. However, for those of you who rushed my little lesson here, don't freak out, this can be solved in a few simple steps. Be warned, I only know how to help those who have Windows... I'm sorry! I am trying! 
Now for those of you on Windows, open up your Search Bar and type Python, without opening anything Right-Click on one of your Python Files (I choose Idle(Python 3.6.2 32-bit) and select open the location of the file. As you can see below, my system is in French, but it should be the same thing or worded similarly in any language.

What this step allows you to do is allow you to precisely locate where your file is on your computer. This will then permit you to create to manually create a PATH with the information you will retrieve. Next, you want to once again Right-Click on one of the files, then click on properties. This will then allow you to copy the Python to PATH.
In properties, you will notice a section marked as Start In, again my system is in French so it appears as "Démarrer Dans". Copy that text until Python. It should look something like this: C:\Users\Name\AppsData\Local\Programs\Python.

Now, on your computer search 'environment' and open Modify the Variables of the System Environment which can also be found in the Configuration Panel.

Once in the system environment, which should look like the image below, click on Variables of the Environment.

Congrats! You finally made it to the final step! I told you it was going to be a small headache! To finish you want to click on New at the bottom of the screen, which is under System Variables, and paste the C:\Users\Name\AppsData\Local\Programs\Python that you copied earlier under value of the variable. Viola! You are done! But, a little piece of advice I have IS ADD TO PATH WHILE INSTALLING TO AVOID ALL OF THIS! Seriously, make your life easier.

Testing Python to make sure it works
    To very quickly make sure you installed it correctly, open Command Prompt (which can be found when searching CMD in your Windows computer or Terminal for Macs), and type python.exe (on Mac it might be python3.6 but don't take my word on it. I forgot to note in down in class simply because I use a Windows and it didn't concern me. Sorry.). Once you have typed that and hit enter you should get a response like "Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information." If you got that perfect, you can type 2+2 and hit enter just to prove to you even more that it works. However, if you got an error, it has something to do with PATH, so I would recommend looking over the section right above or Google it. When you are done testing it out you can either close the Command Prompt or type exit() (Works for both Windows and Mac).

Installing Visual Studio Code
   From what I can remember there is nothing important you need to click on like when installing Python. This means you can easily install it like you would any other thing. Once you have successfully installed Visual Studio Code, launch it and you should see a Welcome Page. From this Welcome Page you should be able to "Add a Language" and in that Case you would install Python. Now, if you are someone who has never used Visual Code Studio before you may be wondering why we are once again installing Python. The Python you are downloading in the Visual Studio Code is actually an extension that will make writing code in Python much easier as it has features like text prediction, auto-indentation, color coding, etc, while as the Python you downloaded from the official Python website allows your computer to read and execute programs written in Python.
   Once you have successfully downloaded the Python extension in Visual Code, you can select 'File' and then 'New File'. Here if you couldn't guess is where you will be writing your code, and if you don't yet know how to write in Python, you can simply type print(2+3) to test it out in Visual Studio Code. Also, if you don't know Python I recommend checking out Codecademy  where they have a wonderful program to learn Python because as of me typing this, I do not have the knowledge to teach you yet.

Saving your files and executing them
There are different ways to save your files in both Mac and Windows, so I am going to quickly explain to you Mac since it's the quickest and then Windows.
  The simplest way to do it on Mac is to open Finder and Click on your Username (from what I can recall this is normally paired with a little icon of a house), and then add a Folder called Python. BAM, you're done. 
   For Windows, it has just a few more steps, but it is still very simple. Open File Explorer, click on This PC, then Windows (C:), Users, your username, and then add a folder named Python. BAM, you are also done. Your file should look like this:
     Any file you choose to save from Visual Studio Code written in Python, I recommend saving under that file you just created as NameOfYourProject.py. Always remember to save your files with .py at the end. 
     To execute your programs you have one of two options: the Command Prompt or the terminal built into Visual Studio Code. Which if you don't already see at the bottom of your screen when you write a file, go to View and then click on Integrated Terminal (Or something written similarly as this is a rough translation from French). It does not matter which one you choose to use since they do the exact same thing and are the exact same. However just because the display is larger, I will show you on the Command Prompt.
    Once you have your file saved correctly, open your Command Prompt or Integrated Terminal. Type dir on Windows or ls on Mac to get the Directory. Here you should be able to see your folder that you created earlier called Python. 

From here you are then going to type cd Python (be careful this is case sensitive), and I am also really hoping that it's the same for Macs. Now to execute the program you wrote (I wrote print(2+3) and if you choose to copy that code, leave a blank line after that just to be safe), type python.exe (Windows), the name of your project file (case sensitive), and hit enter. It should look like this:
One thing I noticed when typing in the Command Prompt is that is still ran the program without the need for python.exe. I'm not sure if that is because I don't truly need python.exe but that is just what I was taught and for the moment I am going to continue using it. 

You did it!
Congrats! You managed to install and execute Python on Visual Studio Code! Now you are free to explore whatever programs your heart desires to create. Go have fun, and go code!


I'd like to take the time to sincerely apologize to any Mac users trying to follow this. I do not have a Mac and have never owned one, but I did try to make it as clear as possible and hopefully I didn't mess up on anything on your parts or just in general. If you notice any mistakes or want to give your input on this please let me know by commenting.

Thank you so much! And I hope this helped someone out there!
















Comments

Popular Posts