How to check if Python works from virtualenv during package installation

I have Python software that contains a configuration file and a man page. To install them, I have the following line in mine setup.py(as described in http://docs.python.org/2/distutils/setupscript.html#installing-additional-files ):

data_files = [('/etc/foo', ['foo.conf']), ('/usr/share/man/man1', ['foo.1'])]

This works fine when I want to install the software as root using python setup.py install, but of course it doesn’t work in virtualenv, since the user is not allowed to write on /etcand /usr/share/man.

What is the best way to fix this? Check VIRTUAL_ENVin the current environment and just not install these files at all? The software will search foo.confin the local directory, so this should not be a problem. The user will skip the man page, but there is no reasonable way to set it anyway, because manit will not look for it somewhere near virtualenv.

+5
source share
1 answer

Ultimately, it seems that your question is really about how to determine if running Python is running in virtualenv. To understand this, we must understand how it works virtualenv.

When you run the activatescript inside virtualenv, it performs two functions:

  • PATH, bin virtualenv, python virtualenv.
  • VIRTUAL_ENV, script .

python virtualenv, python VIRTUAL_ENV. , python , .

, sys sys.prefix. , virtualenv , Python, , .

Python virtualenv : sys real_prefix, , Python, , , Python , , virtualenv:

import sys

if getattr(sys, "real_prefix", None) is not None:
    print "Maybe in a virtualenv"
else:
    print "Probably not in a virtualenv"

. , python , . , /usr/share/man - (, ) , :

  • Python , - /home/johnd/local-python, real_prefix , Python lib , , /etc /usr/share/man

  • , /usr/lib/python2.7 , Python, .

, , , , data_files , virtualenv. , , , , . , , , virtualenv, .

+9

All Articles