💾 Archived View for dece.space › docs › tech › using-python-programs.gmi captured on 2023-06-16 at 16:20:00. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-01-29)

-=-=-=-=-=-=-

Using Python programs

Last update: 2021-08-18.

You may have been sent here because you were trying to install a program using Python, and you are unsure about how to proceed. Hopefully this document will clear a few things up!

Please note this document is aimed at people who may not be familiar with programming or Python. Minimal experience with a terminal is useful here: what a terminal is and how to navigate between directories should be enough!

What is Python?

Python is a programming language, so there are programs written in Python, just like there are programs written in C, Java, etc. Programmers write the source code of the programs.

You may be familiar with programs as “binary” files, i.e. they are not easily readable by a human, or through the `.exe` file extension on Windows. These binary files have been compiled from their source code, using a program named a compiler. When you want to use these programs, you execute these binary files, but you do not manipulate the source code directly. One advantage is that you often do not need an external program to execute yours, so it is easier to share it with other people. Languages using a compiler to produce a binary executable file are called compiled languages.

Examples of compiled languages: C, C++, Go, Rust.

Python is part of different type of programming languages: interpreted languages. Python programs are distributed more or less directly as source code. This means that to run them, you need a Python interpreter installed on your system; you can't just double click on Python source code to execute it. In the case of Python, there are several interpreters available but the most commonly used is called CPython.

Examples of interpreted languages: Python, Javascript, Ruby.

Are there different versions of Python? Is it important for me?

Good question! You may have seen discussions about that, or the software you want to run requires a specific version of Python. Long story short, there are two major versions of Python: Python 2 and Python 3. They are not quite compatible, which means programs written in Python 2 may break on Python 3, and vice versa.

As I'm writing this, Python 2 is not supported anymore by the Python Software Foundation, so you should find less and less software using it. For this reason, in the rest of this document I assume you want to use a Python 3 program, but if you need Python 2 you can most of the time strip the “3” out of the command names.

You also may want to check the minor version of Python required for the software you wish to run. If the developer tells you that it requires Python 3.7 and you have Python 3.5 installed, the software may run into issues, and you should seek support.

OK, how do I check if I have Python?

A simple way to check is accessing the application list of your system: that may be the start menu on Windows, the activities panel on Ubuntu or your Applications list on macOS. Type “python” in there: does something show up? A yellow and blue logo? That's it!

You can also check if Python is available from a terminal. Type “python3 -V” (uppercase v) in there, and you will get the installed Python 3 version if it is installed.

Looks like I don't have it installed…

Well fortunately that's easy to do!

On Linux, you don't usually wander on the Web to download software: you use your package manager. If you don't know what a package manager is, how yours is named or how to use it, take some time to clear these questions up. Then, on the vast majority of Linux distributions, the Python 3 package is named “python3”, whereas the old Python 2 is simply “python”.

For other systems you can check those links:

Install Python 3 on Windows

Install Python 3 on macOS

OK I have Python now. How do I run my program?

There are a lot of possibilities here. The developer of the program you want to run may have left you some instructions and they usually fall in one of the following categories.

“pip install _____”

There is a big repository of Python software named PyPI (Python Package Index), and it's common for Python developers to push their software on that repository to share them. “pip” is a command-line tool that can install Python packages from PyPI to your system, so you may try to run it in your terminal…

Before running the command though, note that there are a few traps here:

Pip by default installs packages for your whole system, like most package managers. Unless you're running as a privileged user, which you should not do, this means that you have two choices:

Once it is installed, you can use pip to check what Python packages are currently installed on your system with `pip3 list`.

“easy_install _____”

Less common today, easy_install is an old way to install a Python package without relying on PyPI. This command may be used with a file that the developer provides you separately from the source code, usually a Python “wheel” with a `.whl` extension. It also can be used on a setup file, in a copy of the source code.

Like pip, easy_install may or may not be installed along with Python, but you can verify it in your terminal: `easy_install`. If the command is not found, you have to install the “setuptools” Python package.

“apt install _____” or “apt-get…” or “yum…” or “pacman…”

These commands are all different package managers for different Linux distributions: apt on Debian-based systems, yum on Red-hat-like systems, pacman on Arch Linux. This means the developer or someone else already made a package for your distribution, so you can install it like you would install any other software on your Linux system.

Note here that distribution packages are different from Python packages. The former usually provides the best integration with the system as it can rely on more tools to handle the specificities of your system and work with them. The latter requires less effort from the developer, can be used on any system instead of some Linux distribution, so software provided as Python packages tend to be more “bleeding-edge”: newer but potentially less stable.

“pipx install _____”

Compared to other methods, pipx is a more recent tool used to solve exactly our problem: install an executable Python program (not a library), isolated from the system to not cause issues, ready to be run. It is supported by the Python Packaging Authority but is rarely installed on systems so you will have to follow their instructions to install it. Read carefully what it tells you during the installation process so you won't have issues to find your installed programs later. I recommend using it when possible!

pipx

I can't find any instructions, but there are whl files provided…

Check out the easy_install section above.

I can't find anything!

When you want to use some project you found on a popular code-sharing platform, developers sometimes forget to put any instructions in a readme file and expect users to sort it out on their own. This is going to be a bit harder to use, but if you are willing to put some effort and research, you might succeed!

First you need to get the source code on your machine. If it is available on a Git repository (or any other VCS platform), you usually can either clone it or download an archive containing the source code.

Then we will try to install the software on your system. To install a Python package using setup files, you will need to have the “setuptools” package installed. Note that these commands do not require privileged permissions and will install the software only for you on most systems. If you want to install them system-wide on a Linux distribution, you will have to prefix `sudo ` before the commands and strip the `--user` option.

If there is a `setup.py` file you can try this command:

python3 setup.py install --user

If there is a `setup.cfg` file but no `setup.py` you can try this command:

python3 -c "from setuptools import setup; setup()" install --user

If there is no such file, the author has not prepared anything for packaging, and the simplest thing to do is to run the software from the source code itself. The Python source files have a py extension. Is there such file named like the project, or named “main” or even “__main__”? You can try running these with `python3 <replace this with the Python file>`.

That does not work / I'm still wondering about something…

Sadly, lots of things can go wrong. The Python software distribution ecosystem is complex, sometimes even for Python developers themselves! Get in touch, I'll be happy to help :)