How to set environment variables in Python and use them in a C extension -
i have created python wrapper c library. library requires environment variable (my_data_folder
) point folder data files. data files (in current implementation of wrapper) installed python wrapper when running setup.py, , placed in folder called coefficients
package installation. set environment variables in package's __init__.py
, excecuted whenever package imported:
# set environment variable import os os.path import join, realpath, dirname my_data_folder = join(realpath(dirname(__file__)), 'coefficients') os.environ['my_data_folder'] = my_data_folder # import c extension (mymodule._mymodule) after setting variable above mymodule import _mymodule
the wrapper functions available mymodule._mymodule.myfunction()
. testing package tox, , works well. in case it's useful, testenv
has following commands:
commands = python setup.py clean --all build_ext --force --inplace {posargs:py.test -vv --ignore=src}
however, when install package using python setup.py install
(or develop
or pip install .
or without -e
) , open python shell run wrapper, c library gives error indicating environment variable not set. happens whether run custom code or run py.test -vv --ignore=src
tox does. importable, , c extension run (i.e. it's compiled correctly), environment variable not passed along, hence error.
i have banged head against wall time , have no idea wrong, , apparently neither google. why environment variable available c extension when running tox not when installing normally?
i'm happy provide more information if it's required. entire source here.
update
it works now. seems have been classical case of "struggle few hours, give , leave work, come next day, works." have no idea why, must have done wrong. perhaps environment wasn't clean thought, or forgot activate it.
Comments
Post a Comment