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?
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 youropen
to something like this: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.