0

Im tring to make a CLI app following this tutorial: https://trstringer.com/easy-and-nice-python-cli/, my file system has the following structure:

app/
├── README.md
├── install.sh
├── app
  ├── __init__.py
  ├── __main__.py
  ├── text.txt
  ├── text1.txt
  └── text2.json
#the app.egg info was created automatically
├── app.egg-info
  ├── entry_points
  ├── SOURCES
└── setup.py

when I call the app from the terminal I get this error: FileNotFoundError: [Errno 2] No such file or directory: 'text1.txt', even when its clearly in the directory. The code were I call that text file worked before I tried to convert the script to a CLI app, not sure if I have to move those text file to an especific dir or especify that I need those files somewhere.

Here setup.py :

from setuptools import setup
setup(
    name = 'app',
    version = '0.1.0',
    packages = ['app'],
    entry_points = {
        'console_scripts': [
            'app = app.__main__:some_function'
        ]
    })

If you need more information about the code or have an alternative on how to turn my python script into a functional CLI app don't hesitate to let me know! I'm also using click.

AMM
  • 186
  • 2
  • 14
  • https://stackoverflow.com/q/7522250/7976758, https://docs.python.org/3/distutils/setupscript.html#installing-package-data, https://packaging.python.org/guides/using-manifest-in/, https://setuptools.readthedocs.io/en/latest/userguide/datafiles.html – phd Jul 08 '21 at 09:43

1 Answers1

1

Without seeing your script, I'm guessing a little here, but I suspect the problem is that you're doing something like "open('file1.txt')" and it works fine when you are calling the program from within the same directory as file1.txt. However, when file1.txt is in a package, you can't find its path that way.

I'm pretty sure this is what you need. You'll want to read through and follow the link to the ResourceManager API.

https://setuptools.readthedocs.io/en/latest/userguide/datafiles.html

Matt Blaha
  • 921
  • 6
  • 9
  • I read what you linked and tried implementing it, here what I did: `packages = find_packages("app"), package_dir={"": "app"}, package_data={"": ["*.txt","*.json"]}, exclude_package_data={"": ["README.txt"]},` But I still get the same error, I only added the code above to *setup.py*, am I missing something else? – AMM Jul 08 '21 at 05:00
  • Within the script being packaged. Once it is packaged, it can use the ResourceManager API to find other packaged files, including data files. https://setuptools.readthedocs.io/en/latest/pkg_resources.html#resourcemanager-api – Matt Blaha Jul 09 '21 at 15:17