Setting up Visual Studio Code for Python.
A brief guide to get started with Python using Microsoft’s Visual Studio Code.
What you need to download?
To test that our installation is successful, open the terminal & enter the following commands:
- Python
python -V
- Microsoft Visual Studio Code
Create a directory & open terminal in that directory. Now, enter the following command. As you do that, Visual Studio Code should open with the directory as the workspace directory.
code .
Now, in case, you get error while executing the above commands, no worries. The errors either indicate that environment variables are not set for the OS or there maybe a typo in the commands.
Setting Environment Variables
- Go to “system properties”, locate & open “Advanced System Settings”.
2. Click on “Environment Variables”, locate “path” under “System Variables” & click “edit”.
3. Now, locate the Python installation directory & copy the path for two folders. Assuming you’ve not changed the default installation directory during setup, they can be located as follows:
• C:\Pythonxx
• C:\Pythonxx\Scriptsxx - Python version
4. Click new & paste each of the path seperately.
Don’t yet close the window, we have still got to set the path for Visual Studio Code as well.
5. Locate the bin folder in the installation directory of Visual Studio Code.
6. Copy the path & paste it the same way you did for Python.
7. Click “ok”, “ok”, “ok” to complete the setup of environment variables.
Again open up the terminal & test the same commands mentioned above. Now, you should be pretty good to proceed.
Setting Visual Studio Code
On clicking the extensions icon & searching for Python in the searchbox, will present you with lot of results. As of now, we don’t need all of them.
As you see in the above image, there is an Python extension from Microsoft. Click on Install. That is all we would need to get started. You can further explore various extensions available.
Writing Hello World
Everything being setup, you’re ready to write some code.
- Create a new file inside Visual Studio Code & name it as “hello.py”.
- Copy the following code in that file & save it.
# Hello worlddef say_hello(): print('Hello, world!')if __name__ == "__main__": say_hello()
3. Right click anywhere inside the file & select, “Run Python File in Terminal”.
This should open up the integrated terminal & display the output, if there weren’t any errors.
And, that’s it. You’ve successfully created the developer environment required for programming in Python using Visual Studio Code.
As a bonus, here are some resources from where you can learn Python.
- GeeksForGeeks.
- Learn Python | Full Course by FreeCodeCamp.
- Learn Python by Building 5 Games By FreeCodeCamp.