Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Setup.py: set environment variable prior to running install_requires

Recently, a change to Apache Airflow requires setting an environment variable SLUGIFY_USES_TEXT_UNIDECODE=yes before it is able to be installed: https://airflow.apache.org/installation.html

In my custom module's setup.py script, I'm including Airflow in the install_requires list. So, when I try to install my custom module, it also fails looking for that environment variable to be set.

Since I have a lot of environments to install this into, I want to set that environment variable automatically in my setup.py module so it is always present. However, It doesn't seem to work if I simply put this line at the top of my setup.py or inside of the run() method of a custom subclass of install (via the cmdclass setup.py option).

os.environ['SLUGIFY_USES_TEXT_UNIDECODE'] = 'yes'

Any thoughts on how I can set an environment variable in a setup.py before any of the install_requires dependencies are installed?

Any help is much appreciated.

like image 383
Joe J Avatar asked Jan 31 '19 19:01

Joe J


2 Answers

It should work out-of-the-box if you run export SLUGIFY_USES_TEXT_UNIDECODE=yes before pip install YOUR_CUSTOM_PACKAGE.

An alternative option is to use the following in your setup.py:

import os
os.system("export SLUGIFY_USES_TEXT_UNIDECODE=yes")
like image 198
kaxil Avatar answered Oct 19 '22 17:10

kaxil


How is your custom module installed? Using wheels? Then you're out of luck as setuptools run setup.py at compile/package time but not at the installation time. With wheels the only solution is to set the environment variable before installation:

SLUGIFY_USES_TEXT_UNIDECODE=yes pip install …

You trick with setup.py should work if you install from sdist (source distribution).

like image 42
phd Avatar answered Oct 19 '22 17:10

phd