PIP不会将data_files安装到正确的位置

时间:2018-04-18 15:23:34

标签: python pip setuptools distutils setup.py

我想在Pypi上传我的包,并通过PIP提供。我的软件包需要在/etc//usr/下安装文件(conf文件和数据文件)。

为了实现这一目标,我使用data_files中的setup()属性。

当我通过python setup.py install运行安装时,一切正常,我的文件安装在我想要的地方。

但是当我使用PIP时,我的文件似乎被卡在/usr/local/lib/python2.7/dist-packages下,我不明白为什么。使用PIP时如何正确安装文件?

我的软件包是一个网络工具,默认情况下我需要在/etc/network/下安装文件,所以我不希望用户除了pip install my_package

之外还要做其他事情。

ENV:

$ pip -V
pip 10.0.0 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.3 LTS (Xenial Xerus)"
ID=ubuntu

我已将问题简化为一个非常简单的setup.py文件。

$ tree
.
├── my_package
│   ├── __init__.py
│   ├── my_package.conf
│   ├── my_package.py
│   └── test_file
└── setup.py

1 directory, 5 files

distutils和setuptools都出现了问题

$ cat setup.py
#!/usr/bin/env python

from distutils.core import setup
#from setuptools import setup

setup(
    name='my_package',
    version='1.0',
    description='Test package',
    author='Julien',
    author_email='julien@website.com',
    packages=['my_package'],
    data_files=[
        ('/tmp/', ['my_package/test_file']),
        ('/etc/my_package/', ['my_package/my_package.conf']),
        ('/usr/bin/', ['my_package/my_package.py'])
    ]
)

以下是一些日志(首先通过python setup.py install然后通过pip安装并轻松安装):

$ ll /usr/local/lib/python2.7/dist-packages/
total 36K
-rw-r--r-- 1 root staff  222 Apr 18 16:47 easy-install.pth
drwxr-sr-x 4 root staff 4.0K Apr 17 12:35 pip
drwxr-sr-x 2 root staff 4.0K Apr 17 12:35 pip-10.0.0.dist-info
$ ls /tmp/
$ ll /etc/my_package
ls: cannot access '/etc/my_package': No such file or directory
$ ll /usr/bin/my_package.py
ls: cannot access '/usr/bin/my_package.py': No such file or directory
$
$ python setup.py install
running install
running build
running build_py
creating build
creating build/lib.linux-x86_64-2.7
creating build/lib.linux-x86_64-2.7/my_package
copying my_package/__init__.py -> build/lib.linux-x86_64-2.7/my_package
copying my_package/my_package.py -> build/lib.linux-x86_64-2.7/my_package
running install_lib
creating /usr/local/lib/python2.7/dist-packages/my_package
copying build/lib.linux-x86_64-2.7/my_package/__init__.py -> /usr/local/lib/python2.7/dist-packages/my_package
copying build/lib.linux-x86_64-2.7/my_package/my_package.py -> /usr/local/lib/python2.7/dist-packages/my_package
byte-compiling /usr/local/lib/python2.7/dist-packages/my_package/__init__.py to __init__.pyc
byte-compiling /usr/local/lib/python2.7/dist-packages/my_package/my_package.py to my_package.pyc
running install_data
copying my_package/test_file -> /tmp/
creating /etc/my_package
copying my_package/my_package.conf -> /etc/my_package/
copying my_package/my_package.py -> /usr/bin/
running install_egg_info
Writing /usr/local/lib/python2.7/dist-packages/my_package-1.0.egg-info
$ python /usr/bin/my_package.py
hello world
$ ls /tmp/
  test_file
$ ll /etc/my_package
total 4.0K
-rw-r--r-- 1 root root 9 Apr 18 16:33 my_package.conf

看到它效果很好,现在如果我清理它并再次尝试使用pip:

$ ll /etc/my_package
ls: cannot access '/etc/my_package': No such file or directory
$ python /usr/bin/my_package.py
python: can't open file '/usr/bin/my_package.py': [Errno 2] No such file or directory
$
$
$ pip install . -v
Created temporary directory: /tmp/pip-ephem-wheel-cache-s_jJG_
Created temporary directory: /tmp/pip-install-gm_uo4
Processing /root/my_package
  Created temporary directory: /tmp/pip-req-build-zDFQvX
  Running setup.py (path:/tmp/pip-req-build-zDFQvX/setup.py) egg_info for package from file:///root/my_package
    Running command python setup.py egg_info
    running egg_info
    creating pip-egg-info/my_package.egg-info
    writing pip-egg-info/my_package.egg-info/PKG-INFO
    writing top-level names to pip-egg-info/my_package.egg-info/top_level.txt
    writing dependency_links to pip-egg-info/my_package.egg-info/dependency_links.txt
    writing manifest file 'pip-egg-info/my_package.egg-info/SOURCES.txt'
    warning: manifest_maker: standard file '-c' not found

    reading manifest file 'pip-egg-info/my_package.egg-info/SOURCES.txt'
    writing manifest file 'pip-egg-info/my_package.egg-info/SOURCES.txt'
  Source in /tmp/pip-req-build-zDFQvX has version 1.0, which satisfies requirement my-package==1.0 from file:///root/my_package
