Beginner's-Guide : How to start with Python programming on Windows

installing python on windows

When I first decided to try Python months ago, the first obstacle I had to overcome was to figure out how to set it all on Windows. You install a Python on Windows, but then what. How do you create projects and run scripts? And that is the focus of this article. We will first install Python and then an editor from which we will make our Python code. Next, we will create a Python project and in it make a simple script and finally, we will examine how to install 3rd party packages.

Note: This article focuses only on how to start with the Python on Windows. If you want to learn more on how to code in Python, there are many books about this subject. The Head First series are always good, so check out the Head First Python. Another one to check it out is the Python Crash Course.

1.) Installing Python 3 on Windows

In this section, we will install Python 3 on Windows and after installation, we will test it if it was installed successfully.

  1. Visit the Python official website at https://www.python.org/, hover over "Download" button and select the Python button under "Download for Windows". In case you need some other platform or some other Python version, select it on the right side of the download hover window:

    python download from python official website

    Click image to enlarge
  2. Save the file in your computer and run it. The User Account Control (UAC) window will show up and ask you if you want to allow this app to make changes to your device. Verify, that the Publisher is "Python Software Foundation" and then click Yes.python install allow this app to make changes to your device
  3. The Python Install window should appear:

    python install - add python to path

    Click image to enlarge

    First, make sure you add a checkbox to "Add Python 3.7 to PATH" as shown above. With this option enabled, you can run python command anywhere from your computer.
    Next, click on "Install Now". This will install the default settings of Python, which should be fine in most cases. If you instead prefer custom installations, choose "Customize Installation".

    Installing should now proceed. Wait, until the setup progress is completed.

    python install progress

  4. When installation finishes, you should see "Setup was successful" message. In case you also see "Disable path length limit" option as shown below, click it, which will remove the Windows filesystem limitation of 260 characters.

    python install successful - disable path length limit

    Click image to enlarge

    If you don't see this option and you are running Windows 10, it would mean that your Windows already has this path limit disabled. If you are running an older version of Windows, you most likely won't see this option at all and you will be limited to the path length of 260 characters.

    Note: To learn more about the mentioned Windows filesystem limitation, check this article.
  5. The Python should now be installed in your Windows. We can check this by opening a Command Prompt. One simple way to open it is to enter cmd inside address bar of Windows Explorer:windows explorer running cmd

    Once inside the command prompt, type the command below, which should output the version installed:

    python --version
    

    cmd - python install test

Now that the Python is installed and working, let's install an editor from which we will make our code.

2.) Installing Visual Studio Code

We don't need any special editor to write code in Python. We could simply use Notepad for example, but a good code editor that supports Python will make us code easier and more efficient with features like IntelliSense and debugging. There is an IDE available just for Python named PyCharm, but my editor of choice is Visual Studio Code also known as VS Code, which is compared to PyCharm a more of a lightweight version of the IDE.

  1. To get the latest VS code, visit the official website at https://code.visualstudio.com/. For the latest stable build, simply click on the green "Download for Windows" button. For other platforms or if you want to try the newest prerelease features available in Insiders version, click on the "Other platforms" and "Insiders Edition" links.

    visual studio code - download from official website

    Click image to enlarge
  2. Save the file somewhere on your computer and run it. The first few steps of the installation process are pretty standard. We accept the license agreement, choose destination location, select Start Menu folder and then we should end up with the window shown below:

    visual studio code install - select additional tasks

    Click image to enlarge

    Here, make sure that the checkbox is selected for "Add to PATH" (it is by default). That way, the Visual Studio Code can run from any location.
    I would also suggest to add a checkbox for Add "Open with Code" action to Windows Explorer for both file and directory context menu. With these options, we can run VS Code directly by simply right-clicking on the file or folder inside the Windows Explorer.

    Next, the window will show listing all the options we selected in the previous steps. Click Install to proceed. After installation is complete, click "Finish".

Now, let's focus on creating our first project in Python using Visual Studio Code.

3.) Creating your first Python project

To create a new project, we create a folder on a computer and then we open this folder in the Visual Studio Code. We do that in VS Code by going to Open > Folder to select the folder, but there is also a way to open the project in VS Code directly from the File Explorer, which is my preferred option. Go to the folder we created in File Explorer and then right-click on it and select "Open with Code" as shown below:

windows explorer - open with code context menu

Note: This option shown above is only available to you if you selected Add "Open with Code" action to Windows Explorer option when installing Visual Studio Code.

