0

I am writing a python library with the following file structure:

main_folder
    |
    main.py
    |
    module_subfolder/
    |    |
    |   module.py
    |
    resources/
         |
        credential.txt

in main.py there is the following function:

def connect():
    cred_fp = 'resources/credential.txt' #note this path is relative 
    client = connect_to_client(cred_fp)

    return client

In module.py I have the following import statement and function:

import sys
sys.path.append(absolute/path/to/main.py)

import main

def do_thing_with_client():
    client = main.connect()
    #does thing
    print('done doing thing')

When I run module.py on its own for testing purposes, I get FileNotFoundError: [Errno 2] No such file or directory: 'resources/credential.txt'

I fixed the problem by changing the file path in main.py to an absolute path, but it feels like there should be a way to keep the path relative, which I need to do. I am new to the process of creating well-organized and portable libraries/modules.

spacecat
  • 138
  • 1
  • 11

0 Answers0