使用setuptools打包并使用pip安装后,Python代码看不到数据文件

时间:2018-10-15 06:30:53

标签: python setuptools setup.py

我已经用setuptools打包了一些python代码。该代码使用一个数据文件,但是在使用pip安装该软件包之后,该代码找不到该数据文件。我在做什么错了?


下面是文件结构和文件内容。可以找到文件here。 python代码setup.py试图使用mesa/a.txt数据文件。如果我不打包+粘贴代码,则代码可以正常工作。

|
|- tm/
|  |- __init__.py
|  |- test2.py
|- mesa/
|  |- __init__.py
|  |- a.txt
|- MANIFEST.in
|- setup.py

MANIFEST.in

include mesa/a.txt

setup.py

from setuptools import setup
setup(name='bobab',
      version='0.1',
      py_modules=['tm.test2'],
      author_email='x@gmail.com',
      package_data = {
        'tm': ['mesa/a.txt']
        },
    )

'mesa/a.txt

hello world!

tm/test2.py

import os

def main():
    print 'hi' 
    print open(os.path.join('..', 'mesa', 'a.txt'), 'r').read() # print file content

if __name__ == "__main__":
    main()

我使用命令python setup.py sdist创建程序包。我使用以下命令来安装软件包:

unzip bobab-0.1.zip
cd bobab-0.1
python setup.py install

该包看不到数据文件mesa/a.txt,如以下错误消息所示:

