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.
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.
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.
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:
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:
And then enter 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:
This will open Visual Studio Code in current folder.
Whichever way is used, the VS Code should now be opened inside the created folder:
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:
After you clicked, you need to name the 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.
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:
Now, the VS Code should suggest you to install a 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:
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
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:
Now, let's run the script. Open the terminal in VS code if not yet opened by going to View > Terminal and type:
After running this command, the output of our script should show up in the terminal:
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:
To check, which packages are already installed, type:
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
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.
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.