0

I have a project that I manage to package, distribute, install and import. My package directory looks something like:

/
pkg/
   __init__.py
   main.py
   data/
       data_1
       data_2.sav
       data_3.bin

The problem is that main.py is dependent on non code files inside the data directory, and when I call main.py, I get the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'data/data_1'

Additional:

  • all data files are being packaged correctly
  • in main.py, the data files are called as:

    data = pickle.load(open('data/data_1', "rb"))

The main script is not finding the data files for some reason. What can possibly be wrong? Any suggestions?

Thanks!!

2
  • Since the file path you use is not absolute, you need to find out what the working directory. Add this somewhere in main.py: print(os.getcwd()) Commented Jan 17, 2020 at 10:56
  • Thanks @FiddleStix - the path I was using was incomplete. Yours and ttekampe suggestions worked well together for me. Commented Jan 17, 2020 at 11:14

2 Answers 2

1

I think the problem is, that the path is interpreted relatively from where you are executing main.py Maybe this works:

import os

this_path = os.path.dirname(__file__)
data =  pickle.load(open(
     os.path.join(this_path, 'data/data_1'),
    "rb"
))
Sign up to request clarification or add additional context in comments.

Comments

0

With python 3.4+, you can use the Pathlib module as below:

from pathlib import Path
path = (Path(__file__).parent / "../data/data_1").resolve()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.