Starting with python on linux

How To Open Python on Windows, Mac, Linux

Last updated on June 2, 2021

You’ll now learn how to open Python on Linux, Windows, and MacOS. First of all, you should know that there are two ways of using Python:

  1. Start an interactive shell, also called a REPL, short for read-evaluate-print-loop.
  2. Start a Python program that you stored in one or more files with the .py extension.

In this tutorial, we’ll start with the interactive shell because it’s ideal for exploring the language. But at some point, using the REPL won’t cut it anymore and you’ll have to start creating Python files.

If you installed Python on your local machine, you first need to start a terminal or command prompt before you can start the Python interactive shell. On all platforms, you should be able to start Python 3 with the command python3 (or sometimes just python ). Just be sure you are running Python 3, not 2, because some systems can have both versions installed.

How to open Python on Linux

On Linux, you first need to start a terminal. This can often be done with the shortcut ctrl + alt + T . Alternatively, you can search for the terminal program in your start menu. The name and where to find it differs from distribution to distribution. Once you have a terminal running, simple enter python3 to start the Python REPL:

How to open Python on Windows

On Windows, you can start Python from a terminal as well. E.g., to start PowerShell, simply hit the Windows key and start typing “PowerShell”. Start it and then enter python3 . If you don’t have PowerShell, you can use the ‘Command Prompt’ program instead.

How to open Python on Mac

On MacOS, search for a program called terminal. You can do so by pressing the command key (⌘) + space bar. This will open up the Spotlight searchbar, in which you start typing the word ‘terminal’.

Once you started the terminal, enter python3 to open the Python REPL.

How to use Python in the cloud (repl.it)

If you decided to use the repl.it cloud-based option, the Python interactive shell is at the right of the screen. You can simply click in the dark shell area and start typing:

About the author

Erik van Baaren

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He’s been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python!

Источник

2. Using Python on Unix platformsВ¶

2.1. Getting and installing the latest version of PythonВ¶

2.1.1. On LinuxВ¶

Python comes preinstalled on most Linux distributions, and is available as a package on all others. However there are certain features you might want to use that are not available on your distro’s package. You can easily compile the latest version of Python from source.

In the event that Python doesn’t come preinstalled and isn’t in the repositories as well, you can easily make packages for your own distro. Have a look at the following links:

for Debian users

for OpenSuse users

Читайте также:  Astra linux как подключить сетевой диск windows

for Fedora users

for Slackware users

2.1.2. On FreeBSD and OpenBSDВ¶

FreeBSD users, to add the package use:

OpenBSD users, to add the package use:

For example i386 users get the 2.5.1 version of Python using:

2.1.3. On OpenSolarisВ¶

You can get Python from OpenCSW. Various versions of Python are available and can be installed with e.g. pkgutil -i python27 .

2.2. Building PythonВ¶

If you want to compile CPython yourself, first thing you should do is get the source. You can download either the latest release’s source or just grab a fresh clone. (If you want to contribute patches, you will need a clone.)

The build process consists of the usual commands:

Configuration options and caveats for specific Unix platforms are extensively documented in the README.rst file in the root of the Python source tree.

make install can overwrite or masquerade the python3 binary. make altinstall is therefore recommended instead of make install since it only installs exec_prefix /bin/python version .

These are subject to difference depending on local installation conventions; prefix ( $ ) and exec_prefix ( $ ) are installation-dependent and should be interpreted as for GNU software; they may be the same.

For example, on most Linux systems, the default for both is /usr .

Recommended location of the interpreter.

prefix /lib/python version , exec_prefix /lib/python version

Recommended locations of the directories containing the standard modules.

prefix /include/python version , exec_prefix /include/python version

Recommended locations of the directories containing the include files needed for developing Python extensions and embedding the interpreter.

2.4. MiscellaneousВ¶

To easily use Python scripts on Unix, you need to make them executable, e.g. with

and put an appropriate Shebang line at the top of the script. A good choice is usually

