NetLogo banner

Home
Download
Help
Resources
Extensions
FAQ
NetLogo Publications
Contact Us
Donate

Models:
Library
Community
Modeling Commons

Beginners Interactive NetLogo Dictionary (BIND)
NetLogo Dictionary

User Manuals:
Web
Printable
Chinese
Czech
Farsi / Persian
Japanese
Spanish

  Donate

NetLogo Models Library:
Code Examples/Extensions Examples/py

(back to the library)

Python Basic Example

[screen shot]

Note: If you download the NetLogo application, every model in the Models Library is included.

WHAT IS IT?

This model shows how to use NetLogo's py extension to interact with Python. This extension allows users with Python installed on their computer to write Python code in their NetLogo model and to share data back and forth between the Python and NetLogo.

THINGS TO NOTICE

First, the model sets up Python with the py:setup and py:python primitives. The default python found on the system will be used. This step is required for models using the Python extension. If you do not have Python installed on your computer, you will need to install it on your computer outside of NetLogo. Once the Python environment is setup, it's available until the model is closed.

This model uses a few basic Python packages, sys, math, and os, to get information and do calculations that would be difficult or impossible in NetLogo. The packages are made available by using py:run to execute the Python import commands. The key to interacting with the packages is using py:set to set a variable value and pass data into the Python extension and py:runresult to get data back out of Python to use in NetLogo.

THINGS TO TRY

Check out the Python math package documentation and see if there are any other calculations it can do that NetLogo doesn't natively support and try to make them work.

EXTENDING THE MODEL

While the uses demonstrated are convenient when needed, the real power of Python is in its scientific packages, like numpy, pandas, and scikit-learn and more.

RELATED MODELS

There is a bundle of other Python extension demo models, py-demo-models.zip, available on the Python extension site.

(back to the NetLogo Models Library)