2

I have the following folder structure:

enter image description here

main.py

import pandas as pd

data = pd.read_csv('data.csv')

def add_num_to_env_num():
    print("success")

test_main.py

import sys
sys.path.insert(1, '/src')

import src.main as main


def test_add_num_to_env_num():
    main.add_num_to_env_num()

When I run command python -m pytest I get the following error:

ERROR tests/test_main.py - FileNotFoundError: [Errno 2] No such file or directory: 'data.csv'

I hoped that the line: sys.path.insert(1, '/src') will solve the issue but it didn't. How can I solve the issue?

EDIT: for the real use-case I cannot use absolute paths.

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
Roberto
  • 649
  • 1
  • 8
  • 22
  • 1
    you just have now '/src' in you windows PATH environment variable. Why would it help you? Your file is NOT in '/src' folder but somewher in some/folders/src – Psytho Mar 23 '23 at 14:46
  • is your file "test.csv" or "data.csv" ? – rok Mar 23 '23 at 14:49
  • @rok good eye, I've just corrected it – Roberto Mar 23 '23 at 14:59
  • @Psytho I tried to adapt the solution from here: https://stackoverflow.com/questions/4383571/importing-files-from-different-folder?rq=2 but I know the chances are low, that's why I'm looking for an another solution. – Roberto Mar 23 '23 at 15:13
  • Voting to close as it seems very specific. The error references test.csv but the code references data.csv. The file structure shows . which represents the current folder that tree is being run from, suggesting that 'src' lives in a sub-directory such as `c:\mycode\src` but the sys.path.insert uses '/src' which says that it is off the root e.g. 'c:\src'. poster should verify all of the paths correctly and edit question if needed. – esac Mar 23 '23 at 16:00
  • @esac I corrected the error message. The root of this structure is C: but the point is I will copy-paste this code to other directories or services so it should be independent of location. In the real case I work on I solved this by giving an absolute path but hen after deployment to the service it failed again. I was informed that only relatives paths will work... – Roberto Mar 23 '23 at 16:10
  • Pytest runs fine with the current structure as long as your cwd is the parent directory. It only fails if your cwd is the test directory. However, it is possible to add a more definitive solution to your code if `data.csv` will always be in the same directory as `main.py` by assembling a filepath relative to `main.py` file location. If those two files will always be together in the same folder I can write up an answer for you. – nigh_anxiety Mar 23 '23 at 16:50
  • @nigh_anxiety yes, these two files (main.py and data.csv) are always in the same folder. I'd be grateful for the solution. – Roberto Mar 23 '23 at 16:56

1 Answers1

1

Assuming that data.csv will always be in the same directory, you can write main.py to always load data.csv with a path relative to the location of main.py. The __file__ variable contains the full path of the script currently being executed/imported, so we can get the relative path from that.

This solution uses pathlib.Path, but it is possible with only the tools in the os module as well.

Having a separate datapath variable here isn't necessary, but added for readability.


import pandas as pd
from pathlib import Path

datapath = Path(__file__).parent / Path("data.csv")
data = pd.read_csv(datapath)

def add_num():
    return 1

This related question has multiple answers of other ways to get the script's parent directory and relative path. How do I get the path and name of the file that is currently executing?

nigh_anxiety
  • 1,428
  • 2
  • 4
  • 12