A Hello World Python Repository

python-hello_world

This repository contains an example of a minimal Python package.
The setup.py file is central, it uses setuptools to package the python code.
In the following we consider that a Conda environment has been created and activated.
You can for instance use the development environment described in the conda/environment.yaml file.
To do so, type the following command line

conda env create -f conda/environment.yaml

To create a Python package that can be used in this environment, three options are possible:

  1. A develop mode.
    Using this mode, all local modifications of source code will be considered in your Python interpreter (when restarted) without any post-installation.
    This is particularly useful when adding new features.
    To install this package in develop mode, type the following command line
python setup.py develop --prefix=${CONDA_PREFIX}

Once this step is done, type the following command line for running tests

nosetests test -x -s -v

Note that this require to have installed nose in your environment.

  1. An install mode.
    Using this mode, all local modifications of source code will NOT be considered in your Python interpreter (when restarted).
    To consider your local modifications of source code, you must install the package once again.
    This is particularly useful when you want to install a stable version in one Conda environment while creating another environment for using the develop mode.
    To install this package in develop mode, type the following command line
python setup.py install --prefix=${CONDA_PREFIX}
  1. A release mode.
    Using the mode, a Conda package will be created from the install mode and can be distributed with Conda.
    To build this package with Conda (with conda-build installed), type the following command line
conda build conda/recipe -c defaults --override-channels

Then, you can upload the generated package or just install it.
To install this conda package, type the following command line

conda install python-hello_world -c local -c defaults --override-channels

With the previous modes, the Conda environment doesn’t know that this python package has been installed.
But with this method, the python-hello_world will appear in your Conda package listing.

Once the package is installed, it can be used from the Python interpreter with namespaces defined by your directories containing __init__.py files.
Here, we can print the "Hello World" message using the following commands in the Python interpreter

import hello_world

hello_world.func()

Moreover, in the hello_world/cli.py we use the argparse Python module to define command line tools declared both in the setup.py and the meta.yaml of the Conda recipe (entry points).
So whatever the mode chosen, to display this message you could also type in your terminal

hello_world

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.