2

I have a setup code in python using setuptools, and I have the python code to install in /usr/share/mypackage (owned by root), but I'm trying to install the package as a non-privileged user. So, I create a virtual environment (with virtualenv) in a directory $ENVDIR owned by the user. Then, after activating the virtual environment (with source $ENVDIR/bin/activate), I run python /usr/share/mypackage/setup.py install.

The problem is that running setup.py install tries to create a package.egg-info (or something like that) file inside /usr/share/mypackage, which obviously triggers a permission denied error unless I run the setup as root, which is not what I need...

So, the question is: How can I fix this without running setup.py as root? That is, how can I prevent setup.py from writing to /usr/share/mypackage?

Eduardo Bezerra
  • 1,923
  • 1
  • 17
  • 32

1 Answers1

1

You cannot install as a non-priviledged user into /usr/share any workaround would violate unix permissions. To be fair, you should never ever install anything in /usr/share by default. /usr/share is the OS/distro territory and shall be not meddled with when installing software by hand, the correct place is /usr/local/share. Reference: The FHS spec

Packaged software install itself by default on /usr/local but allow for switches to change this path. People that compile distros change that prefix to /usr, without the possibility of changing the prefix an OS would never be compiled! And python and setuptools are no different they have such a prefix switch.

In setuptools you define directories relative to the distribution root. Using absolute paths in setup.py is plain bad. By default the distribution root is what is defined by sys.prefix. Moreover setuptools has the --prefix switch that changes that value from the command line. You shall install your package as a non-priviledged user as:

setup.py --prefix=~/local

Now, a virtualenv activate script sets up the VIRTUAL_ENV environment variable. Which you can use when inside the virtualenv:

setup.py --prefix=$VIRTUAL_ENV

(This is what pip does)


Addendum

I am aware that python sets sys.prefix to '/usr' by default. Therefore encouraging packages to be installed directly into /usr, as opposed to /usr/local. Yet, that still violates the FHS spec, and shall be avoided.

grochmal
  • 2,901
  • 2
  • 22
  • 28
  • Maybe I was unclear... The installation directory is **not** /usr/share. At least it is not supposed to be... When running apt-get install of my debian package, I just put the python source files in /usr/share, but the actual `python setup.py install` is run by the daemon user (I create a user and a group just to run the daemon that I install): in the daemon start script, the call is made to `python setup.py install`, which tries to write to /usr/share, even if I put `python setup.py install --prefix=$ENVDIR`... – Eduardo Bezerra Jul 11 '16 at 10:22