Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main: If user supplies --python, auto-set CONDA_PY environment variable. #662

Merged
merged 1 commit into from
Dec 1, 2015

Conversation

stuarteberg
Copy link
Contributor

To build with a specific python version, the user can try two things:

  1. The CONDA_PY environment variable, e.g. CONDA_PY=27 conda build my-package

    OR

  2. The --python option on the command-line, e.g. conda build --python=2.7 my-package

Internally, both mechanisms set CONDA_PY within conda_build.config. Additionally, os.environ["CONDA_PY"] can also be used, but only if the user used the first mechanism. It would be nice if that also worked in the second case. (For example, that would make the value consistently available to jinja2 templates within meta.yaml, as well as the build.sh environment.)

This little PR just copies config.CONDA_PY (and config.CONDA_NPY, etc.) into os.environ, so the two modules are in sync for these variables.

It allows meta.yaml syntax like this, which doesn't always work in the current version of conda-build:

build:
  string: np{{CONDA_NPY}}py{{CONDA_PY}}_0_foo_bar

@stuarteberg
Copy link
Contributor Author

To better understand this problem, consider this example:

package:
  name: mypackage
  version: "0.1"

build:
  number: 0
  string: py{{ CONDA_PY }}_0

requirements:
  build:
    - python
  run:
    - python

In this case, CONDA_PY=27 and --python=2.7 do not have the same effect on the build string:

$ CONDA_PY=27 conda build mypackage
...
BUILD START: mypackage-0.1-py27_0
...

Looks good so far...

$ conda build mypackage --python=2.7
...
BUILD START: mypackage-0.1-py_0
...

Whoops, I expected to see py27!

@stuarteberg
Copy link
Contributor Author

Ping @msarahan and @groutr:

Sorry to pester you guys with more jinja template stuff today, but can you take a look at this? This is a tiny PR (4 lines) that fixes CONDA_PY, CONDA_R, CONDA_PERL, and CONDA_NPY in meta.yaml jinja templates.

# Using --python, --numpy etc. is equivalent to using CONDA_PY, CONDA_NPY, etc.
# Auto-set those env variables
for var in conda_version.values():
if getattr(config, var):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so I'm clear on this: you are effectively giving priority to any setting passed in with command line stuff, like "--python" over the environment variables, right? I'm ok with that, just want to make sure I understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was already the case. The original behavior was equivalent to the following pseudo-code:

# By default, use os.environ
conda_build.config.Config.CONDA_PY = os.environ['CONDA_PY']

# Override with --python=foo.bar, if available
if args.python:
    conda_build.config.Config.CONDA_PY = args.python

I just want os.environ to also reflect the change. In the pseudo-code, I'm adding a line to the if-block:

# By default, use os.environ
conda_build.config.Config.CONDA_PY = os.environ['CONDA_PY']

# Override with --python=foo.bar, if available
if args.python:
    conda_build.config.Config.CONDA_PY = args.python
    # Also, update os.environ
    os.environ['CONDA_PY'] = args.python

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I guess it's going outside the if-block. But it's equivalent.

# By default, use os.environ
conda_build.config.Config.CONDA_PY = os.environ['CONDA_PY']

# Override with --python=foo.bar, if available
if args.python:
    conda_build.config.Config.CONDA_PY = args.python

# No matter what, make sure os.environ is in-sync with conda_build.config
os.environ['CONDA_PY'] = conda_build.config.Config.CONDA_PY

@groutr
Copy link
Contributor

groutr commented Dec 1, 2015

when you set --python=2.7, does CONDA_PY have a value of 27 or 2.7?

@msarahan
Copy link
Contributor

msarahan commented Dec 1, 2015

OK, well +1 from me. Will check in with @groutr

@stuarteberg
Copy link
Contributor Author

when you set --python=2.7, does CONDA_PY have a value of 27 or 2.7?

CONDA_PY has a value of 27

Edit: That's the original behavior, unchanged in this PR. See here: https://github.com/conda/conda-build/pull/662/files?diff=unified#diff-c79cbf0306ce864b4ea844b108e06d4dL301

@groutr
Copy link
Contributor

groutr commented Dec 1, 2015

Ok. I didn't see where they reformatted the string. +1 from me too.

groutr added a commit that referenced this pull request Dec 1, 2015
main: If user supplies --python, auto-set CONDA_PY environment variable.
@groutr groutr merged commit 2002fd1 into conda:master Dec 1, 2015
@groutr groutr self-assigned this Dec 1, 2015
@stuarteberg
Copy link
Contributor Author

Excellent. Thanks, guys.

@github-actions github-actions bot added the locked [bot] locked due to inactivity label May 18, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked [bot] locked due to inactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants