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

2024-10-02 08:18:33 发布

您现在位置:Python中文网/ 问答频道 /正文

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


以下是文件结构和文件内容。可以在here找到这些文件。python代码setup.py正在尝试使用mesa/a.txt数据文件。如果我不打包+pip,代码就可以正常工作。在

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

MANIFEST.in

^{pr2}$

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'

我使用python2.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

Tags: topybuildinfocreatingtxtegglinux
2条回答

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

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

然后呢

^{pr2}$

应调整以下几点:

  1. MANIFEST.in将仅在打包源dist时使用,因此python setup.py sdist将包括{},即使是在当前的设置中。二进制分布不是这样,bdist_egg和{}都不会读取{}。在
  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'],并且在向tm包中添加更多模块时,不需要关心更新py_modules。在
  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部分。

相关问题 更多 >

    热门问题