-3

Assuming I am installing a python application from setup.py, and doing so within a virtualenv. Also, assuming that I have a need to provide the application sensitive configurations such as API keys / URIs.

My virtualenv might be in a path such as:

/opt/appname/venv

I believe I want to be able to install a default config file for the user of the app to modify before successful execution.

A default example might be installed to:

/etc/appname/config.sample

The problem is, if I am in a virtualenv, setup.py / setuptools really doesn't handle installing into a global path ( as far as I know ).

What would be the best pythonic way to handle this fairly common scenario?

Matt Joyce
  • 2,010
  • 2
  • 20
  • 31
  • Your question is a little vague. What global path are you talking about. A concrete example would be very useful. Please [edit] your question and provide this information. – martineau Aug 17 '16 at 23:38
  • I don't think it's vague at all. A concrete example is impossible as it's a question of pythonic style. – Matt Joyce Aug 18 '16 at 14:48
  • Have you considered the options of alternative installation? https://docs.python.org/3/install/index.html#alternate-installation – creativeChips Aug 22 '16 at 14:43
  • as best as I can tell a post installation script is the best path forward for this, that is what I am doing. I am using RPM for that. Python packaging leaves much to be desired in core functionality. – Matt Joyce Oct 04 '16 at 15:54
  • another option is using a packer on your python env. i've also looked at cython style solutions but solving dependencies is pretty ugly. =/ python is not really good at being packaged... still. – Matt Joyce Oct 24 '16 at 18:58

2 Answers2

1

Assuming the config file alters Virtual Environment Variables and that is why it must be modified before the launch of the virtual environment, then you can modify the activate bash script located in ./venv/bin to accomplish this. So if you want to add a global path from a config file, then last line in activate could be:

MY_GLOBAL_PATH=<program that looks into config file and returns path>

Then the virtual enviroment can be activated as it usual would:

source ./venv/bin/activate
Liam Kelly
  • 3,524
  • 1
  • 17
  • 41
  • I like to leave my python applications as naive about the filesystem (including config files) as possible and use environmental variables instead. I find this is a very clean paradigm when using virtual envs and using the method `os.getenv`. I accomplish this by using the 'VIRTUAL_ENV' variable created by pyvenv to find file inside by project directory or by having an additional bash variable script that contains the locations of config files outside my project dir. If you want to use less bash and more python you can use the `os` module to set and get environment variables. – Liam Kelly Oct 25 '16 at 14:45
  • cfm's allow for the deployment of config files cleanly and in a centrally managed fashion. particularly useful are utilities like encrypted databags / vaults for storying API keys dynamically / securely etc. environment variables can be dangerous as there can be namespace conflicts more easily. IMHO. – Matt Joyce Oct 26 '16 at 18:15
-1

Have you tried using Python's site-specific configuration options? It seems to me like .pth files and a sitecustomize.py file might be helpful.
See also: Accepted answer on "Using .pth files".

Giving more precise detail on what might be done is difficult without a more specific example of the sort of "global path" you mean - even though this is a general style question to some extent.

Community
  • 1
  • 1
Vivian
  • 1,539
  • 14
  • 38