Python 2.7.12 (default, Dec  4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
>>> import tm.test2
>>> tm.test2.main()
hi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "tm/test2.py", line 5, in main
    print open(os.path.join('..', 'mesa', 'a.txt'), 'r').read()
IOError: [Errno 2] No such file or directory: '../mesa/a.txt'

我使用Python 2.7。


下面是包装和安装日志。有趣的是,打包日志中提到数据文件mesa/a.txt已添加到软件包中,但安装日志中没有提到它的存在。

包装日志:

C:\Users\Franck\Documents\GitHub\misc\src\test>python setu
p.py sdist
running sdist
running egg_info
creating bobab.egg-info
writing bobab.egg-info\PKG-INFO
writing top-level names to bobab.egg-info\top_level.txt
writing dependency_links to bobab.egg-info\dependency_links.txt
writing pbr to bobab.egg-info\pbr.json
writing manifest file 'bobab.egg-info\SOURCES.txt'
reading manifest file 'bobab.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'bobab.egg-info\SOURCES.txt'
warning: sdist: standard file not found: should have one of README, README.rst,
README.txt

running check
warning: check: missing required meta-data: url

warning: check: missing meta-data: either (author and author_email) or (maintain
er and maintainer_email) must be supplied

creating bobab-0.1
creating bobab-0.1\bobab.egg-info
creating bobab-0.1\mesa
creating bobab-0.1\tm
copying files to bobab-0.1...
copying MANIFEST.in -> bobab-0.1
copying setup.py -> bobab-0.1
copying bobab.egg-info\PKG-INFO -> bobab-0.1\bobab.egg-info
copying bobab.egg-info\SOURCES.txt -> bobab-0.1\bobab.egg-info
copying bobab.egg-info\dependency_links.txt -> bobab-0.1\bobab.egg-info
copying bobab.egg-info\pbr.json -> bobab-0.1\bobab.egg-info
copying bobab.egg-info\top_level.txt -> bobab-0.1\bobab.egg-info
copying mesa\a.txt -> bobab-0.1\mesa
copying tm\__init__.py -> bobab-0.1\tm
copying tm\test2.py -> bobab-0.1\tm
Writing bobab-0.1\setup.cfg
creating 'dist\bobab-0.1.zip' and adding 'bobab-0.1' to it
adding 'bobab-0.1\MANIFEST.in'
adding 'bobab-0.1\PKG-INFO'
adding 'bobab-0.1\setup.cfg'
adding 'bobab-0.1\setup.py'
adding 'bobab-0.1\bobab.egg-info\dependency_links.txt'
adding 'bobab-0.1\bobab.egg-info\pbr.json'
adding 'bobab-0.1\bobab.egg-info\PKG-INFO'
adding 'bobab-0.1\bobab.egg-info\SOURCES.txt'
adding 'bobab-0.1\bobab.egg-info\top_level.txt'
adding 'bobab-0.1\mesa\a.txt'
adding 'bobab-0.1\tm\test2.py'
adding 'bobab-0.1\tm\__init__.py'
removing 'bobab-0.1' (and everything under it)

安装日志:

(sedona) dernonco@ilcompn0:/mnt/ilcompn0d1/user/dernonco/temp/bobab-0.1$ python setup.py install
running install
running bdist_egg
running egg_info
writing bobab.egg-info/PKG-INFO
writing top-level names to bobab.egg-info/top_level.txt
writing dependency_links to bobab.egg-info/dependency_links.txt
reading manifest file 'bobab.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'bobab.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build
creating build/lib.linux-x86_64-2.7
creating build/lib.linux-x86_64-2.7/tm
copying tm/__init__.py -> build/lib.linux-x86_64-2.7/tm
copying tm/test2.py -> build/lib.linux-x86_64-2.7/tm
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/tm
copying build/lib.linux-x86_64-2.7/tm/__init__.py -> build/bdist.linux-x86_64/egg/tm
copying build/lib.linux-x86_64-2.7/tm/test2.py -> build/bdist.linux-x86_64/egg/tm
byte-compiling build/bdist.linux-x86_64/egg/tm/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/tm/test2.py to test2.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying bobab.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying bobab.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying bobab.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying bobab.egg-info/pbr.json -> build/bdist.linux-x86_64/egg/EGG-INFO
copying bobab.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/bobab-0.1-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing bobab-0.1-py2.7.egg
Copying bobab-0.1-py2.7.egg to /mnt/ilcompn0d1/user/dernonco/pyenv/sedona/lib/python2.7/site-packages
Adding bobab 0.1 to easy-install.pth file

Installed /mnt/ilcompn0d1/user/dernonco/pyenv/sedona/lib/python2.7/site-packages/bobab-0.1-py2.7.egg
Processing dependencies for bobab==0.1
Finished processing dependencies for bobab==0.1

2 个答案:

答案 0 :(得分:1)

应调整几件事:

  1. MANIFEST.in仅在打包源dists时使用,因此python setup.py sdist将包括mesa/a.txt,即使当前设置也是如此。二进制分布不是这种情况,bdist_eggbdist_wheel都不会读取MANIFEST.in
  2. package_data通常用于放置在软件包中的非python文件,但是mesa不在tm目录中,因此相对路径是错误的。您可以通过设置虚线路径来规避此问题:

    package_data={'tm': ['../mesa/a.txt']}
    

    但是,如果mesatm软件包的一部分,则将其放置在所属位置是很有意义的。

  3. os.path.join('..', 'mesa', 'a.txt')将解析相对于当前目录的路径,因此仅当您位于tm目录中时,此行才能找到文件-安装软件包后,几乎不会案子。您需要正确解析路径,例如针对__file__属性:

    os.path.join(os.path.dirname(__file__), '..', 'mesa', 'a.txt')
    

其他说明:

  1. 您可以安全地将py_modules=['tm.test2']替换为packages=['tm'],并且在向py_modules包中添加其他模块时,不必担心更新tm
  2. setuptools提供了一个名为pkg_resources的模块,该模块为通过package_data安装的非python文件提供了有用的管理功能。例如,

    open(os.path.join('..', 'mesa', 'a.txt'), 'r').read()
    

    可以替换为

    pkg_resources.resource_string('tm', '../mesa/a.txt')
    

    如果您想了解更多信息,请参考setuptools文档中的ResourceManager API部分。

答案 1 :(得分:0)

当我使用文件调用不同文件夹中的其他文件时,我在文件顶部使用此代码。

from os.path import dirname, join, abspath
sys.path.insert(0, abspath(join(dirname(__file__), '..')))

然后

from file_name import value