设置setup.py以包装单个.py文件和单个数据文件,而无需创建任何文件夹

时间:2021-06-23 13:29:28

Project tree:

$.
├── 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(),
)

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:

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

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

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

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

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?

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

2 个解决方案

#1


16  

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:

如果您有这样的单文件模块,将不会创建任何文件夹,您的.py文件将直接移动到包含其他python模块的目录(/usr/lib/pythonX.X/site-packages/,for例)。这就是你必须创建一个目录的原因:

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

#2


3  

http://docs.python.org/2/distutils/setupscript.html

"You can specify the data_files options as a simple sequence of files without specifying a target directory, but this is not recommended, and the install command will print a warning in this case. To install data files directly in the target directory, an empty string should be given as the directory."

“您可以将data_files选项指定为一个简单的文件序列而不指定目标目录,但不建议这样做,并且在这种情况下install命令将打印警告。要直接在目标目录中安装数据文件,请输入空字符串应该作为目录给出。“

However, here the target directory is NOT the site-packages folder, but the prefix folder, that is the root of the venv. If you'd want the .txt reside in the site-packages dir bare, then it would look not only ugly but seem that really not supported. On the other hand it is possible to install it to another location in the env, for example in "share/doc/foo":

但是,这里的目标目录不是site-packages文件夹,而是前缀文件夹,即venv的根目录。如果您希望.txt驻留在site-packages目录中,那么它看起来不仅丑陋而且看起来真的不受支持。另一方面,可以将它安装到env中的另一个位置,例如“share / doc / foo”:

data_files=[('share/doc/foo', ['happy_birthday-art.txt'])],

#1


16  

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:

如果您有这样的单文件模块,将不会创建任何文件夹,您的.py文件将直接移动到包含其他python模块的目录(/usr/lib/pythonX.X/site-packages/,for例)。这就是你必须创建一个目录的原因:

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

#2


3  

http://docs.python.org/2/distutils/setupscript.html

"You can specify the data_files options as a simple sequence of files without specifying a target directory, but this is not recommended, and the install command will print a warning in this case. To install data files directly in the target directory, an empty string should be given as the directory."

“您可以将data_files选项指定为一个简单的文件序列而不指定目标目录,但不建议这样做,并且在这种情况下install命令将打印警告。要直接在目标目录中安装数据文件,请输入空字符串应该作为目录给出。“

However, here the target directory is NOT the site-packages folder, but the prefix folder, that is the root of the venv. If you'd want the .txt reside in the site-packages dir bare, then it would look not only ugly but seem that really not supported. On the other hand it is possible to install it to another location in the env, for example in "share/doc/foo":

但是,这里的目标目录不是site-packages文件夹,而是前缀文件夹,即venv的根目录。如果您希望.txt驻留在site-packages目录中,那么它看起来不仅丑陋而且看起来真的不受支持。另一方面,可以将它安装到env中的另一个位置,例如“share / doc / foo”:

data_files=[('share/doc/foo', ['happy_birthday-art.txt'])],