Setting up setup.py for packaging of a single .py

2020-05-20 07:14发布

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:

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.

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?

2条回答
冷血范
2楼-- · 2020-05-20 07:40

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
查看更多
萌系小妹纸
3楼-- · 2020-05-20 07:41

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."

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":

data_files=[('share/doc/foo', ['happy_birthday-art.txt'])],
查看更多
登录 后发表回答