which searches for the Python interpreter in the whole PATH . However, some Unices may not have the env command, so you may need to hardcode /usr/bin/python3 as the interpreter path.

To use shell commands in your Python scripts, look at the subprocess module.

2.5. Custom OpenSSLВ¶

To use your vendor’s OpenSSL configuration and system trust store, locate the directory with openssl.cnf file or symlink in /etc . On most distribution the file is either in /etc/ssl or /etc/pki/tls . The directory should also contain a cert.pem file and/or a certs directory.

Download, build, and install OpenSSL. Make sure you use install_sw and not install . The install_sw target does not override openssl.cnf .

Build Python with custom OpenSSL (see the configure –with-openssl and –with-openssl-rpath options)

Patch releases of OpenSSL have a backwards compatible ABI. You don’t need to recompile Python to update OpenSSL. It’s sufficient to replace the custom OpenSSL installation with a newer version.

Источник

Getting Started with Python Programming and Scripting in Linux – Part 1

It has been said (and often required by recruitment agencies) that system administrators need to be proficient in a scripting language. While most of us may be comfortable using Bash (or other shell of our choice) to run command-line scripts, a powerful language such as Python can add several benefits.

Learn Python Programming Scripting in Linux

To begin with, Python allows us to access the tools of the command-line environment and to make use of Object Oriented Programming features (more on this later in this article).

On top of it, learning Python can boost your career in the fields of desktop applications and data science.

Being so easy to learn, so vastly used, and having a plethora of ready-to-use modules (external files that contain Python statements), no wonder Python is the preferred language to teach programming to first-year computer science students in the United States.

In this 2-article series we will review the fundamentals of Python in hopes that you will find it useful as a springboard to get you started with programming and as a quick-reference guide afterwards. That said, let’s get started.

Python in Linux

Python versions 2.x and 3.x are usually available in most modern Linux distributions out of the box. You can enter a Python shell by typing python or python3 in your terminal emulator and exit with quit() :

Читайте также:  Видеоплеер с субтитрами для windows

Running Python Commands on Linux

If you want to discard Python 2.x and use 3.x instead when you type python, you can modify the corresponding symbolic links as follows:

Remove Python 2 and Use Python 3

By the way, it is important to note that although versions 2.x are still used, they are not actively maintained. For that reason, you may want to consider switching to 3.x as indicated above. Since there are some syntax differences between 2.x and 3.x, we will focus on the latter in this series.

Another way you can use Python in Linux is through the IDLE (the Python Integrated Development Environment), a graphical user interface for writing Python code. Before installing it, it is a good idea to perform a search to find out what are the versions available for your distribution:

Then, you can install it as follows:

Once installed, you will see the following screen after launching the IDLE. While it resembles the Python shell, you can do more with the IDLE than with the shell.

For example, you can:

1. open external files easily (File → Open).

Python Shell

2) copy (Ctrl + C) and paste (Ctrl + V) text, 3) find and replace text, 4) show possible completions (a feature known as Intellisense or Autocompletion in other IDEs), 5) change the font type and size, and much more.

On top of this, you can use the IDLE to create desktop applications.

Since we will not be developing a desktop application in this 2-article series, feel free to choose between the IDLE and the Python shell to follow the examples.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

Getting started¶

What you’ll need¶

A Python!¶

If you haven’t yet got python, the latest official installation packages can be found here:

Python 3 is preferable, being the newest version out!

On Windows, you’ll want to add Python to your PATH, so it can be found by other programs. With Python 3.5 or later, there should be and option to do this in the installer. Otherwise, you can navigate to your installation directory ( C:\Python34\ ), open the Tools , then Scripts folder, and run the win_add2path.py file by double clicking on it.

And a Code Editor¶

