Python Development Environment Setup

If you are looking for how to get started with Python development, you have come to the right place!

The first, and MOST important step to getting started with Python is getting setup. Actually, getting setup is important when learning any programming language. You need to have the proper tools installed and configured to help ensure your success!

Actually, there’s really no such thing as the perfect setup to guarantee your success. You can definitely take a few steps during the setup process that will at least help contribute to your success.

By the time you are done with this post, you will have a fully functional Python development environment AND you will write a basic application.

And I’ll even through in my 2 favorite tips to set up a killer Python development environment

Let’s get started!


 

Python Development Pieces

In order to successfully create a development environment to start writing Python applications, you are going to need 2 important pieces:

  1. Python
  2. A Code Editor

That’s all you honestly need to get started. As I mentioned before, I also want to show you my 2 favorite tips to enhance you Python development environment, but that will be the icing on the cake later.

Obviously in order to get started with Python development, you need to install Python. Installing Python on your computer will provide you with all the tools necessary to be able to write your own Python applications as well as run other applications or code snippets that you find on the internet.

A code editor is really just a slightly more powerful text editor. It is specifically designed to include “knowledge” about certain programming languages and help you with certain things like syntax highlighting based on your language and even Intellisense.

Intellisense is the way that certain code editors, as well as fully fledged Integrated Development Environments (IDEs), know the programming language you are using and can show you options of what types of valid commands and syntax you can use. They will also show you options of what you can type next and provide some help to you while learning a new language. This is very helpful when you are just getting started.

Let’s start your Python development journey by installing Python!


 

Installing Python

The first part of setting up your Python development environment is simply installing Python. And to be completely honest, it’s actually quite simple.

You begin by heading over to the

Python download page

It will look something like this:

python development

Towards the top of the screen you will see a button that says “Download Python xxxxx” where the “xxxxx” will be the latest version of Python. At the time I’m writing this post, the latest version is 3.7.1. This page is typically “smart enough” to know which operating system you are using and it will show you the version appropriate for your computer. If for some reason it isn’t, simply scroll down and select the appropriate package for your computer.

In this tutorial, we will use the latest and greatest version of Python.

But what about the version that comes with my computer????

If you are using a Mac or another Unix-based operating system, there is a good chance that there will be a version of Python that already came installed.¬† Typically it’s a version somewhere around 2.7 or 2.8.

While anything with a version of 2.5 or above is probably fine, it’s generally accepted in the Python community that if you are maintaining code that is still using version 2.x, it’s fine. But most new Python code will be written in the latest and great version. And right now, that’s 3.7.1.

So, to follow along, I highly recommend that you install the latest version on the Python downloads page and go with a 3.x version.

If you are familiar at all with the Python 2.x version, you will notice some syntactical differences with the 3.x version, so it will just be easier to use the latest version.

The Python installers for different languages will all vary slightly. For the most part, all you will need to do is click the “Next” or “Continue” buttons to go through the entire process. If you are provided with any options, simply leave the defaults and move on.

Before progressing to the next step of your Python development process, let’s verify your installation.


 

Verifying Your Installation

Once the installation process is done, it’s time to verify that everything is installed properly. For this, you are going to need to use whatever command prompt or terminal application you have installed on your computer.
 

Windows

If you are on a Windows machine, simply open your Command Prompt and type the following command:

python --version

The response for this command should be:

3.7.1

Or you will see whatever version you downloaded at the beginning of this post. Just make sure it is at least 3.7!

NOTE: If for some reason you get an error running this command, try closing your command prompt and re-opening it.
 

Mac and Linux

If you are following along with either Mac or Linux and use the command above, you may get a different result.

On my Mac, if I type the following command:

python --version

I get the following response:

2.7.9

Strange…

I know I installed the latest 3.7.1 version on my laptop. Why is it showing version 2.7.9????

When it comes to Python, version 2.x and 3.x are considered different software packages. Since they are different software packages, multiple versions can be installed on your computer at the same time. And when 2.x was installed, the executable command that it used was “python”.

In order for version 3.x to be installed on the same computer, it needs to have a different executable name. So, version 3.x uses the alias command “python3”.

If you slightly modify my previous command to the following:

python3 --version

I get this response:

3.7.1

 

Installing A Code Editor

