Python Series Part 1: Installing and Configuring Python 3

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
344
Reaction score
377
Credits
11,920
As of the writing of this article, the release of Python version 3.11 is the newest. Python is a programming language that you can use for writing programs.

I will cover many areas of the Python programming language, but there are many code editors you can use. You can use whichever editor you may feel comfortable, but the coding is the same.

Before we get into the programming language, we need to get Python and the editor installed and configured.

Python

On all Linux systems, you should be able to install Python from the main repositories. While writing these articles, I am using Ubuntu 22.04, but the versions should be similar.

To install Python, I open a Terminal and type sudo apt install -y python3. and then hit the TAB key for completion. In my case, the completion will put in a '1' for 'python3.1'. If I press TAB again two times, I should get a list of packages that start with 'python3.1'. Two entries that are noteworthy are 'python3.10' and 'python3.11'. These are the two most recent versions available in the Repository. So, type in another '1' so the command is:

Code:
sudo apt install -y python3.11

To verify that Python 3 exists now, use the command:

Code:
python3 --version

My result is 'Python 3.10.12'.

You will also want to install 'pythonpy' so you can execute single lines in the Terminal:

Code:
sudo apt install -y pythonpy

So now, you can execute the command py --version and get the same result.

If I should look at https://python.org/downloads, I can see that there is a release for Python 12. You could download the source code and install it, but the version in the repository should be a very stable version.

Editor - Visual Studio Code

As I mentioned before, there are many Integrated Development Environment (IDE) that you could use, such as Visual Studio Code. The IDE can help prompt you for the correct commands and show you the syntax of the command. An IDE is better than just a text editor.

For Visual Studio Code, go to https://code.visualstudio.com/download/. Here, select your Operating System (OS) and it should start the download.

Once the system completes the download, change directory to 'Downloads' with 'cd Downloads' and issue the command:

Code:
sudo dpkg -i code_1.85.1-1702462158_amd64.deb

The command should install the package on your system. Start 'Visual Studio Code' and go through the setup.

It should ask you about a theme.

The next option is to select 'Rich support for all your languages'. After selecting this option, a list of languages should appear on the left side. On my system, Python was the first choice. Click on Python and select 'Install'. You should now have support for the Python language in the editor.

NOTE: If you know more than one programming language, install all the language support options for all of them. Having one IDE for all your coding is better than and easier to learn one IDE than a few.

We need to set up the Python Interpreter as the default. In the bottom-left of the Visual Studio Code program is a cog that, when highlighted, says 'Manage'. Click on it and then select 'Command Palette'. At the top of the screen should be a search box. Type in the search box 'select: python interpreter' and press ENTER. You should see a box appear similar to Figure 1.

Figure 1.JPG

FIGURE 1

Select the line that is shown as 'Recommended'.

NOTE: If the Python option does not appear, then close Visual Studio. Make sure we installed Python and try opening Visual Studio and try again.

Close open panes in the Visual Studio Code app and then click on the 'Explorer' in Visual Studio, which is two papers near the top-left side. A pane should open up, since this is the first time. This is the default folder where the system stores your coding projects. Click on 'Open Folder' and select a directory of your choice. It should open your 'Home' folder and then here, I will create a folder named Python and select it. You can place the folder where you want, or give it a different name. After you configure this option, see the option in Figure 2 that asks about trusting the authors in the Home folder. Check the box and click on 'Yes, I trust the authors'.

Figure 2.JPG

FIGURE 2

Next to the 'PYTHON' menu item, click on the page with the plus sign, as shown in Figure 3. Make sure the help box pops up to show that the option is 'New File'.

Figure 3.JPG

FIGURE 3

It will then prompt you for a name. I will type in 'First-Program.py'. All Python programs must end with the 'py' extension so that Visual Studio knows to use the Python Interpreter.

NOTE: Keep in mind that we can use the Visual Studio Code program with multiple languages, so it is important to designate the Python programs.

In the right pane, see there is a Line 1. This is the first line of your code, so type in 'print'. Do not type in the single quotes. You should see a box pop up to show you the command possibilities for the letters as you type. After print, type in '("'. There should appear the ending double-quote and the ending parenthesis. In between the double quotes, type in 'Welcome to Linux.org'. So the line should be:

print ("Welcome to Linux.org")

Save the program with the shortcut key of 'CTRL+S' or 'File→Save' on the menu.

To execute the code, click on the sideways pointing triangle, like a 'Play' button, in the top right corner. You should see an output similar to Figure 4 at the bottom of the right pane.

Figure 4.JPG

FIGURE 4

The command, 'print', is printing the string between the double-quotes within the parenthesis.

If you wanted, you can open a Terminal, and then change into your Python folder and execute the program with 'python3 First-Program.py'. The output is the same as running it inside Visual Studio Code.

You can easily change the string and print something else.

Conclusion

Python is a programming language in Linux. You can also install Python on Windows and macOS, as well as Visual Studio Code.

Make sure you have the configuration completed before continuing on with learning the Python programming language.
 
Last edited by a moderator:



Staff online

Members online


Top