If you don't have this option in File Explorer, we can still easily open the project from File Explorer. Go inside the project folder, then we click on address bar:

windows explorer - click address bar

And then enter cmd:

windows explorer running cmd

This will open the Command Prompt at the same path as in File Explorer. Now, we just need to do one more step. Inside Command Prompt enter:

code .

This will open Visual Studio Code in current folder.

command prompt - run visual studio code

Whichever way is used, the VS Code should now be opened inside the created folder:

visual studio code - opening project folder

Click image to enlarge

Now, let's create a simple Python program.

4.) Creating and running first Python script from the project

Now that we created a project, Let's create a simple Python script in it. The script will be simple, it will output into the terminal the "hello world" string. There are several ways to create a new file in VS Code, the one I prefer is to click on the + sign as shown below:

visual studio code - create a new file
After you clicked, you need to name the file:

visual studio code - name new file

In our case, we name it hello.py. Make sure, it ends with .py extension. That way, the VS Code knows, we will be coding in Python.

Since this is the first time we created a Python script in the Visual Studio Code, at bottom right corner you should see a suggestion to install a Python extension.

visual studio code - install python extension

Click on "Install" and wait for the extension to install. You should see it listed under the Extensions tab. We will need to reload the VS Code, so click the "Reload" button located near the extension:

visual studio code - python extension reload

Click image to enlarge

Now, the VS Code should suggest you to install a linter pylint.

visual studio code - install linter pylint

Linters highlight the problems in your source code, so they are very useful. Click on "Install". This time, the VS Code will install this inside the PowerShell terminal as shown here:

visual studio code - linter pylint installed

Wait for it to complete the installation. Now, that we are finished installing the recommended extensions, let's focus on our code.

Creating a simple "Hello World" program

Open the hello.py in VS Code and enter the following code:

print("-"*50)
print("Hello World")
print("-"*50)

The code above will output 3 lines. The first line will print - character 50 times. Next, the "hello world" string will be printed and then on the 3rd line, another 50 characters of - will be printed. Now, we need to save the changes. The Visual Studio Code also supports the Auto Save feature. With this feature, we don't need to save changes, we can simply focus on our code and the VS Code will save it for us automatically. To enable the Auto-Save feature, go to File > Auto Save, to toggle the feature on & off:

visual studio code - auto save toggle

Click image to enlarge

Now, let's run the script. Open the terminal in VS code if not yet opened by going to View > Terminal and type:

python hello.py

After running this command, the output of our script should show up in the terminal:

visual studio code - run python script in terminal

Note: The VS code terminal defaults to PowerShell for Windows 10 and Command Prompt for older Windows, so the terminal is nothing special. Running commands on a terminal in VS Code is the same as if we opened a separate PowerShell / Command Prompt window and ran the commands from there.

Congratulations, you just made a first Python program in Windows. Next, let's focus about the Python packages.

5.) Installing 3rd party packages and libraries

When you start coding in Python, sooner or later, you will need to install a 3rd party framework or a library. With Python, this is easily accomplished using a pip command line tool which is used to install and uninstall Python packages that are found on https://pypi.org/. The way we use them is to go to the terminal and type pip install module_name. For example, if we want to install a Python gaming library pygame, we would simply type:

pip install pygame

You should see the install progress inside the terminal, as shown here:

visual studio code terminal - installing pygame library

Click image to enlarge

To check, which packages are already installed, type:

pip freeze

visual studio code terminal - pip freeze command

And if you want to get general information about the package, such as homepage URL, short description , which package does it depend on,... then type pip show module_name. For example, if we again take pygame package as an example, we would type:

pip show pygame

visual studio code terminal - pip show

About Python virtual environments

When you start creating more and more projects, the versions of packages that these projects need will start to clash. For example, you created a project A and you installed a package for it. Now you create a project B which also needs same package, but it needs a different version than the one already available. Also remember that packages usually depend on other packages too, so eventually, you will come across this problem. To avoid these conflicts, we can make use of Python virtual environments which help us to isolate each project.

For more information about them and how to use them, check this article.

Conclusion

Python is a very versatile language, but to start using it on Windows, we need to install the necessary tools. In this article, we first installed Python itself, then focused on installing Visual Studio Code editor. Next, we created a new project, made a simple script inside the project and run it from the terminal. Finally, we examined the pip command line utility, which is used to install 3rd party packages and touched a bit about the Python environments.

2 Comments

  1. Rosie
    September 14, 2019
    • admin
      September 14, 2019

Write a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.