Importing Python Module

From FIFE development wiki
Revision as of 14:13, 4 June 2012 by Fifebot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Import Scheme (Since FIFE 0.3.0)

Because of some re-organization we have been doing with FIFE 0.3 we found a need to change the way fife gets imported into Python projects. This change affects all Python based FIFE clients. Currently you would add the <FIFE>/engine/swigwrappers/python and <FIFE>/engine/extensions directory to your Python path and import FIFE and it's extensions like this:

 import fife_compat
 import fife, fifelog
 from basicapplication import ApplicationBase
 import pychan
 import pychan.widgets as widgets
 from fife_utils import getUserDataDirectory

We have moved things around now to make FIFE more like a regular Python module. When you install FIFE as a Python module it will place it in your Python's site-packages directory just like all other python modules. To keep users site-packages directory clean we decided to place all of FIFE's python related code/objects within the site-packages/fife directory. The new directory structure looks similar to the <FIFE>/engine/python directory (in the build_system_rework branch) like this:


Because of the new directory structure to accomplish the same imports as given above you would have to do this:

 from fife import fife
 from fife.extensions import fife_compat, fifelog
 from fife.extensions.basicapplication import ApplicationBase
 from fife.extensions import pychan
 from fife.extensions.pychan import widgets
 from fife.extensions.fife_utils import getUserDataDirectory

Using FIFE without Installing

If you wish to use fife as a python library without installing fife to your common Python installation directory (usually named site-packages or dist-packages) then you will need to add or modify the PYTHONPATH environment variable


On windows you can right click on My Computer and then click Properties. Navigate to the Advanced tab and click on the Environment Variables button. Look for PYTHONPATH in the lists of current environment variables. If not found you will need to click on New to create a new environment variable and name it PYTHONPATH and add <FIFE>\engine\python to its value. You can create this in either the user variables (only available to this user) or system variables (available to all users). If PYTHONPATH already exists double click on it and append <FIFE>\engine\python to its value.


On linux if you are using a bash shell then type the following command into a terminal window:

 export PYTHONPATH=$PYTHONPATH:<FIFE>/engine/python

Note the above only adds the FIFE path to the PYTHONPATH environment variable for this terminal session. If you want it added to all sessions you will need to put the above code into a location that is sourced automatically such as ~/.bash_profile

If there are any questions feel free to ask in IRC or on the forums!