Author: Tim Cummings

Using PyCharm with Anaconda

No Comments

There are a few tricks to getting PyCharm to work with Anaconda. This post demonstrates running a flask web application on Windows 10 with the only Python installation being Anaconda. The source code we will use is a git repository so you will need git installed to proceed. (see The screenshots use PyCharm Edu which is the same as PyCharm Community with the edu-tools plugin. You don’t need this plugin. Also, we do not have the Anaconda plugin which I think is only available for PyCharm Pro.

To begin, run PyCharm. You may need to close any open projects to see the splash screen. (File menu > Close project).

1. You can clone a git repository straight from the splash screen. Click “Get from Version Control”

2. Select “Repository URL” and fill in the details of the repo on github.
Version control: Git
Directory: C:\Users\<username>\PyCharmProjects\bpss-prime-minister
Click “Clone” to clone the repository

3. Click “Yes” to open the project directory in PyCharm.

4. Now you need to configure PyCharm to use an Anaconda virtual environment. This is done in “File” > “Settings…” in Windows/Linux or “PyCharm” > “Preferences” in Mac.

5. In settings, select the Project Interpreter for this project. As this is the first project, no Interpreters are currently set up. Click on “Show All…” in the Project Interpreter pull down menu.

6. The list of Project Interpreters is empty, so click on the “+” to create a new one.

7. Be sure to select “Conda Environment” in the left panel. If you were using Python without Anaconda you would select “Virtualenv Environment”. Selecting “System Interpreter” is not best practice because you will pollute the system python installation with custom modules making it difficult to replicate environments on different computers. “Pipenv Environment” is for those who prefer using pipenv to manage environments. This is more a legacy requirement as “Virtualenv Environment”, which these days uses venv rather than virtualenv, can do anything pipenv can do.

Conda environments are stored in the Anaconda root directory. I installed Anaconda for my use only so this directory is C:\Users\<username>\Anaconda3 . If you have installed Anaconda for all users it will probably be C:\Anaconda3 .

The conda environment can be called anything you want but I normally call it the same name as the project so I know what it is used for. In this case I have named it “bpss-prime-minister”.

Click “OK” to create the new conda environment. Then click “OK” on the “Project Interpreters” window to return to “Settings”.

8. Click “OK” ensuring the newly created environment is now selected as the Project Interpreter in the pull down menu.

9. Open Terminal pane by clicking on “Terminal” in the bottom left of the project window.

10. In the future, when you open Terminal, it will automatically activate the correct conda environment. It may even activate it now as it has done for me in the screenshot. However, if you opened Terminal before creating the environment, it will be using base environment, indicated by (base) at the beginning of each prompt in Terminal. Activate the new conda environment by typing:

conda activate bpss-prime-minister

After activating, each command line prompt should start with (bpss-prime-minister) .

Now you need to install the library requirements which are stored in requirements.txt. Normally you would type conda install --file requirements.txt or even leave it for PyCharm to automatically install requirements, but in this case conda has problems installing flask_bootstrap and python-dotenv. To solve these issues I have decided to use pip to install all the requirements. PyCharm Terminal has already put me in the correct directory so I can just use the filename without the path.

pip install -r requirements.txt

11. After all the requirements are installed you can launch the app. The first command is a custom command defined by the app to initialise the database. In Terminal type:

flask init-db

The second command launches the app in a dev only web server. In Terminal type:

flask run

12. You can click on the displayed hyperlink in Terminal, or open a web browser such as Firefox and type in the address.

Congratulations. You are now running a flask web application on your Windows computer using only Anaconda and PyCharm. To stop the application, go back to Terminal and type ctrl-c, by holding down the “Ctrl” key, pressing the “c” key, and then releasing both keys.

Categories: Python

Flask “Hello World!” in PyCharm Edu

On their website, Flask provide a simple “Hello World!” demo that you can run from the command line. I have written this post to show how the same thing can be achieved using PyCharm Edu. It is more steps, but once it is set up, the web application can be run by a menu item or keystroke in PyCharm Edu without ever having to use the terminal. Jetbrains recommend PyCharm Professional for Flask development work but with this technique all you need is one of the free IDEs PyCharm Edu or PyCharm Community.

TL;DR (too long; didn’t read)

If you are already familiar with PyCharm Edu, the steps are essentially:

  1. Create a new python project with a virtual environment, and “Flask” in your requirements.txt file.
  2. Create a new Python file as per the Flask demo.
  3. If you are using PyCharm Edu you will need to enable the “Run” menu.
  4. Create and run a new “Python” run configuration:
    1. Name: flask run hello
    2. Module (not Script): flask
    3. Parameters: run
    4. Environment variables: PYTHONUBUFFERED=1;;FLASK_ENV=development

Detailed instructions with pictures

These screen shots have been taken from PyCharm Edu 2019.1 using Python 3.7.3 on Windows 10 Pro.

Create a new project in PyCharm Edu
Name your project “hello-flask”. This will also create a new virtual environment.
Click on “hello-flask” to select the project folder. Then click on > next to “hello-flask” to expand the folder and show the contents. Depending on where you are saving your virtual environments you may or may not see “venv”.
Now that the project folder is selected, you can select “New…” from the “File” menu to create a new file in the project folder.
A floating menu will appear asking you the type of file. We are creating a text file so choose “File” which can be used for any file type.
Name the file requirements.txt and click “OK”.
In the requirements.txt type the word “Flask”. This replaces the step “pip install Flask” from the command line demo. After you have typed in “Flask” click on the recommendation to “Install requirement”. The recommendation may not come straight away so continue on to the next steps and it should appear before you get to running the web application.
To create a new Python file, right click on the project folder and select New > Python File.
Because PyCharm knows it is going to be a Python file, we don’t need to type in .py on the end. Just type in the name “hello” and click “OK”.
In, type in the code from the Flask “Hello World!” demo. The file will get saved automatically.
If you are using PyCharm Community Edition you can skip the next three steps which are only required to enable some menus which are hidden in PyCharm Edu but visible in PyCharm Community Edition. Click on “Settings…” in the “File” menu. On Mac this menu is called “Preferences”.
The main menu is missing some menus so:
1. Expand “Appearance and Behaviour”
2. Select “Menus and Toolbars”
3. Expand “Main menu”
4. Select “Code” menu so we can add new menus after it
5. Click on “+” to add extra menus
6. Click “Add Action…”
Select all menus between “Refactor” and “Window” and click on “OK”.
The menus are now included so click “OK”
To run a flask app we need to create a “run configuration”. Select “Edit configurations…” from the “Run” menu.
Click “+” to add a new configuration and select “Python”.
1. Name your configuration “flask run hello” or something else if you prefer.
2. Change label from “Script path:” to “Module name:”
3. Type “flask” as the module name
4. Type “run” as the parameter
5. Configure environment variables by clicking box. (See next screenshot)
6. After setting environment variables (next screenshot), click “OK”
1. “+” to add a new environment variable
2. Name it “FLASK_APP”
3. Give it value “”
4. “+” to add another environment variable
5. Name it “FLASK_ENV”
6. Give it value “development”
7. Click “OK” to return to previous dialog box and click “OK” again.
Now you can run the builtin web application server by selecting “Run ‘flask run hello'” from the Run menu. If the first item does not say ‘flask run hello’ then choose “Run…” (third item in menu) and select ‘flask run hello’ from the popup menu.
A run status pane will open at the bottom of the screen. You can click on the URL to see the outpu of your web application in a web browser. Click the red square to stop your application.
This is what your web browser should show you. Yay!!! 🙂
Categories: Playing

Pythonator at QSITE

1 Comment

Pythonator makes Python coders out of gamers. It runs on a Minetest game server and comes with a companion PyCharm Edu course which teaches gamers how to overcome the obstacles in the game by writing Python programs.

A Pythonator workshop will be held at QSITE conference 2018. Attendees should bring a Windows, Mac or Linux laptop. Android and iPads are not suitable. If you don’t already have the following software installed, please install it prior to attending.

  1. Python 3
  2. PyCharm Edu v2018.1.2
  3. Minetest v0.4.17.1

Also download the courses. Only the first course will be used during the workshop.


Send an email to to request a free account on the server. Then you can try out the exercises on the server for a few weeks after the completion of the conference.

Detailed installation – Windows 64 bit

Python 3

From the Python website download Python 3.6.5. Default settings are fine during the install although I recommend users also select “Add Python 3.6 to PATH” which is unticked by default.

PyCharm Edu 2018.1.2

Download and install PyCharm Edu v2018.1.2.
Download the Pythonator companion courses for PyCharm Edu. Do not unzip or extract the companion courses.


  1. Download Minetest
  2. Extract the zip file into your Documents directory so you have a folder Documents\minetest-
  3. In File Explorer right-click on Documents\minetest-\bin\minetest.exe and select “Send to > Desktop (Create shortcut)”.

Now you can run Minetest by double-clicking the Minetest icon on your desktop.


Detailed installation – Mac

This section is for users of macOS 10.13 who are uncomfortable with using the command line. If you are comfortable using the command line, or running an older version of macOS, see the next section.

Python 3

From the Python website download and install Python 3.6.5. Default settings are fine during the install.

PyCharm Edu 2018.1.2

Download and install PyCharm Edu v2018.1.2.
Download the Pythonator companion courses for PyCharm Edu. Do not unzip or extract the companion courses. Safari users need to ensure Safari doesn’t ‘open “safe” files after downloading’ which automatically unzips these files. This is the bottom setting in the General tab of Safari Preferences…


If you are running macOS 10.13 you can download a prebuilt version of minetest To install, open the downloaded dmg file and drag the minetest icon on to the Applications icon. To run the first time right-click the minetest icon in Applications folder and click “Open”. There might be a security warning but you will then be able to click “Open” button.


Detailed installation – Mac (using the command line)

This section is for those comfortable with the Mac command line using Terminal application. I prefer installing using Homebrew because it matches the build of minetest to my version of macOS and it is easy to install updates.

Python 3 and Minetest

Type the following in Terminal to install XCode, Homebrew, Python 3 and Minetest if you have not installed them already.

xcode-select --install
/usr/bin/ruby -e "$(curl -fsSL"
brew install python
brew install minetest
open /usr/local/opt/minetest

The last line opens the installed minetest folder in Finder. Double-click the minetest icon. While minetest is running, right-click on the icon in the dock and select “Options > Keep in Dock” to make it easier to run next time.

PyCharm Edu 2018.1.2

Download and install PyCharm Edu v2018.1.2.
Download the Pythonator companion courses for PyCharm Edu. Do not unzip or extract the companion courses.

Detailed installation – Windows 32 bit

Python 3

From the Python website download Python 3.6.5. Default settings are fine during the install although I recommend users also select “Add Python 3.6 to PATH” which is unticked by default.

PyCharm Edu 2018.1.2

Download and install PyCharm Edu v2018.1.2.
Download the Pythonator companion courses for PyCharm Edu. Do not unzip or extract the companion courses.


  1. Download Minetest
  2. Extract the zip file into your Documents directory so you have a folder Documents\minetest-
  3. In File Explorer right-click on Documents\minetest-\bin\minetest.exe and select “Send to > Desktop (Create shortcut)”.

Now you can run Minetest by double-clicking the Minetest icon on your desktop.


Detailed installation – Ubuntu Linux

sudo add-apt-repository ppa:minetestdevs/stable
sudo apt install python3 python3-venv libcanberra-gtk-module minetest 
sudo snap install pycharm-educational --classic

To open a course in PyCharm Edu

File menu > Browse Courses > Click the green arrow > Import local course > Select one of the above two files > Click Join

Categories: CoderDojo, Minetest

Running the PyCharm Edu Minetest course


If you want to run the PyCharm Edu Minetest course developed by Triptera you will need a licence. Licences are free if you are running the course in 2018. Please contact Triptera for a licence. Please provide details where you would like to use the software. The licence covers the “builder_police” mod for Minetest and the “Ninja 2 minetest course” for PyCharm Edu, both of which are available with open source. All other components are covered by free and open source licences. Support is available to assist you setting up the PyCharm Edu Minetest course.

What do students learn

Students doing my PyCharm Edu Minetest course learn the following mathematical concepts

  • 3 dimensional coordinate systems
  • formulae including uses of integer division and modulo and absolute values

They also learn the following python coding skills

  • program flow
  • variables
  • loops
  • conditionals
  • data types including ints, strings, lists, tuples, dictionaries, JSON strings
  • functions
  • modules


Minetest is a Minecraft clone allowing players to build block structures in a virtual world. To set up your PyCharm Edu Minetest course server follow instructions at Python programming course using Minetest

Minetest is open source free software which runs on Linux, Mac, Windows plus it even runs on Android, iOS and Haiku. At CoderDojo we used to run this course in Minecraft but have now switched to Minetest because of its many advantages.

  • Minetest is free so ninjas don’t have to buy a Minecraft licence.
  • Minetest passwords can be set by server op. This is much better than Minecraft which would not ask for the password when ninjas were at home, but as soon as they joined our LAN at CoderDojo Minecraft asked for their password which they couldn’t remember.
  • Minetest can run on a LAN with no Internet access. Minecraft users would have to log in to the unreliable library wifi to enter their Minecraft password and then switch networks to our LAN to join the virtual world.
  • Minetest is better on low budget hardware. I was running a server on a 2GB RAM Core 2 Duo Linux Mint headless (no monitor) desktop PC.
  • Minetest has an excellent modding interface that encourages mods unlike Minecraft which obfuscates its java code to make modding harder. The Lua modding language has surprisingly good performance. I hadn’t used Lua before but I picked it up quite quickly and it is apparently common in other games which need scripting. I did have trouble getting network sockets working in Lua on Windows but eventually solved that problem.

Minetest does not do mobs (monsters and other non player characters) as well as Minecraft. Mobs in Minetest are not native so have to be implemented in mods using the Lua scripting language. This has not been an issue for this course which we run without mobs for better performance.

Op Commands

Running the PyCharm Edu Minetest course is mostly automatic. However there are a few custom commands you can enter through in-game chat to manage the world

/set_player_task player_name task_number
resets a players tasks (replace player_name with player’s name, task_number = 0 to start again)
example /set_player_task tim 1

/set_jail_free_task 1
sets the task a player needs to complete to get out of jail

There are also some useful built-in commands

provides help on chat commands

/help all
lists all the chat commands

/help command
help on a specific chat command. e.g. /help time

/time 6:00
set clock to morning time so players are not in darkness. If this becomes onerous, consider setting the “Time speed” to zero in Settings > Advanced settings > Server / Singleplayer > Game > Time speed

/grant player_name irc_builder
gives a player privileges to use building commands over IRC (should be automatic for new players) will show player’s list of existing privileges.
example /grant tim irc_builder

/revoke player_name irc_builder
opposite of grant

/teleport player_name x y z
move player to new coordinates. e.g. /teleport tim 95 12 20 see help for variations

/setpassword player_name new_password
sets player’s password in Minetest. Password can not be blank or contain spaces if it is to work with ircbuilder.
example /set_password tim sesame

see what privileges you have

You can even send any of these commands to the server over IRC. Send private messages to the server botnick (eg mtserver). The first message is to login. The second and subsequent messages are the same as the chat commands but replace the / with cmd. Below is an example IRC session. Replace mtuser and mtuserpass with the minetest user and password you want to connect as. The minetest username can be different to the IRC nick name.

login mtuser mtuserpass
cmd privs
cmd time 6:00

You can even use python console to send commands if you don’t have an IRC client. mtuser needs to be the serverop player name

from ircbuilder import MinetestConnection
from coderdojo import ircserver, mtuser, mtuserpass, mtbotnick, channel
mc = MinetestConnection.create(ircserver, mtuser, mtuserpass, mtbotnick, channel)
mc.send_cmd("time 6:00")
Categories: CoderDojo, Minetest

PyCharm Edu review

PyCharm Edu is an Integrated Development Environment (IDE) for python which has the additional ability to run courses for learning python programming. It also enables teachers to create those courses. PyCharm Edu is cross platform, running on Linux, Mac and Windows.

At CoderDojo I am mentoring ninjas in coding python so I thought I would try PyCharm Edu. To create a course, I created a series of tasks. Each task is a python program with sections left out to be completed by ninjas (answer placeholders). A task has a task description to explain to the ninjas what to do, and a series of tests to determine when they had completed the task. Each answer placeholder has hints to help solve it, some text which needs to be replaced by the ninja and even the correct answer. The tasks have to provide enough information for the ninja to understand what needed to be done, but not enough that the answer was trivial.

Writing the tests was challenging. The tests needed to check for all possible correct answers. For example, if the correct answer was “x+y” then I should also accept “y+x” or “y + x”. I solved this problem by using the python eval() or exec() to test the value of functions or the effect of statements rather than compare the exact characters typed to the answer.

For example, here is how I used eval() to test for where the correct formula should be (x+y).

def test_formula_1(ninja_answer):
    list_data = [{"x": 5, "y": 15}, {"x": 10, "y": -5}]
    global_data = {}
    correct_answer = "x+y"
    for data in list_data:
            guess = eval(ninja_answer, global_data, data)
        except NameError:
            failed("NameError: should only be in terms of variables " + ",".join(data.keys()) + " but includes other variables. Your formula: " + ninja_answer)
            return False
        correct = eval(correct_answer, global_data, data)
        if guess != correct:
            failed("Incorrect answer for data " + str(data) + ". Correct answer: " + str(correct) + ". Your answer: " + str(guess) + ". Your formula: " + ninja_answer)
            return False
    return True

The types of tests I used to check for correct answers were

  • Exact match – when a string value or number value had to be exact
  • Partial match – when only start of string or end of string important
  • Formula correct – using eval()
  • Statement correct – using exec()
  • Printed output correct
  • Effect of program – eg results in Minetest world

When the ninja (student) is running the course they have access to a full python IDE, including command completion and debugging. As a teaching aid one of the most useful features is the console where ninjas can test individual python statements. The console has a sub-window showing variables created in the console and their values, great for teaching how variables work.

The ninja can see the code for the current task and a task description describing what they have to do. In the diagram below notice the sections of code with thin rectangles around them. These are the answer placeholders that the ninja needs to replace to solve the task. They shouldn’t need to type outside these rectangles, although they can if they want to embellish the program.

While attempting a task they can do the following actions:

  • Get hints on an answer
  • Reset the task to undo all their changes
  • View the answers (after which they must reset the task)
  • Run the program with output going to a local window (ctrl-shift-F10)
  • Check the task which assesses how well they have done by running tests
  • Advance to next or previous task

Advantages of using PyCharm Edu over web python tutorials or text editors:

  • Course can be customised
  • Student learns a proper IDE. Useful for developing bigger python projects later
  • Python console with variable values display
  • Syntax colouring and formatting recommendations
  • Command completion
  • Logic checking such as:
    • variables declared before used
    • import statements used or missing
    • spelling of function names and variable names
  • Python virtual environment support
  • Automatic installation of modules listed in requirements.txt either from pypi or LAN
  • More reliable and functional than Python IDLE
  • Better debugging than Thonny

Problems with PyCharm Edu

I did find some problems with PyCharm Edu. However, Jetbrains have been excellent at responding to my issues in this free software.

  • PyCharm Edu is usable but expect occasional hang or lack of responsiveness.
  • PyCharm Edu turns off a lot of menu items by default to avoid confusing new students. They can be turned back on in preferences.
  • PyCharm Edu and PyCharm Community Edition don’t have the same support for flask or django development found in PyCharm Pro. I was still able to do some flask development.
  • Unable to display local images in task descriptions. This feature is apparently coming in an update due in a couple of weeks.
  • “Play” button for running tasks mostly didn’t work. However, ctrl-shift-F10 achieved the same functionality reliably.


PyCharm Edu is my choice of tool for teaching python and I am sure most of the problems will be addressed in time.

Categories: CoderDojo, Minetest

Python programming course using Minetest

1 Comment

Do you want to learn at home how to program in python while enjoying the fun of building structures in Minetest? These instructions will help you create your own setup similar to the one used at Coderdojo. First of all, make sure you have finished the instructions for the Minetest client install and the Minetest server install.

The steps to configure your course platform:

  1. Install builder_police mod for Minetest.
  2. Install courses in PyCharm Edu

To configure your course platform you will need Internet access to download builder_police from github, ircbuilder python module from, and the courses from this website.

To run the course you will need Internet access to a public IRC server (e.g., which handles the communications between your python program and Minetest.

Minetest mods

The following commands are to be typed into Git-Bash (Windows) or Terminal (Mac, Linux). Change directory to the minetest mods directory. If the directory doesn’t exist you may have to create it.

cd Documents/minetest-0.5.0-6cfd699b-irc-win64/mods  # Windows 64 bit
cd Library/Application\ Support/minetest/mods # Mac
cd .minetest/mods                             # Linux
git clone

Now that you have installed the builder_police mod, you need to enable it in Minetest by selecting “configure” for the world, selecting the “builder_police” mod and clicking the “enabled” check box.

Install courses in PyCharm Edu

    1. Download the following files without unzipping them. Safari users may have to turn off “Open safe files after downloading” in the “General” tab of the Preferences to stop automatic unzipping.
    2. Open PyCharm Edu and select “Browse Courses” from the “File” menu.
    3. Underneath the list of courses, click on the small rectangle with an arrow pointing into it and select “Import a local course”.
    4. Select the downloaded file “”.
    5. Once it has imported you can click on the “Join” button in the bottom right hand corner. Joining a course takes a while because PyCharm will set up a virtual environment for your python programming which doesn’t affect other python programs on your computer.
    6. The virtual environment will detect that you need the python module ircbuilder version 0.0.5 or later and give you an option to install or ignore this requirement. Choose “Install requirement”.
    7. To do the course there are some details you need to set so that your python programs can connect to IRC and Minetest. These details are all stored in a file called “”. In PyCharm, go to the File menu and select “Open…”. Navigate to your home folder, the folder “PyCharmProjects”, then the folder “Ninja 2 minetest course v5”, select “” and click “OK”.
mtuser = ""      # your minetest username
mtuserpass = ""  # your minetest password. This file is not encrypted so don't use anything you want kept secret. Can't have spaces in it

# The following must match your settings in minetest server > Settings > Advanced Settings > Mods > irc > Basic >
ircserver = ""   # same as IRC server
mtbotnick = ""   # same as Bot nickname
channel = ""     # same as Channel to join
    1. You need to fill in the values. Here is an example. Of course, you would need different values for mtuser, mtuserpass and channel.
mtuser = "joe"                   # your minetest username
mtuserpass = "mysecretpassword"  # your minetest password. This file is not encrypted so don't use anything you want kept secret. Can't have spaces in it

# The following must match your settings in minetest server > Settings > Advanced Settings > Mods > irc > Basic >
ircserver = ""  # same as IRC server
mtbotnick = "mtserver"       # same as Bot nickname
channel = "##minetest3214"   # same as Channel to join
  1. Save this file and you are ready to do the course.
Categories: CoderDojo, Minetest

Minetest server install


If you want to practice at home building Minetest structures with python programs, you will need to install Minetest server with its mods. Before we start, make sure you have completed the Minetest client install.

The steps to configure the server are:

  1. Install git
  2. Install mods. irc, irc_commands, irc_builder, signs_lib
  3. Configure IRC


Windows users will need to download the latest from and install. This will also install Git-Bash which is a command line shell similar to those on Mac and Linux.

Linux and Mac users should already have git installed. To test, type the following in the Terminal:

git --version

If it is not installed it will give an error message saying “command not found”. Mac users can download the software from the above site or install it using brew as they did with minetest.

brew install git # Mac

Linux users can install using one of the following if it is not already installed.

apt install git # Ubuntu, Debian
yum install git # Fedora, Centos, Red Hat


The irc mod for minetest requires luasocket. This is difficult to install on windows so I have built a 64 bit version of minetest with irc mod and luasocket already built in. It can be downloaded from

On Mac, install luasocket using brew and luarocks.

brew install lua51
luarocks-5.1 install luasocket

On Ubuntu install luasocket using apt and luarocks.

apt install luarocks
luarocks install luasocket

Minetest mods

The following commands are to be typed into Git-Bash (Windows) or Terminal (Mac, Linux). Change directory to the minetest mods directory. If the directory doesn’t exist you may have to create it.

Windows 64 bit

cd Documents/minetest-0.5.0-6cfd699b-irc-win64/mods


cd Library/Application\ Support/minetest/mods
git clone --recursive


cd .minetest/mods
git clone --recursive

Windows / Mac / Linux

git clone
git clone
git clone
mv minetest-irc_commands irc_commands

Configure mods and IRC

Every Minetest world in which you want to use these mods needs to have them enabled.

  1. Run Minetest.
  2. Select [Settings] tab, then [Advanced Settings] button in bottom left
  3. Expand: + Server / Singleplayer + Security
  4. [Edit] secure.trusted+mods. type in irc
  5. Expand: + Mods + irc + Basic
  6. [Edit] Bot nickname. For example: mtserver
  7. [Edit] IRC server. I like Rizon which has server:
  8. [Edit] Channel to join. Choose a unique channel . eg ##minetest3214
  9. Port number can be 6667 and Auto-connect enabled. Channels starting with ## are generally temporary channels. Remember these settings to use in your python program. Make sure your IRC server allows several connections from the same IP address.
  10. From the [Local Game] tab, select your world and click on [Configure]
  11. Select mod on the right and click the “enabled” check box above for each of the four
  12. Click [Save] button to return to [Local Game] tab
  13. Type in a password in green password field and click [Host Game]
  14. Users who wish to build structures from python will need privilege irc_builder. After they have logged in grant them this privilege by typing /grant username irc_builder

Program in Python 3

Now you can start to write programs in Python which will build structures in Minetest. Remember to install ircbuilder python module in your python virtual environment so that your program can communicate with IRC and hence with Minetest. You can do this in PyCharm Edu from Settings (Windows) or Preferences (Mac) > Project > Project Interpreter > + (and type in ircbuilder).

Categories: CoderDojo, Minetest

Minetest client install



At CoderDojo this term we will be learning to program in Python 3 to build structures in a Minetest world. Minetest is a free, open-source, high performance Minecraft clone that is easily modded. There already exist mods which allow chat commands to be executed over IRC (Internet Relay Chat). I have created a Minetest mod (irc_builder) to add chat commands allowing us to build structures using those commands. I have also created a python module (ircbuilder) which sends commands through IRC to the Minetest world.

To set up a client computer you only need install three things:

  1. Python 3
  2. PyCharm Edu v2017.3
  3. Minetest v0.4.16

Python 3

Windows or Mac: From the Python website download Python 3.6.4 or later. Install it using the default settings, plus select “Add Python 3.6 to PATH” which is unticked by default.


sudo apt install python3 # Debian, Ubuntu
sudo yum install python3     # Red Hat, Fedora, Centos

PyCharm Edu

PyCharm Edu is a free open source integrated development environment (IDE) for Python, better than IDLE which comes with Python. It also can guide students through a course of lessons and tasks, providing hints and checking on tprogress automatically.

Windows, Mac, Linux: Download PyCharm Edu v2017.3 or later from the Jetbrains website. Install using defaults. The courses will be provided as zip files with the names


To open a course

File menu > Browse Courses > Click the green arrow > Import local course > Select one of the above two files > Click Join


You can install minetest client without any mods as server mods only need to be installed on the server and then every client can use them.

Windows 64 bit

Download Minetest v0.4.16 (portable 64  bit) from the Minetest website. Extract the zip file to your Documents folder. The program can be run by double clicking on Documents\minetest-0.4.16-win64\bin\minetest.exe

Windows 32 bit

Download Minetest v0.4.16 (portable 32  bit MSVC) from the Minetest website. Extract the zip filet to your Documents folder. The program can be run by double clicking on Documents\minetest-0.4.16-win32-msvc\bin\minetest.exe


The Mac version of Minetest needs to be installed using Terminal (Applications/Utilities). If you haven’t installed brew, install it first with (one line):

/usr/bin/ruby -e "$(curl -fsSL"

Then install minetest from Terminal with:

brew cask install xquartz
brew install minetest

Minetest can be run by double clicking /usr/local/opt/minetest/ or from Terminal with:

sudo apt install minetest # Debian, Ubuntu
sudo yum install minetest     # Red Hat, Fedora, Centos

Minetest can be run by typing minetest on command line.

Categories: CoderDojo, Minetest

Python in Minecraft 10 – functions and modules

Importing modules

In the previous python in minecraft blog we created a program to construct an east-west tunnel. This is a useful program which we might want to use often. We can put the program into a function and save the function in a module so it can be used by other programs. We will call the function “buildEastTunnel()”. The module name will be just the name of the file, which in this case we will call “”. Then whenever we want to build a tunnel running east west, we can import the module just like we have been importing mcpi.minecraft and mcpi.block.

There are several ways to import functions. The simplest method imports the module but not the function name. We still need to tell python to look in that module when calling the function

import tunnel

If you want to give the module a better name you can use “as” to assign a new name. This is what we do when importing mcpi.minecraft as minecraft.

import tunnel as mole

If you want to use the function without qualifying it with the module name each time, you can import the function explicitly from the module.

from tunnel import buildEastTunnel

Function parameters

Now calling buildEastTunnel() as it is currently written will always construct a tunnel in the same place, x = -400 to -200, y = 72 at each end, z = 244. However, if we design the function to take parameters then when we call the function we can specify where the tunnel is built.

def buildEastTunnel(mc, x1, x2, y1, y2, z):
    """Builds an east-west tunnel (in the x direction)"""
    # rest of function ...

import mcpi.minecraft as minecraft
from tunnel import buildEastTunnel
mc = minecraft.Minecraft.create()
buildEastTunnel(mc, 0, 200, 75, 75, 233)

This example passes the mc object to the function so it knows which minecraft server to build the tunnel in. Then it gets the x, y and z coordinates at each end of the tunnel. Because the tunnel is running east-west there is only one z value required. Our code is designed to go from xmin to xmax but someone calling our function might put the higher value of x first and the lower value second or vice versa. We can take advantage of python’s ability to assign several variables at once to ensure xmin is less than xmax.

def buildEastTunnel(mc, x1, x2, y1, y2, z):
    """Builds an east-west tunnel (in the x direction)"""
    if x1 < x2:
        xmin, yatxmin, xmax, yatxmax = x1, y1, x2, y2
        xmin, yatxmin, xmax, yatxmax = x2, y2, x1, y1
    # rest of function ...

When designing a function to take parameters, it is possible to specify some parameters as “required” and other parameters as “optional”. Optional parameters need to be given a default value. In our example it would be useful for the y values to use ground level if it is not supplied. “Required” parameters have to come before “optional” parameters so the parameters list should now be (mc, x1, x2, z, y1, y2).

def buildEastTunnel(mc, x1, x2, z, y1=None, y2=None):
    """Builds an east-west tunnel (in the x direction)"""
    if x1 < x2:
        xmin, yatxmin, xmax, yatxmax = x1, y1, x2, y2
        xmin, yatxmin, xmax, yatxmax = x2, y2, x1, y1
    if yatxmin == None:
        yatxmin = getGroundHeight(mc, xmin, z)
    if yatxmax == None:
        yatxmax = getGroundHeight(mc, xmax, z)
    # rest of function ...

Finding ground height

We are calling a function getGroundHeight(mc,x,z) which we haven’t written yet. In mcpi.minecraft is a function mc.getHeight(x,z) which finds the highest block which is not air at x and z coordinates. Unfortunately that block will often be a tree or sapling. Here is a function which corrects mc.getHeight(x,z) for trees. It makes use of the mc.getBlock(x,y,z) function which returns the id of the block at those coordinates.

def getGroundHeight(mc, x, z):
    """finds heighest non air block which is also not a tree or sapling"""
    LEAVES2 = block.Block(161)
    y = mc.getHeight(x, z)
    while mc.getBlock(x, y, z) in (,,,,
        y -= 1
    return y
Categories: CoderDojo, Minecraft

Moon lander with autopilot in python

In the previous example, the user was the pilot. See if you can design an autopilot which will land the lunar landing module on the moon. You need to design a function which calculates a new burn value based on available data, altitude, speed, gravity. Here is my simple example.

    if speed + gravity > 2 * impact:
        burn = 10 * (speed + gravity - (impact) * 2)
    if impact < 2:
        burn = 10 * (2 * speed + gravity - altitude)

It is a bit erratic but lands properly at the end. See if you can develop a formula which has a smoother burn control.

Altitude=1000.000 Speed=30.000 Fuel=1500.000 Impact=33.333 Previous burn= 0.000
Altitude= 970.000 Speed=29.622 Fuel=1480.000 Impact=32.746 Previous burn=20.000
Altitude= 940.378 Speed=29.244 Fuel=1460.000 Impact=32.156 Previous burn=20.000
Altitude= 911.134 Speed=28.866 Fuel=1440.000 Impact=31.564 Previous burn=20.000
Altitude= 882.268 Speed=28.488 Fuel=1420.000 Impact=30.970 Previous burn=20.000
Altitude= 853.780 Speed=28.110 Fuel=1400.000 Impact=30.373 Previous burn=20.000
Altitude= 825.670 Speed=27.732 Fuel=1380.000 Impact=29.773 Previous burn=20.000
Altitude= 797.938 Speed=27.354 Fuel=1360.000 Impact=29.171 Previous burn=20.000
Altitude= 770.584 Speed=26.976 Fuel=1340.000 Impact=28.566 Previous burn=20.000
Altitude= 743.608 Speed=26.598 Fuel=1320.000 Impact=27.957 Previous burn=20.000
Altitude= 717.010 Speed=26.220 Fuel=1300.000 Impact=27.346 Previous burn=20.000
Altitude= 690.790 Speed=25.842 Fuel=1280.000 Impact=26.731 Previous burn=20.000
Altitude= 664.948 Speed=25.464 Fuel=1260.000 Impact=26.113 Previous burn=20.000
Altitude= 639.484 Speed=25.086 Fuel=1240.000 Impact=25.492 Previous burn=20.000
Altitude= 614.398 Speed=24.708 Fuel=1220.000 Impact=24.866 Previous burn=20.000
Altitude= 589.690 Speed=24.330 Fuel=1200.000 Impact=24.237 Previous burn=20.000
Altitude= 565.360 Speed=23.952 Fuel=1180.000 Impact=23.604 Previous burn=20.000
Altitude= 541.408 Speed=23.574 Fuel=1160.000 Impact=22.966 Previous burn=20.000
Altitude= 517.834 Speed=23.196 Fuel=1140.000 Impact=22.324 Previous burn=20.000
Altitude= 494.638 Speed=22.818 Fuel=1120.000 Impact=21.678 Previous burn=20.000
Altitude= 471.820 Speed=22.440 Fuel=1100.000 Impact=21.026 Previous burn=20.000
Altitude= 449.380 Speed=22.062 Fuel=1080.000 Impact=20.369 Previous burn=20.000
Altitude= 427.318 Speed=21.684 Fuel=1060.000 Impact=19.707 Previous burn=20.000
Altitude= 405.634 Speed=21.306 Fuel=1040.000 Impact=19.038 Previous burn=20.000
Altitude= 384.328 Speed=20.928 Fuel=1020.000 Impact=18.364 Previous burn=20.000
Altitude= 363.400 Speed=20.550 Fuel=1000.000 Impact=17.684 Previous burn=20.000
Altitude= 342.850 Speed=20.172 Fuel= 980.000 Impact=16.996 Previous burn=20.000
Altitude= 322.678 Speed=19.794 Fuel= 960.000 Impact=16.302 Previous burn=20.000
Altitude= 302.884 Speed=19.416 Fuel= 940.000 Impact=15.600 Previous burn=20.000
Altitude= 283.468 Speed=19.038 Fuel= 920.000 Impact=14.890 Previous burn=20.000
Altitude= 264.430 Speed=18.660 Fuel= 900.000 Impact=14.171 Previous burn=20.000
Altitude= 245.770 Speed=18.282 Fuel= 880.000 Impact=13.443 Previous burn=20.000
Altitude= 227.488 Speed=17.904 Fuel= 860.000 Impact=12.706 Previous burn=20.000
Altitude= 209.584 Speed=17.526 Fuel= 840.000 Impact=11.958 Previous burn=20.000
Altitude= 192.058 Speed=17.148 Fuel= 820.000 Impact=11.200 Previous burn=20.000
Altitude= 174.910 Speed=16.770 Fuel= 800.000 Impact=10.430 Previous burn=20.000
Altitude= 158.140 Speed=16.392 Fuel= 780.000 Impact= 9.647 Previous burn=20.000
Altitude= 141.748 Speed=16.014 Fuel= 760.000 Impact= 8.852 Previous burn=20.000
Altitude= 125.734 Speed=15.636 Fuel= 740.000 Impact= 8.041 Previous burn=20.000
Altitude= 110.098 Speed=16.083 Fuel= 728.246 Impact= 6.846 Previous burn=11.754
Altitude=  94.015 Speed=13.692 Fuel= 688.115 Impact= 6.867 Previous burn=40.131
Altitude=  80.324 Speed=13.733 Fuel= 672.314 Impact= 5.849 Previous burn=15.802
Altitude=  66.590 Speed=11.698 Fuel= 635.736 Impact= 5.693 Previous burn=36.577
Altitude=  54.893 Speed=11.385 Fuel= 616.393 Impact= 4.821 Previous burn=19.343
Altitude=  43.508 Speed= 9.643 Fuel= 582.748 Impact= 4.512 Previous burn=33.645
Altitude=  33.865 Speed= 9.024 Fuel= 560.339 Impact= 3.753 Previous burn=22.409
Altitude=  24.841 Speed= 7.506 Fuel= 528.936 Impact= 3.310 Previous burn=31.403
Altitude=  17.335 Speed= 6.619 Fuel= 503.853 Impact= 2.619 Previous burn=25.083
Altitude=  10.716 Speed= 5.238 Fuel= 473.818 Impact= 2.046 Previous burn=30.035
Altitude=   5.478 Speed= 4.092 Fuel= 446.138 Impact= 1.339 Previous burn=27.680
Altitude=   1.386 Speed= 1.386 Fuel= 402.864 Impact= 1.000 Previous burn=43.274
Altitude=   0.000 Speed= 0.000 Fuel= 372.780 Last burn=30.084
You have landed

Here is the full code

# this game simulates a lunar landing module (LLM) landing on the moon
# The programmer is the autopilot and needs to control how much fuel is burnt in the
# retro rockets so that the descent speed slows to zero just as the
# altitude above the moon's surface reaches zero. If it impacts the moon
# more than 5 m below the surface, or your speed on impact is
# greater than 5 m/s then it has crashed.
# Otherwise it is considered to be a 'good' landing.
# If it runs out of fuel, LLM will accelerate towards moon by gravity.

# set up the initial parameters
speed = 30      # speed approaching the moon
fuel = 1500     # how much fuel is left
altitude = 1000 # altitude above moon
gravity = 1.622 # acceleration due to gravity
burn = 0        # initial rate of burning fuel in retrorockets

# while LLM is above the moon's surface,
# calculate flight data and take input from pilot
while altitude > 0:
    # calculate how long until LLM will impact moon at current speed (impact)
    if speed <= 0:
        impact = 1000
        impact = altitude / speed
    # display flight data
    print("Altitude={:8.3f} Speed={:6.3f} Fuel={:8.3f} Impact={:6.3f} Previous burn={:6.3f}".format(altitude,speed,fuel,impact,burn))
    # let autopilot calculate the new burn rate
    if speed + gravity > 2 * impact:
        burn = 10 * (speed + gravity - (impact) * 2)
    if impact < 2:
        burn = 10 * (2 * speed + gravity - altitude)
    # ensure rate of fuel burning is within rocket's capability and doesn't exceed remaining fuel
    if burn < 0:
        burn = 0
    if burn > 50:
        burn = 50
    if burn > fuel:
        burn = fuel
    #calculate new flight data
    altitude -= speed
    speed += gravity - burn/10
    fuel -= burn
# loop has ended so we must have hit moon's surface
# display final flight data and assess whether it was a crash or a good landing
print("Altitude={:8.3f} Speed={:6.3f} Fuel={:8.3f} Last burn={:6.3f}".format(altitude,speed,fuel,burn))
if altitude <- 5 or speed > 5:
    print("You have crashed")
    print("You have landed")
Categories: CoderDojo