In order to really start your Python development process, you need a quality code editor.

If you have seen any of my other posts, it probably comes as no surprise to you that I am going to recommend that you use Visual Studio Code.

Visual Studio Code is quickly becoming one of the most popular code editors around. It’s very flexible and offers support for lots of programming languages. And it is very well suited for Python development!

I already have another explanation of the Visual Studio Code installation process, so I won’t repeat it here. If you don’t have it installed and want to learn how check out the

Installing Visual Studio Code

section of my

C# Development: Getting Setup

blog post.

Go ahead…

I’ll wait.

Now that you have Visual Studio Code installed, as well as Python, you are just about ready to write Python code. But not quite. I have a couple very important additions that I highly recommend to all Python developers.


 

Installing Visual Studio Code Extensions

At this point, you could actually get started on your Python development journey. But… Let’s do a little work to set you up for success.

Visual Studio Code all by itself is a wonderful tool. Don’t get me wrong. But, in order to really take advantage of its power, it needs a little help.

Out of the box, Visual Studio Code has enough support to identify certain file types as belong to different programming languages. But beyond that, the rest is definitely up to you. And when you are trying to get started learning a new programming language, it helps to have a bit of a safety net.

That safety net is going to come in the shape of a couple extensions.

Extensions are simply additions to Visual Studio Code that provide more functionality than what comes with Visual Studio Code by default.
 

Python Extension

The first extension you NEED to install is the Python extension.

The Python extension provides so many wonderful tools that will help your Python development skills improve so much faster.

Open Visual Studio Code and select the last icon on the Activity Bar on the left. In the search bar on the top, simply type “python”.

The result will look something like this:

python development

Select the first option with the name of “Python”. In the main section of the interface, it will show a description of the extension. You want the one made by Microsoft that has the following description:

“Linting, Debugging (multi-threaded, remote), Intellisense, code formatting, refactoring, unit tests, snippets, and more.”

When you select it, you will see a green button that says “Install”.

Click that button.

Visual Studio Code will install that extension and the button will change to “Reload”.

Click the “Reload” button, Visual Studio Code will restart, and the Python development extension will be ready to use!
 

Code Runner Extension

This extension is a guilty pleasure of mine. Over the years, as I write more and more code that I run at the command line, I get tired or bouncing back and forth either between Visual Studio Code and a terminal application or between the Editor and Terminal panes within Visual Studio Code.

A few months back, I installed the Code Runner extension and never looked back.

This extension allows you to run your code in the Editor pane with a keyboard shortcut and displays the result in the Output pane at the bottom of Visual Studio Code. Such a simple concept, but it is magnificent.

Open your Extensions option in the Activity Bar again, this time searching for “code runner”. More than likely it will be the first option in the list:

python development

Once again, click the green “Install” button, followed by the “Reload” button. Visual Studio Code will restart and the Code Runner extension is ready!


 

Extension Configuration

Just installing these extensions is not enough to fully take advantage of them. They need a little bit of configuration, but it’s rather simple in the end.

This is where my favorite tips come into play!

Let’s start with configuring the Python extension.
 

Python Extension Configuration

When you install the Python extension, you get the really nice syntax highlighting and Intellisense for free. But you can still take it a step further.
 

Python Interpreter

The first thing you want to do is make sure you are using the correct version of the Python interpreter inside of Visual Studio Code. This will ensure that Visual Studio Code is using the proper version of Python when running your python code.

Begin by opening the Command Pallet with CMD-SHIFT-P (CTRL-SHIFT-P on Windows), and you should see this:

python development

Inside the search box at the top of the screen type the following:

Python: Select Interpreter

Select that option when it appears in the dropdown box.

You should now see options that look similar to this:

python development

Your options may be different depending on your operating system and the version(s) of Python have installed. Remember, we want to work with at least version 3.7 if possible. Select the latest version of Python that you can from the dropdown box.

NOTE: If you recently installed a newer version of Python and you don’t see it in this list, you may need to close Visual Studio Code, re-open it, and try again to see the latest version installed.
 

Python Linter

If I can give you one piece of advice when either learning a new language or trying to get better using a language, it would be to use a linter. A linter is a language tool that will analyze your code and determine whether or not you are using proper, or accepted as proper, syntax. This will help to teach you how to write cleanly structured code that will be more accepted amongst your peers as well as help you to learn to read other peoples code.

This is incredibly important to help you progress.

Similar to how you selected the Python interpreter, you are going to choose a linter.

Open the Command Pallet with CMD-SHIFT-P (CTRL-SHIFT-P on Windows) and type the following:

Python: Select Linter

Select that option from the dropdown box and you will see something similar to the following:

python development

You now see a list of the Python linters that you have installed.

NOTE: When you first get started with this, a dialog or two may appear telling you that you need to install a linter for your version of the Python interpreter. Go ahead and select the install option.

You are free to choose any linter that you want. If you don’t know about any of these, or don’t have a preference, I would select the pylint option.

The Pylint linter is probably one of the more popular Python linters out there. If it is good enough for the majority of people out there, it’s good enough for me.
 

Code Runner Extension Configuration

The final piece of configuration with the the Code Runner extension.

If you are running on a Windows machine and the “python –version” command returns the latest version you have installed (at least 3.7), you can skip this step. Or stick around so you can see how Code Runner is configured. It’s good for you too!

Let’s begin by opening the Command Pallet again, but this time we are going to open the Preferences Pane. Once the Command Pallet is open type the following:

Preferences: Open Settings

This will open a dialog that looks similar to this:

python development

In the “Search settings” dialog at the top of the screen, type the following:

Run Code config

Select the “Run Code configuration” towards the bottom of the list:

python development

In the right-hand pane, you should see an option that says “Code-runner: Executor Map”. Beneath that should be a link that says “Edit in settings.json”. Click that link.

You will now see a screen that displays the default settings for all the code “executors” that Code Runner has:

python development

Take a look at the “python” option in the “Default User Settings”. If you are running on a Mac or a Unix-based operating system, and you need to execute the command “python3” to use the latest version of python, you are going to need to change the “python” option from “python -u” to “python3”.

When you start to edit this, Visual Studio Code may tell you that you need to end the User Settings. At that point, it will probably open a second pane to the right, like the image above. If it does that, just make your change to the version on the right hand side. The right side is you local user settings that will override the defaults on the left.

Save your changes, close the settings, and you are done.

With the Code Runner extension installed and configured, when you are writing your Python code, all you need to do is use the CTRL-ALT-N shortcut to execute your code in the Visual Studio Code Terminal pane.

Let’s try it out.


 

Start Your Python Development

With everything installed and configured you are ready for your first Python development task.

The Hello World application!

I know these might not seem like exciting applications to write, but they are very important.

They show you the absolute basic code of a given language to actually do something, and it teaches you what it takes to actually run your code.

And who am I to spit in the face of tradition?!

Start by creating a new file. Right-click in the Explorer pane on the left and choosing “New File”. Give it a the name “hello_world.py”.

In the Code pane, type in the following line:

print "Hello World!"

and save your changes.

Once you do this, you should see some red squiggles underneath the print command. If you put you mouse over the red squiggles, you will see the following:

python development

Pay close attention to the first line that starts with “[pylint]”.

This complaint is courtesy of your friends neighborhood Python linter. It’s telling you this isn’t the proper way to use the print command.

The more accepted way to write it is as follows:

print("Hello World!")

Change your line to match the above and save the changes.

The red squiggles should magically disappear.

Now that you have your code looking nice. Let’s run it with the Code Runner extension.

To do this, simply use the “CTRL-ALT-N” keyboard shortcut. You should see something like the following:

python development

At the bottom, in the Output pane, you will see your “Hello World!” output!

Congratulations!!! You did it!!!


 

Summary

Hopefully through following this post, you now ready to start progressing in your journey to learn more about the Python programming language.

In order to get ready to truly start writing Python code you need to have 2 things installed:

  1. Python
  2. A Code Editor

In this post, you went through the process of installing the latest version of Python and installed my favorite code editor, Visual Studio Code.

Having Visual Studio Code is not enough to set yourself up for success. To help you write better Python code from the beginning, you also installed the Python and Code Runner extensions for Visual Studio. You also configured those extensions to work with your version of Python and setup a linter to help you write cleaner Python code.

Finally, you completed your first Python application, the ever popular Hello World.

From here, the sky is the limit!

Happy Coding!

 

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *