py文件和单个数据文件

py文件和单个数据文件

本文介绍了设置setup.py以打包单个.py文件和单个数据文件,而无需创建任何文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目树:

$.
├── happy_birthday-art.txt
├── happy_birthday.py
├── MANIFEST.in
├── README.rst
└── setup.py

setup.py

from setuptools import setup

setup(
    name='Happy_birthday',
    py_modules=['happy_birthday'],
    data_files=['happy_birthday-art.txt'],
    entry_points={
    'console_scripts': ['happy_birthday = happy_birthday:main', ],},
    long_description=open('README.rst').read(),
)

现在,当我在虚拟环境中执行python setup.py sdist然后pip install创建的.tar.gz文件时,我得到以下消息:

Now when I do python setup.py sdist and then pip install the created .tar.gz file in a virtual environment I get the following message:

warning: install_data: setup script did not provide a directory for 'happy-birthday-art.txt' -- installing right in '/home/username/.virtualenvs/happy_birthday'

程序使用该.txt文件,因此以后尝试运行该文件时会失败.

The program uses that .txt file so it fails when trying to run it afterwards.

但是我不想将happy_birthday-art.txt安装到单独的文件夹中.我想将其安装在安装了happy_birthday.py的文件夹中.另外,我不想在setup.py中使用绝对路径.如何最好地设置我的setup.py文件?

But I don't want to install happy_birthday-art.txt into a separate folder. I want to install it in the folder where happy_birthday.py is installed. Also, I don't want to have to use absolute paths in setup.py. How do I best set up my setup.py file?

推荐答案

如果您具有这样的单文件模块,则不会创建任何文件夹,您的.py文件将直接移至包含其他文件的目录中python模块(例如/usr/lib/pythonX.X/site-packages/).因此,您必须创建目录:

If you have a single-file module like this, no folder will be created, your .py file will be moved directly into the directory which contains the other python modules (/usr/lib/pythonX.X/site-packages/, for example). That's why you have to create a directory:

$ .
|-- happy_birthday/
    |-- __init__.py
    |-- art.txt
|-- MANIFEST.in
|-- README.rst
|-- setup.py

这篇关于设置setup.py以打包单个.py文件和单个数据文件,而无需创建任何文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 05:37