This lesson will be about setting up your Python environment. It’s mainly intended for Windows, but any further lessons should work across platforms.
Downloading Python
First, go to python.org. You’ll see a big download button, which you could use, but I recommend downloading a slightly older version instead. The main reason is third-party libraries aren’t always updated to support newer versions, and this guide will (hopefully) cover those later on. Instead, you’ll want to download 3.11.9 (the version I’m working with). It’s pretty well supported. You can download it on this page. Click the Windows installer (64-bit) option to download it.
It will download an installer file. Run it, and check the Add python.exe to PATH option. Then click “Install now”. It will install, then you can exit the installer.
To verify the installation worked, open command prompt and run python --version
. Assuming it worked, you should see the version outputted.
Ways to write Python code
Python files (.py) are written in plain text, meaning you can use any text editor to write Python code. However, certain programs will be better than others for Python. Here are a few good options:
- Built in python IDLE: Simple, but not very powerful
- Sublime Text: Less simple, but more powerful, fast.
- Visual Studio Code: Very powerful, has lots of extensions, but is less lightweight than Sublime Text.
- Important: Visual Studio Code is not the same as Visual Studio. While Visual Studio Code is an advanced text editor for many languages, Visual Studio is a full IDE mainly used for C++ and .NET. You do not need Visual Studio to code Python, and will likely overwhelm yourself if you try to use it.
Personally, I like using Visual Studio Code, and that’s what I will be using and recommend for these tutorials.
Creating a python file
To create a new python file, you can follow these steps:
- Open the folder you want to put the python file in
- Some IDEs will put cache files in the same folder, so it’s often useful to put python files in a dedicated folder.
- Right click inside the folder, and create a new text document.
- If you haven’t already, you should turn on Show file extensions under View > Show > File Extensions (the exact path will differ on older versions of Windows)
- If you haven’t already, you should turn on Show file extensions under View > Show > File Extensions (the exact path will differ on older versions of Windows)
- When naming the file, make sure to change the extension from .txt to .py
- If it gives you a warning, click Yes.
- You can also open the folder in VSCode by right-clicking and choosing Open with Code (you may need to click Show more options first).
Running code in VSCode
To run a python file in VSCode, you’ll want to open the file in it, then use ctrl+F5. You should see a list of options, choose Python Debugger.
It will open a terminal session on the bottom of the screen. Not much will happen though, since our file is empty.
You can also run files outside of VSCode by double clicking a .py file. You’ll notice though, with an empty file (or even files with code), they will only open a window for a split second, then close it. This is normal, and I’ll explain why this happens in the next lesson. For now, just stick to running files in VSCode.