User Tools

Site Tools


python

This is an old revision of the document!


Embedded Python

Introducing Python

Mystic BBS has an embedded Python engine which allows for an incredible amount of flexibility when it comes to creating modifications to BBS functionality, or even creating your own games.

Custom Python scripts can be executed by a menu command on any menu, and can also replace any prompt in a Theme's prompts file so that the script is executed instead of the prompt that normally we have been displayed. Python scripts can also be executed by MPL scripts and even other Python scripts as well.

Why Python 2

For those familiar with Python, it is important to note that Mystic uses the 2.x style of Python. Even though there is a 3.0 version, the 2.x line was selected for a pretty long list of reasons. Here are some of them:

  • Better package support for Python 2.x
  • Python 2 is the default Python version used in the latest LTS versions of Ubuntu
  • Python 2 is the default Python version used in the latest of Apple's OS X
  • Proven stability over many years in production environments
  • The Python 2 syntax will not change, causing your scripts to break while the same cannot be said about Python 3

Installation and Requirements

Mystic requires that Python 2.7 is installed on your system if you wish to execute Python scripts. The presence of Python is detected automatically by Mystic, and is initialized as needed. It is not in any way a dependency and Mystic will continue to operate normally without it should you decide you do not wish to use it.

Most Linux desktop distributions and Apple's macOS will come with Python 2.7 installed by default, and in many cases no installation will be required in order to use Python. In Windows, Python needs to be installed by downloading it from the Website: www.python.org

Note: If you are using the 32-bit version of Mystic (even if it is in a 64-bit operating system), then you need the 32-bit version of Python installed. If you are using the 64-bit version of Mystic then you need to have the 64-bit version of Python installed.

Library Validation

When initializing Python, Mystic will be attempting to locate the following library depending on operating system:

Windows: python27.dll
Linux: libpython2.7.so.1.0
MacOS: libpython2.7.dylib

In Windows, you should probably know if Python is installed already since it has to be manually installed.

In Linux environments, you can usually determine if Python is installed by executing “python –version” in a terminal window. The version should be 2.7. You can also check to see if you have the proper library installed by searching the results of ldconfig:

ldconfig -p | grep libpython2.7.so.1.0

If the above command does not find anything but “python –version” prints 2.7, then you may have Python installed under a different library name. Execute the same command with just the base library name to locate where Python is installed:

ldconfig -p | grep libpython

The result will tell you where Python 2.7 is installed. If Python 2.7 is installed under a different library name, then you will need to create a symbolic link to that filename using “libpython2.7.so.1.0” so Mystic can find it.

Environment Variables

In addition to the basic Python installation, some environment variables may need to be set so that Mystic can find and import 3rd party modules used within your Python programs. In many cases the default installation will work just fine and no modifications will be needed. However, if an error occurs when executing modules, then the environment variables may need to be set or adjusted.

One common error reported when the environment variables need adjusted or set is the message “Importerror: No module named site” printed to STDOUT.

PYTHONHOME and PYTHONPATH are two environment variables which need to be set. Depending on your operating system, Python may already be installed and working without making any changes. As an example, if you have Python installed to C:\PYTHON27 in Windows, you'd want to set environment variables as such:

 SET PYTHONHOME=C:\PYTHON27
 SET PYTHONPATH=C:\PYTHON27\LIB
 SET PATH=%PYTHONHOME%;%PATH%

For more information on how to set environment variables, you'll need to research the specific operating system that you are using and the Python documentation. For reference: Here is what the Python documentation says about these environment variables:

   PYTHONHOME
  
   Change the location of the standard Python libraries. By default, the
   libraries are searched in prefix/lib/pythonversion and
   exec_prefix/lib/pythonversion, where prefix and exec_prefix are
   installation-dependent directories, both defaulting to /usr/local.
  
   When PYTHONHOME is set to a single directory, its value replaces both
   prefix and exec_prefix. To specify different values for these, set
   PYTHONHOME to prefix:exec_prefix.
  
   PYTHONPATH
  
   Augment the default search path for module files. The format is the same
   as the shell’s PATH: one or more directory pathnames separated by
   os.pathsep (e.g. colons on Unix or semicolons on Windows). Non-existent
   directories are silently ignored.
  
   In addition to normal directories, individual PYTHONPATH entries may
   refer to zipfiles containing pure Python modules (in either source or
   compiled form). Extension modules cannot be imported from zipfiles.
  
   The default search path is installation dependent, but generally begins
   with prefix/lib/pythonversion (see PYTHONHOME above). It is always
   appended to PYTHONPATH.
  
   An additional directory will be inserted in the search path in front of
   PYTHONPATH as described above under Interface options. The search path
   can be manipulated from within a Python program as the variable sys.path
python.1495907953.txt.gz · Last modified: 2017/05/27 12:59 by g00r00

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki