1

My program has the next structure:

|---main.py
|---home
      |----read.csv
      |----importer.py

main.py has the next code:

from home import importer as imp
imp.load()

importer.py has the next code:

import pandas as pd
import sys


def load():
    arch = pd.read_csv("read.csv")
    print arch


if __name__ == '__main__':
    load()

and read.csv is any csv, it is for example

h,k
3,4
5,6

When I run importer.py, it runs ok, and that's because read.csv is in the current path.

But when I try to run main.py, it throws an exception, because it can't find read.csv. It's because read.csv is not in the current path.

I need it works in both modes, and the solution would be to append the home path to the python main program.

I tried to do this sys.path.append(os.path.join(os.getcwd(), "home")) but it did'nt work.

Any idea?

3
  • 1
    you have to use absolute paths not relative paths. i suggest having load take in a path parameter and passing in a absolute path every time. Commented Nov 9, 2015 at 22:02
  • Do you have __init__.py in your home directory? Commented Nov 9, 2015 at 22:31
  • Yes, I have init.py in home Commented Nov 9, 2015 at 22:39

1 Answer 1

1

This works

def load():
    filedir, _ = os.path.split(__file__)
    arch = pd.read_csv(os.path.join(filedir, 'read.csv'))
    print arch
Sign up to request clarification or add additional context in comments.

1 Comment

I think then that you have to specify the full path. It works but I'd liked to be able to add this path so you always have access to every file in home.

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.