kevinflynn1993
Posts: 2
Joined: Fri Jul 31, 2015 3:33 am

can't get python connected to minecraft

Fri Jul 31, 2015 3:49 am

Hi
I have minecraft on my raspberry pi, I have both python 2 and python 3 as well. I can't seem to get my python to interact with the minecraft. when i type in the code:
from mcpi import minecraft

mc = minecraft.Minecraft.create()

mc.postToChat("Hello world")

I just get an error message that mcpi isn't found, or something like that.

i think maybe i need to download minecraft? the thing is it (minecraft) was automatically included in the SD card I received in the mail. its included in the games.

anyone have any idea what i need to do?

thanks,
Kevin

metalj
Posts: 212
Joined: Sat Feb 07, 2015 11:56 pm

Re: can't get python connected to minecraft

Fri Jul 31, 2015 6:42 am

Open Python 2 IDLE (not Python 3) by clicking Menu > Programming > Python 2. Use File > New Window to create a new program and save it as myprogram.py. You are going to need 3 modules, the minecraft.py and block.py modules from the api library and the standard time module so you can introduce delays into your program. Type the following code into your program to import the modules.
import mcpi.minecraft as minecraft
import mcpi.block as block
import time

Use the Minecraft python library to create a connection to the game, your program will use this to talk to Minecraft and make things happen. mc = minecraft.Minecraft.create() Using the minecraft connection, mc, you can interact with the game and send the player a message. Then use time.sleep() put in a 5 seconds delay into your program.
mc.postToChat("Hello Minecraft World")
time.sleep(5)
Make sure Minecraft is still running and that you are ‘in a world’, press ESC to return to the main menu so you can run your program. Run the program by clicking 'Run, Run Module' in IDLE. Switch back to Minecraft to see the results of your hard work - be quick though as its only on the screen for a few seconds, if you miss it, re-run the program and try again. Any errors will be shown in red text in the Python Shell, take care to make sure you have typed the code in correctly, be careful of upper and lower case letters as Python is case sensitive.
Last edited by metalj on Sat Aug 01, 2015 3:54 am, edited 3 times in total.
Backspace 28 times :)

kevinflynn1993
Posts: 2
Joined: Fri Jul 31, 2015 3:33 am

Re: can't get python connected to minecraft

Fri Jul 31, 2015 12:44 pm

thanks a lot!

metalj wrote:Open Python 2 IDLE (not Python 3) by clicking Menu > Programming > Python 2. Use File > New Window to create a new program and save it as myprogram.py. You are going to need 3 modules, the minecraft.py and block.py modules from the api library and the standard time module so you can introduce delays into your program. Type the following code into your program to import the modules. import mcpi.minecraft as minecraft import mcpi.block as block import time Use the Minecraft python library to create a connection to the game, your program will use this to talk to Minecraft and make things happen. mc = minecraft.Minecraft.create() Using the minecraft connection, mc, you can interact with the game and send the player a message. Then use time.sleep() put in a 5 seconds delay into your program. mc.postToChat("Hello Minecraft World") time.sleep(5) Make sure Minecraft is still running and that you are ‘in a world’, press ESC to return to the main menu so you can run your program. Run the program by clicking 'Run, Run Module' in IDLE. Switch back to Minecraft to see the results of your hard work - be quick though as its only on the screen for a few seconds, if you miss it, re-run the program and try again. Any errors will be shown in red text in the Python Shell, take care to make sure you have typed the code in correctly, be careful of upper and lower case letters as Python is case sensitive.

Return to “Beginners”