Python Programming in Minecraft

Using Python in Minecraft

If you show a kid how to do his times tables with a for loop, he may get mildly interested.  But if you show a kid how to build a diamond tower in Minecraft with a for loop, and now we are talking excited.

Some very clever people have wrapped up the Minecraft programming interface into a Python library. You can use this library to run Python programs in a live game of Minecraft.  The program was originally distributed for the Raspberry Pi Linux operating system, but some other clever people have created a way to run the Python Minecraft library on a PC.

If you want to get the kids excited about programming, you should check this out.

Minecraft on the Raspberry Pi

The latest version of the Rapsbian operating system distribution comes with the Minecraft game server installed. If you are not using the latest Raspbian, you can install Minecraft using the instructions on the pi.minecraft.net site.  These instructions are reproduced here with some additional comments to clarify how to handle users and permissions properly.

  • To download the Minecraft game server for Raspberry Pi: click here if you are using your Raspberry Pi desktop browser, make sure to save the file in your user directory to avoid problems with permissions.
  • Or,  from the command line, create a new Minecraft directory in your home folder and, type: “sudo wget  https://s3.amazonaws.com/assets.minecraft.net/pi/minecraft-pi-0.1.1.tar.gz”
  • To decompress it: “tar -zxvf minecraft-pi-0.1.1.tar.gz”.  This will create the directory mcpi underneath where you decompressed the tar file.
  • To run Minecraft, navigate to the directory with: cd mcpi
  • Followed by ./minecraft-pi.

You can now run Minecraft on your Raspberry Pi.

Note, if when you run minecraft you get an error that says “failed to open vchiq instance”, then you need to change the file permission of the vchiq file.  Type “sudo chmod 777 /dev/vchiq

Writing Python Programs for Minecraft (Raspberry Pi)

To get started writing Python programs to run on Minecraft for the Raspberry Pi, you should follow the instructions on this page.  The StuffAboutCode.com web site has lots of excellent Minecraft Python programming details and Raspberry Pi information.

Once you are setup, you can start using the Minecraft game as a focal point for learning how to write programs using Python.  You should check out some excellent resources created by Craig Richardson.

Writing Python Programs for Minecraft (Windows or Mac)

If you are not using a Raspberry Pi, or even if the kids do have a Raspberry Pi, but they want to run their Python programs on their PC version of Minecraft (which is faster and has more features), there is a way to use the Python library for Minecraft on your PC.  You have to setup your own local Minecraft server that is capable of talking with the Python library for Minecraft.

There is an excellent set of instructions here.  I add some details below to address some problems I had making it work.

  • First, you need to make sure you can run Java programs from the command line. If you are using a Mac, you can skip this step. If you are running Windows, you must first:
    • Find Java.exe on your Windows computer, it is probably at “C:\Program Files (x86)\Java\jre7\bin” for a 64 bit machine.
    • Add the path “C:\Program Files (x86)\Java\jre7\bin” to your Path Environment Variable.  There are instructions on how to set a path variable in our tutorial here, in the last section.
  • Now you can run the instructions from this site to install your local Minecraft Server, and the plugin that allows the Python library to work.
    • Note:  Pay attention to the file names that are downloaded. Some of the files are a more recent version than referenced in the instructions, so you will need to alter the syntax of the commands in the instructions to use the proper file names.
  • Now you need to setup the Python library for minecraft on your PC.  Download the .tar (zipped) file from pi.minecraft.net site.
  • Create a folder in your user directory, say “rpiMinecraft” and extract the .tar file there.  Note, you may need to get a program tarball zip file extractor for your PC such as 7-Zip.
  • The extracted tar file will create a directory called mcpi.  Now create another directory in your User folder, say “McPython”.  Create a sub directory there calling it “McPython/minecraft”.  Note, it is important that this last step use the directory name “/minecraft”.
  • Using file explorer, find “… rpiMinecraft/mcpi/api/python/mcpi”.  Copy the contents of this folder to “McPython/minecraft/”.
  • Go to your “… /McPython” directory, and save a Python program there.  You can get an excellent sample to start with here.
    • Note, in your Python program, you will type “import minecraft.minecraft as minecraft”.  This is why it is important to have your subfolder named “minecraft in the previous step.
  • You are now running your own Python code on your local Minecraft server on your PC.  Now get to work making something really great.

Best Python for Minecraft Links

Arghbox Guide

StuffAboutCode