A code editor helps with reading and writing programming code. There are many around, and it is one of the most personal choices a programmer can make — Like a tennis-player choosing their racket, or a chef choosing their favourite knife. To start off with, you’ll just want a basic, easy-to-use one that doesn’t get in your way, but is still effective at writing python code. Here are some suggestions for those:

  • Atom: A new code editor available for Windows, Mac and Linux. It’s an open-source project developed by GitHub and is very easy to add functionality for, with its packages system.
  • Sublime Text: A great all around editor that’s easy to use. It’s Ctl+B shortcut lets you run the python file you’re working on straight away. Runs on Windows, Mac and Linux.
  • Geany: A simple editor that doesn’t aim to be too complicated. Available on Windows and Linux (you can probably find it in your package manager).
  • TextMate: One of the most famous code editors for Mac, it used to be a paid product but has since been open-sourced.
  • Gedit and Kate: if you run Linux using Gnome or KDE respectively, you might already have one of these two installed!
  • Komodo Edit: a sleak, free editor for Mac, Windows and Linux, based on the more powerful Komodo IDE.

If you’d like our recommendation, try out Sublime Text first.

Wordpad, TextEdit, Notepad, and Word are not suitable code editors.

Читайте также:  C windows system32 описание файлов

What is Python, exactly?¶

Ok, so python is this thing called a programming language. It takes text that you’ve written (usually referred to as code), turns it into instructions for your computer, and runs those instructions. We’ll be learning how to write code to do cool and useful stuff. No longer will you be bound to use others’ programs to do things with your computer — you can make your own!

Practically, Python is just another program on your computer. The first thing to learn is how to use and interact with it. There are in fact many ways to do this; the first one to learn is to interact with python’s interpreter, using your operating system’s (OS) console.

A console (or ‘terminal’, or ‘command prompt’) is a textual way to interact with your OS, just as the ‘desktop’, in conjunction with your mouse, is the graphical way to interact your system.

Opening a console on Mac OS X¶

OS X’s standard console is a program called Terminal. Open Terminal by navigating to Applications, then Utilities, then double-click the Terminal program. You can also easily search for it in the system search tool in the top right.

The command line Terminal is a tool for interacting with your computer. A window will open with a command line prompt message, something like this:

Opening a console on Linux¶

Different linux distributions (e.g Ubuntu, Fedora, Mint) may have different console programs, usually referred to as a terminal. The exact terminal you start up, and how, can depend on your distribution. On Ubuntu, you will likely want to open Gnome Terminal. It should present a prompt like this:

Opening a console on Windows¶

Window’s console is called the Command Prompt, named cmd. An easy way to get to it is by using the key combination Windows+R ( Windows meaning the windows logo button), which should open a Run dialog. Then type cmd and hit Enter or click Ok . You can also search for it from the start menu. It should look like:

Window’s Command Prompt is not quite as powerful as its counterparts on Linux and OS X, so you might like to start the Python Interpreter (see just below) directly, or using the IDLE program that Python comes with. You can find these in the Start menu.

Using Python¶

The python program that you have installed will by default act as something called an interpreter. An interpreter takes text commands and runs them as you enter them — very handy for trying things out.

Just type python at your console, hit Enter , and you should enter Python’s Interpreter.

To find out which version of python you’re running, instead type python -V in your console to tell you.

Interacting With Python¶

After Python opens, it will show you some contextual information similar to this:

The prompt >>> on the last line indicates that you are now in an interactive Python interpeter session, also called the “Python shell”. This is different from the normal terminal command prompt!

You can now enter some code for python to run. Try:

Press Enter and see what happens. After showing the results, Python will bring you back to the interactive prompt, where you could enter another command:

An extremely useful command is help() , which enters a help functionality to explore all the stuff python lets you do, right from the interpreter. Press q to close the help window and return to the Python prompt.

To leave the interactive shell and go back to the console (the system shell), press Ctrl-Z and then Enter on Windows, or Ctrl-D on OS X or Linux. Alternatively, you could also run the python command exit() !

Exercise¶

Just above we demonstrated entering a command to figure out some math. Try some math commands of your own! What operations does python know? Get it to tell you what 239 and 588 added together, and then squared is.

Solution¶

Here are some ways you might have got the answer:

Источник

Оцените статью