Get package version for conda meta.yaml from source file
Question: I'm trying to reorganize my python package versioning so I only have to update
the version in one place, preferably a python module or a text file. For all
the places I need my version there seems to be a way to load it from the
source `from mypkg import __version__` or at least parse it out of the file as
text. I can't seem to find a way to do it with my conda meta.yaml file though.
Is there a way to load the version from an external source in the meta.yaml
file?

I know there are the git environment variables, but I don't want to tag every
alpha/beta/rc commit that gets tested through out local conda repository. I
could load the python object using `!!python/object` in pyyaml, but conda
doesn't support arbitrary python execution. I don't see a way to do it with
any other jinja2 features. I could also write a script to update the version
number in more than one place, but I was really hoping to only modify one file
as the definitive version number. Thanks for any help.


Answer: There are lots of ways to get to your endpoint. Here's what conda itself
does...

The source of truth for conda's version information is `__version__` in
`conda/__init__.py`. It can be loaded programmatically within python code as
`from conda import __version__` as you suggest. It's also hard-wired into
`setup.py` [here](https://github.com/conda/conda/blob/5a8e020/setup.py#L56)
(note [this
code](https://github.com/conda/conda/blob/5a8e020/setup.py#L25-L31) too), so
from the command line `python setup.py --version` is the canonical way to get
that information.

In 1.x versions of conda-build, putting a line

    
    
    $PYTHON setup.py --version > __conda_version__.txt
    

in `build.sh` would set the version for the built package using our source of
truth. **The`__conda_version__.txt` file is deprecated**, however, and it will
likely be removed with the release of conda-build 2.0. In recent versions of
conda-build, the preferred way to do this is to use `load_setup_py_data()`
within a jinja2 context, which will give you access to all the metadata from
`setup.py`. Specifically, in the `meta.yaml` file, we'd have something like
this

    
    
    package:
      name: conda
      version: "{{ load_setup_py_data().version }}"
    

* * *

Now, how the `__version__` variable is set in `conda/__init__.py`...

What you [see in the source
code](https://github.com/conda/conda/blob/5a8e020/conda/__init__.py#L23) is a
call to the
[`auxlib.packaging.get_version()`](https://github.com/kalefranz/auxlib/blob/8523d3f/auxlib/packaging.py#L144)
function. This function does the following in order

  1. look first for a file `conda/.version`, and if found return the contents as the version identifier
  2. look next for a `VERSION` environment variable, and if set return the value as the version identifier
  3. look last at the `git describe --tags` output, and return a version identifier if possible (must have git installed, must be a git repo, etc etc)
  4. if none of the above yield a version identifier, return `None`

Now there's just one more final trick. In conda's [`setup.py`
file](https://github.com/conda/conda/blob/8523d3f/setup.py#L78-L81), we set
`cmdclass` for `build_py` and `sdist` to those provided by `auxlib.packaging`.
Basically we have

    
    
    from auxlib import packaging
    setup(
        cmdclass={
            'build_py': packaging.BuildPyCommand,
            'sdist': packaging.SDistCommand,
        }
    )
    

These special command classes actually modify the `conda/__init__.py` file in
built/installed packages so the `__version__` variable is hard-coded to a
string literal, and doesn't use the `auxlib.packaging.get_version()` function.

* * *

In your case, with not wanting to tag every release, you could use all of the
above, and from the command line set the version using a `VERSION` environment
variable. Something like

    
    
    VERSION=1.0.0alpha1 conda build conda.recipe
    

In your `build` section meta.yaml recipe, you'll need add a `script_env` key
to tell conda-build to pass the `VERSION` environment variable all the way
through to the build environment.

    
    
    build:
      script_env:
        - VERSION
    

