Skip to main content

Pythonized CC Tweaked (ComputerCraft) API

Warning: CPython can't build safe sandboxes for arbitrary untrusted code (read more). Never use code in this repo if you don't trust your players!

  1. Before you start Minecraft, enable localhost in mod server config

    In case of singleplayer it's located inside your saves folder. In case of multiplayer check your server folder.

    Edit computercraft-server.toml

    [[http.rules]]
    	host = "$private"
    	action = "allow"  # change here deny to allow
    
  2. Install & start python language server

    python -m pip install computercraft
    python -m computercraft.server
    
  3. Start Minecraft, open up any computer and type:

    wget http://127.0.0.1:8000 py
    py
    

    Now you have python REPL in computercraft! To quit REPL type exit() and press enter.

    To run any program:

    py program.py  # relative to current dir
    py /path/to/program.py
    

py is short Lua program that interacts with the server. cc module contains almost everything as is in ComputerCraft documentation:

from cc import disk, os

disk.eject('right')
print(os.getComputerLabel())

Opening a file:

from cc import fs

with fs.open('filename', 'r') as f:
    for line in f:
        print(line)

Waiting for event (os.captureEvent instead os.pullEvent):

from cc import os

timer_id = os.startTimer(2)
for e in os.captureEvent('timer'):
    if e[0] == timer_id:
        print('Timer reached')
        break

Using modems:

from cc import peripheral

modem = peripheral.wrap('back')
listen_channel = 5
# this automatically opens and closes modem on listen_channel
for msg in modem.receive(listen_channel):
    print(repr(msg))
    if msg.content == 'stop':
        break
    else:
        modem.transmit(msg.reply_channel, listen_channel, msg.content)

Using parallel:

from cc import parallel, os

def fn():
    os.sleep(2)
    print('done')

parallel.waitForAll(fn, fn, fn)

Importing in-game files as modules:

from cc import import_file

p = import_file('/disk/program.py')  # absolute
m = import_file('lib.py', __file__)  # relative to current file

More examples can be found in this repository: examples/ and tests/cc_programs/

Metadata

Release files for computercraft 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for computercraft 0.4.0
File Interpreter ABI Platform Actions
computercraft-0.4.0-py3-none-any.whl Python 3 none any Details for computercraft-0.4.0-py3-none-any.whl

Release files / computercraft-0.4.0-py3-none-any.whl

Download URL computercraft-0.4.0-py3-none-any.whl
Size 46.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
661f116e361a6e5016f471b12b8e8a21e8e6512cd2c3e99ca85134ad5bf4aaf0
BLAKE2b-256 checksum
How to use checksums
57594d909e35d8ec56a7f9defd6f23c8e22630f26c99840d61b772534a6f76cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Security for computercraft 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.4.0 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page