Home / News / Ready to Code Python in Visual Studio? Let’s Set It Up

Ready to Code Python in Visual Studio? Let’s Set It Up

253
Ready-to-Code-Python-in-Visual-Studio-Lets-Set-It-1

New to programming in Python and not sure where you should start? Visual Studio (VS) is a popular Integrated Developer Environments (or IDEs), and the first time I tried to install Python on my machine to work with VS, I was pulling my hair out. We’ll help you set it up with much less hair loss as a side effect.



Installing Python on Windows or macOS

Naturally, before you can start programming in Python, you’ll need to install it. You can get the package from the Python.org downloads page, letting you choose which operating system you’re using. If you’re using Linux, you can find Python using the system package manager.

Once you’ve run the installation file and everything seems to have been completed successfully, you can check your Python version to ensure it’s properly installed.

Checking Python on Windows

Hold down Win+R and then type “PowerShell” (without the quotes). Hit Enter, and the PowerShell window should pop up.

Once that’s done, you should enter the following command:

python --version

When you hit enter, the PowerShell window should display something like this:

Python 3.8.4

You can also use the command to get the same results:

 python -V 

When you install Python on Windows, you can access it using any IDE you choose.

Checking Python on macOS

Press the CMD+Space keys and type “Terminal” (without the quotes). Hit Enter, and the Terminal window will pop up.

Once you’ve opened the Terminal window, you can use either of these commands to check the Python version:

$ python3 --version

or

$ python3 -V

Once you’ve checked that you have a version of Python installed and gotten a version number in response, we can set up Visual Studio.

Installing Python on Linux

Depending on your distribution, Python may already be installed. We can check by entering the following command in our terminal and hitting Enter:

python3 --version

You should get an output that tells you the Python 3 version we’re running. If not, you’ll need to update your essential packages. On Debian and Ubuntu-based systems, you can do that using the terminal command:

sudo apt update

When that’s done, we’ll need to prep our system by running this command:

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev

You can install Python from the Python.org source files. This is the simplest method for me, personally. Use your terminal to navigate to where your downloaded files go. By default, it should be in your “Downloads” directory, which you can get access to by using:

cd ~/Downloads

The file we downloaded is a compressed file that needs to be extracted. We can do this by running:

tar -xJf Python-3.12.1.tar.xz

If the version you got is different, simply change the name of the file to the Python version you downloaded, and it should work. The default extraction folder will be named after the file, and we can get into it by using:

cd Python-3.12.1

Since this is a source, we’ll have to compile it to be able to use it on Linux. Luckily, there’s a handy configure file we can use, like this:

./configure --enable-optimizations

The configuration will create makefiles which we can then use to install Python like this:

sudo make install

This will take a while, so we can take a quick cookie-break while it builds and compiles our Python installation. When it’s done, we can check the version again:

python3 --version

This command told us that we were currently running Python 3.12.1. Success!

Installing Visual Studio for macOS and Windows

Visual Studio is a powerful code editor that has a massive amount of features. If you haven’t installed it yet, you’ll need to get the install package from Microsoft’s site. The Community version of VS is free, allowing anyone to get into development. Once you’ve downloaded the installation file, it’s a simple matter of running it.

When the installation window pops up, there will be a LOT of things we can choose to add to our installation. For now, we’ll just select Python since it’s all we’re interested in. Things may take some time to download, depending on your internet speed. When the installation is done, we should be ready to jump in.

Testing Python Support

Debug Window for Python

Once we’ve installed VS, we want to test whether it works with Python. To do this, we open VS, press “Continue Without Code, ” and then hit Alt+i.

In the empty window that follows, we type “2+2” (without the quotes). The result we get should be 4 (at the bottom of the window).

Installing Visual Studio for Linux

Sadly, VS is unavailable for Linux, but don’t despair. Linux users can instead install Visual Studio Code for their Python programming. VS Code is not the same as VS, but since VS is unavailable for Linux, VS Code is the next best thing.

For Debian or Ubuntu Users

Get the package using the wget command:

wget https://go.microsoft.com/fwlink/?LinkID=760868 -O vscode.deb

Then install the package:

sudo dpkg -i vscode.deb

For Fedora/Red Hat users:

First, get the package with this command:

wget https://go.microsoft.com/fwlink/?LinkID=760867 -O vscode.rpm

Once it’s downloaded, install the package:

sudo rpm -ivh vscode.rpm

And to run your Linux installation of VS Code, simply head to the nearest terminal and type:

 code 

Congratulations, your VS Code now runs on Linux!

Starting a Python Project From Scratch in VS

First thing first, if you’re using VS Code in Linux, you can skip ahead to the section at the bottom of this one.

The debug window is helpful in telling us we have a working Python install, but how do we start a project from scratch? That’s simple.

Open VS from your preferred startup icon.

Hit “Create New Project.”

Create New Project

In the window that follows, we select “Python Application” and hit “Next.”

New Python Application

We configure the project and hit “Create.”

Configure Project Details

We’ll be presented with an empty project to start our Python programming in. Congratulations on setting up Python to work with VS!

Starting with Visual Studio Code in Linux

The process is a little different but a lot faster since there are no hoops to jump through when creating a project. We start by typing:

codewhich will start VS Code and give you the “Welcome” screen. We’ll select the “New File” entry:

VS Code in Linux

We’ll enter a filename (like HelloWorld.py), and voila – we’re already inside the IDE. Once it detects a *.py filename ending, VS Code will prompt you to install the Python Intellisense plugin. You should since it makes your coding a lot easier by automating a lot of the process. And that’s it! You now have a fully-featured IDE set up to run in Linux.

Keep in mind that Visual Studio and Visual Studio Code are different IDEs. While many things are similar they are not the same.

Other IDEs to Consider

Visual Studio is a heavyweight IDE, but others are much simpler to use. Windows has a very simplistic version of Python suitable for beginners that you can get from the Store page. It’s ideal for teaching the basics, but if you want to actually do any actual coding, you’ll need a proper IDE.

Most professional Python developers opt for PyCharm, which is a dedicated Python IDE. Other developers who are used to Visual Studio may look at VS Code (we installed it on Linux here), which offers many of the same features, but in a more lightweight package. There are more IDEs than you can shake a stick at, and finding the one that suits you best is a matter of trial and error.

Comments