简体   繁体   English

为什么我不能使用distutils将这些数据文件包含在Python发行版中?

[英]Why can't I include these data files in a Python distribution using distutils?

I'm writing a setup.py file for a Python project so that I can distribute it. 我正在为Python项目编写一个setup.py文件,以便我可以分发它。 The aim is to eventually create a .egg file, but I'm trying to get it to work first with distutils and a regular .zip. 目的是最终创建一个.egg文件,但我试图让它首先使用distutils和常规.zip。

This is an eclipse pydev project and my file structure is something like this: 这是一个eclipse pydev项目,我的文件结构是这样的:

ProjectName
   src
      somePackage
         module1.py
         module2.py
         ...
   config
      propsFile1.ini
      propsFile2.ini
      propsFile3.ini
   setup.py

Here's my setup.py code so far: 到目前为止,这是我的setup.py代码:

from distutils.core import setup

setup(name='ProjectName', 
      version='1.0', 
      packages=['somePackage'],
      data_files = [('config', ['..\config\propsFile1.ini', 
                                '..\config\propsFile2.ini', 
                                '..\config\propsFile3.ini'])]
      )

When I run this (with sdist as a command line parameter), a .zip file gets generated with all the python files - but the config files are not included. 当我运行它(使用sdist作为命令行参数)时,会生成包含所有python文件的.zip文件 - 但不包括配置文件。 I thought that this code: 我以为这段代码:

 data_files = [('config', ['..\config\propsFile1.ini', 
                                    '..\config\propsFile2.ini', 
                                    '..\config\propsFile3.ini'])]

indicates that those 3 specified config files should be copied to a "config" directory in the zip distribution. 表示应将这3个指定的配置文件复制到zip分发中的“config”目录。 Why is this code not accomplishing anything? 为什么这段代码没有完成任何事情? What am I doing wrong? 我究竟做错了什么?

(I have also tried playing around with the paths of the config files... But nothing seems to help. Would Python throw an error or warning if the path was incorrect / file was not found?) (我也尝试过使用配置文件的路径...但似乎没有任何帮助。如果路径不正确/找不到文件,Python会抛出错误或警告吗?)

Create a MANIFEST.in file like this: 像这样创建一个MANIFEST.in文件:

include config\*

( EDIT ) Look here for more information: http://docs.python.org/distutils/sourcedist.html#specifying-the-files-to-distribute 编辑 )在这里查看更多信息: http//docs.python.org/distutils/sourcedist.html#specifying-the-files-to-distribute

I finally got this to work by moving the entire config directory into the src folder. 我终于通过将整个config目录移动到src文件夹中来实现这一点。 This must mean that my paths are off... but as I couldn't seem to find a way to back up a directory ("..\\" didn't make a difference), I'm going to stick with this solution for now. 这必然意味着我的路径已经关闭......但由于我似乎无法找到备份目录的方法(“.. \\”并没有什么区别),我将坚持使用这个解决方案目前。

I guess you have to escape the backslashes in the filenames; 我想你必须逃避文件名中的反斜杠; eg, instead of '..\\config\\whatever' , write '..\\\\config\\\\whatever' , or use the raw string syntax: r'..\\config\\whatever' . 例如,而不是'..\\config\\whatever' ,写'..\\\\config\\\\whatever' ,或使用原始字符串语法: r'..\\config\\whatever'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM