Proper permissions for python installation directory?

a 夏天 提交于 2019-12-25 12:44:54

问题


I'm trying to use a python app on a server for the first time. I started by adding setuptools as root:

[root@server mydirectory]# yum install python-setuptools

Cool. Then I try setup.py:

[user@server mydirectory]$ python setup.py install
running install
error: can't create or remove files in install directory

The following error occurred while trying to add or remove files in the
installation directory:

    [Errno 13] Permission denied: '/usr/lib/python2.4/site-packages/test-easy-install-25752.write-test'

This directory /usr/lib/python2.4/site-packages is owned by root, so that makes sense.

My question is, should I chmod the site-packages directory, or should I be running setup.py as root?


回答1:


The traditional way to install stuff system-wide as a non-root user is to use sudo. Which is why you see things like this all over the Python docs:

sudo python setup.py install

Some people prefer to instead make the site-packages group-writable by some "dev" group so you don't need to do this. (This is effectively what the Mac package manager Homebrew does.)


Alternatively, you can install into per-user site packages. Not every project can do this, but anything based on modern setuptools should be able to do so.

And, while we're at it, if you're installing stuff based on modern setuptools, it's probably better to pip install . instead of python setup.py install anyway. That will, among other benefits, create egg-info files so the package can be detected as a dependency, uninstalled, etc.

See the Python Packaging User Guide for more information.


Finally, you may want to consider using a virtual environment. With Python 3.3+, this is built in as venv, although it doesn't have its own pip until 3.4. With earlier versions of Python, you can install virtualenv off PyPI.

Many hosted server environments for Python (2.x or 3.x) come with virtualenv pre-installed. If not, installing it system-wide will of course require you to be root… but after that, you will be able to install (most) other packages into per-project virtual environments instead of system-wide.




回答2:


Installing packages with pip/easy_install and running directly setup.py files require root privileges because they read/write in those restricted folders.

Usually hosts like www.openshift.com support a virtualenv for you so you just activate it and you have your own per-user environment. Affecting the global site-packages is usually forbidden since it may be a shared host.

In my experience, in a local ubuntu-installed laptop, I have two options:

  1. Run installs as sudo
  2. Run installs in a virtualenv

perhaps your host, if shared, supports virtualenv. Try asking them if it doesn't support it.



来源:https://stackoverflow.com/questions/21840483/proper-permissions-for-python-installation-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!