Text file not being imported with module

1.2k Views Asked by At

I'm working on a project, and have the following directory structure:

Classifier_Code/
    classifier/
        __init__.py
        Event.py
        classifier.py
        lib/
            __init__.py
            categories.txt
    test.py

The file __init__.py inside lib/ reads a list of several hundred category names from categories.txt and stores them in a list, which I then want to import into classifier.py using the statement

from lib import CATEGORY_LIST

However, when doing so, I get the following error:

Traceback (most recent call last):
  File "classifier/classifier.py", line 1, in <module>
    from lib import CATEGORIES
  File "classifier/lib/__init__.py", line 3, in <module>
    with open('categories.txt') as categories:
IOError: [Errno 2] No such file or directory: 'categories.txt'

That is, when I'm trying to import the module, I get an error because the text file categories.txt is going along with it.

Does anyone know a fix for this specific problem?

2

There are 2 best solutions below

1
On

If you just use open('categories.txt') then it's going to look for the text file in the current working directory.

If you instead want it to look in the same directory where __init__.py is located, try changing your open to something like this:

with open(os.path.join(os.path.dirname(__file__), 'categories.txt')) as categories:

Assuming that code is in __init__.py, then __file__ should be the path to __init__.py, and you'll end up with the full path to your text file.

0
On

categories.txt will require a full path hence it cant locate where that is. Python always searches current directory. Try os.getcwd() to get current working directory and then Try os.listdir() to change working directory. This way your file will be recognized.

This link here gives detailed explanation of how Python handles files imports and how it works: Importing files from different folder in Python