Could not parse version from link: file:///root/my_package
Building wheels for collected packages: my-package
  Created temporary directory: /tmp/pip-wheel-s5MYDt
  Running setup.py bdist_wheel for my-package ...   Destination directory: /tmp/pip-wheel-s5MYDt
  Running command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-req-build-zDFQvX/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/pip-wheel-s5MYDt --python-tag cp27
  running bdist_wheel
  running build
  running build_py
  installing to build/bdist.linux-x86_64/wheel
  running install
  running install_lib
  creating build/bdist.linux-x86_64
  creating build/bdist.linux-x86_64/wheel
  creating build/bdist.linux-x86_64/wheel/my_package
  copying build/lib.linux-x86_64-2.7/my_package/__init__.py -> build/bdist.linux-x86_64/wheel/my_package
  copying build/lib.linux-x86_64-2.7/my_package/my_package.py -> build/bdist.linux-x86_64/wheel/my_package
  running install_data
  creating build/bdist.linux-x86_64/wheel/my_package-1.0.data
  creating build/bdist.linux-x86_64/wheel/my_package-1.0.data/data
  creating build/bdist.linux-x86_64/wheel/tmp
  copying my_package/test_file -> build/bdist.linux-x86_64/wheel/tmp/
  creating build/bdist.linux-x86_64/wheel/etc
  creating build/bdist.linux-x86_64/wheel/etc/my_package
  copying my_package/my_package.conf -> build/bdist.linux-x86_64/wheel/etc/my_package/
  creating build/bdist.linux-x86_64/wheel/usr
  creating build/bdist.linux-x86_64/wheel/usr/bin
  copying my_package/my_package.py -> build/bdist.linux-x86_64/wheel/usr/bin/
  running install_egg_info
  running egg_info
  creating my_package.egg-info
  writing my_package.egg-info/PKG-INFO
  writing top-level names to my_package.egg-info/top_level.txt
  writing dependency_links to my_package.egg-info/dependency_links.txt
  writing manifest file 'my_package.egg-info/SOURCES.txt'
  warning: manifest_maker: standard file '-c' not found

  reading manifest file 'my_package.egg-info/SOURCES.txt'
  writing manifest file 'my_package.egg-info/SOURCES.txt'
  Copying my_package.egg-info to build/bdist.linux-x86_64/wheel/my_package-1.0.egg-info
  running install_scripts
  creating build/bdist.linux-x86_64/wheel/my_package-1.0.dist-info/WHEEL
done
  Stored in directory: /tmp/pip-ephem-wheel-cache-s_jJG_/wheels/50/f9/70/af45a262303e11843fbd0173011769c87b1ae21d2f1f39bf42
  Removing source in /tmp/pip-req-build-zDFQvX
Successfully built my-package
Installing collected packages: my-package

Successfully installed my-package-1.0
Cleaning up...
$
$
$ python /usr/bin/my_package.py
python: can't open file '/usr/bin/my_package.py': [Errno 2] No such file or directory
$ ll /etc/my_package
ls: cannot access '/etc/my_package': No such file or directory
$
$
$
$
$ ll /usr/local/lib/python2.7/dist-packages/
total 56K
-rw-r--r-- 1 root staff  195 Apr 18 17:04 easy-install.pth
drwxr-sr-x 3 root staff 4.0K Apr 18 17:08 etc
drwxr-sr-x 2 root staff 4.0K Apr 18 17:08 my_package
drwxr-sr-x 2 root staff 4.0K Apr 18 17:08 my_package-1.0.dist-info
drwxr-sr-x 4 root staff 4.0K Apr 17 12:35 pip
drwxr-sr-x 2 root staff 4.0K Apr 17 12:35 pip-10.0.0.dist-info
drwxr-sr-x 3 root staff 4.0K Apr 17 16:47 testresources
drwxr-sr-x 2 root staff 4.0K Apr 17 16:47 testresources-2.0.1.dist-info
drwxr-sr-x 2 root staff 4.0K Apr 18 17:08 tmp
drwxr-sr-x 3 root staff 4.0K Apr 18 17:08 usr
$ tree /usr/local/lib/python2.7/dist-packages/etc/
/usr/local/lib/python2.7/dist-packages/etc/
└── my_package
    └── my_package.conf

1 directory, 1 file
$ tree /usr/local/lib/python2.7/dist-packages/usr/
/usr/local/lib/python2.7/dist-packages/usr/
└── bin
    ├── my_package.py
    └── my_package.pyc

1 directory, 2 files
$ tree /usr/local/lib/python2.7/dist-packages/tmp/
/usr/local/lib/python2.7/dist-packages/tmp/
└── test_file

0 directories, 1 file
$

使用easy_install

$ easy_install . -v
Processing .
Writing /root/my_package/setup.cfg
Running setup.py -q bdist_egg --dist-dir /root/my_package/egg-dist-tmp-_2Ocor
error: Setup script exited with error: SandboxViolation: open('/tmp/test_file', 'wb') {}

The package setup script has attempted to modify files on your system
that are not within the EasyInstall build area, and has been aborted.

This package cannot be safely installed by EasyInstall, and may not
support alternate installation locations even if you run its setup
script by hand.  Please inform the package's author and the EasyInstall
maintainers to find out if a fix or workaround is available.

我真的被困在这里,如果有人能帮助我解决这个问题,我将非常感激! :)

谢谢!

0 个答案:

没有答案