Introduction to Python for Delphi programmers

Why Delphi programmer needs Python at all? The main reason is: Python ecosystem is much bigger and much more active than Delphi ecosystem; there are many useful and actively developing projects in Python, much more than you can find in Delphi. Currently I am dabbling in a nice MkDocs documentation generator and planning to use it for documenting my project.

The first thing you need to understand about Python is project isolation. In Delphi you can add units, packages, etc to a project and it does not affect other projects; the Delphi projects are well isolated. If you simply install Python and start developing projects, you quickly discover that there is no project isolation at all; if you add python package for one project, the package becomes available for all projects. It is quite stunning when you encounter it for the first time, but there is a solution: you need a separate Python installation for each project. These separate installations are called environments. The idea is: you have one global Python installation with the sole purpose of creating environments; you never use the global installation for developing projects. When you start a new project you create a new environment, and when later on you add packages to the environment it does not affect global installation or other environments.

There are several ways to create environments, the way I use is Conda package manager.

I am using Python on Linux Mint, and Linux Mint already has Python installed (on Windows you probably need to install Python first). But this Python belongs to the system; the system installed it for its own purposes, and trying to modify the system Python is bad idea. Good news are: Python is Conda installation requirement, and you have it.

Go to Miniconda download page and download Miniconda for your system. There are two installer versions, based on Python 2 and Python 3 – use the one based on Python 3; it does not matter much; by default Python 3 version creates Python 3 environments. Don’t install Anaconda – if you want to play with Anaconda install it later into environment.

Open Terminal window, go to the download folder and run the downloaded installer; accept the default settings and answer “yes” to the installer questions. After the installation is completed close the Terminal window and open it again. Now you have Python installed in your home folder; to check it run which python command:

  serg@TravelMate ~ $ which python
  /home/serg/miniconda3/bin/python

This is global Python installation that will be used to create environments.

To play with MkDocs I’ve created environment named mkdocs:

  conda create -n mkdocs pip

pip is Python package manager that will be included in the new environment. Conda documentation may say that you need not pip because you can install everything using Conda itself; I believe this is too good to be true, and prefer to have pip in any environment, and it is better to install pip right in the environment create command.

Now we need to activate the newly created environment; after the activation check that we have different python installed in the environment:

  serg@TravelMate ~ $ source activate mkdocs
  (mkdocs) serg@TravelMate ~ $ which python
  /home/serg/miniconda3/envs/mkdocs/bin/python

The next step is to upgrade pip in the environment:

  pip install --upgrade pip

and finally install MkDocs package into the environment:

  pip install mkdocs

Check that mkdocs is installed:

  (mkdocs) serg@TravelMate ~ $ mkdocs --version
  mkdocs, version 1.0.2 from /home/serg/miniconda3/envs/mkdocs/lib/python3.7/site-packages/mkdocs (Python 3.7)

If you are not going to do more for now, deactivate the environment

  (mkdocs) serg@TravelMate ~ $ source deactivate
  serg@TravelMate ~ $ 

or just close the Terminal window.

3 thoughts on “Introduction to Python for Delphi programmers

  1. That’s the -extremely – naive way of saying that Delphi (or object pascal in general) lacks bindings.
    Python in itself relies on bindings written in low-level languages, including some in Object Pascal.
    Don’t confuse people. Python is glue, not substance. And I like it a lot…mind you..

    • “Python in itself relies on bindings written in low-level languages, including some in Object Pascal….Python is glue, not substance.”

      While Python is perhaps the ultimate glue language, it’s categorically untrue to suggest that that’s its only use. There are over 150,000 python modules available from the python package manager. From web frameworks – micro e.g. CherryPy to full e.g. Django, to a math/statistics/data mining stack of numpy, scipy, matplotlib and pandas, financial analysis libraries, machine learning, ORMs such as SQLAlchemy… the list goes on and on. It’s only on top of that massive basis that you add the bindings available for C, C++, D, R, Fortran, Matlab, Octave, C#, Java, PHP, Perl, Javascript, Node.js, lua, etc. And then on top of all of that you have the fact that it’s embedded or callable from programs such as spreadsheets (LibreOffice, Excel with plugin), databases (PostgreSQL, SQL Server), ebook managers (Calibre), version control systems (Mercurial), image editing (GIMP), 3D modeling (Blender), documentation generators (Sphinx, etc.), static website generators (Nikola, etc.), text editors (Sublime Text, Kate, etc.), math programs (Sagemath, SAS, etc.), business intelligence and reporting tools (PowerBI, Rapidminer, etc.), even shells (Xonsh). That’s a heck of a lot of substance!

Leave a reply to Joseph Mitzen Cancel reply