You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

When running Python model adapters within FEWS, Python should be installed on your system and the dependencies (Python packages) of your adapter as well. The proposed way to do this is to:

  • work in virtual environment
  • organize model adapter code as python package
  • make sure that all dependencies are listed in the setup.py of the package, to allow straightforward installation

Working with virtual environment

A Python virtual environment, put simply, is an isolated working copy of Python which allows you to work on a specific project without worry of affecting other projects. You can have multiple virtual environments on your system, all having their own specific (versions of) packages available. The virtualenv package provides the basic needs, but virtualenvwrapper makes life a bit easier.

 More information on installation and use can be found in the documentation:

virtualenv

virtualenvwrapper

Use virtualenvwrapper-win on a windows system (https://pypi.python.org/pypi/virtualenvwrapper-win)

Create Python package

The python scaffold package (https://pypi.python.org/pypi/Scaffold/0.1.3) helps to create a proper directory structure for your package, following the structure as explained in http://www.scotttorborg.com/python-packaging/minimal.html

Each project you scaffold will create the following directory structure:

/[projectname]/
/[projectname]/setup.py
/[projectname]/bin
/[projectname]/docs
/[projectname]/[projectname]
/[projectname]/[projectname]/__init__.py
/[projectname]/tests
/[projectname]/tests/__init__.py
/[projectname]/tests/[projectname]_tests.py

Both setup.py and [projectname]_tests.py are set up automatically to reference your project name as a module. The rest is up to you! You should put your code file(s) in the

/[projectname]/[projectname]/

 directory and your tests in 

/[projectname]/tests/

Installing Scaffold

You can view the scaffold package on PyPi here: http://pypi.python.org/pypi/Scaffold/0.1.3

To install, simply use pip install scaffold

Running Scaffold

You can run scaffold as a python module:

python -m scaffold -p "projectname" [-d {base directory}]

Picking A Name

Python module/package names should generally follow the following constraints:

  • All lowercase
  • Unique on pypi, even if you don’t want to make your package publicly available (you might want to specify it privately as a dependency later)
  • Underscore-separated or no word separators at all (don’t use hyphens)
  • No labels