title: Reading file using relative path in python project
body: <p>Say I have a python project that is structured as follows:</p>
  
  <pre><code>project
      /data
          test.csv
      /package
          __init__.py
          module.py
      main.py
  </code></pre>
  
  <p><code>__init__.py</code>:</p>
  
  <pre><code>from .module import test
  </code></pre>
  
  <p><code>module.py</code>:</p>
  
  <pre><code>import csv
  
  with open("..data/test.csv") as f:
      test = [line for line in csv.reader(f)]
  </code></pre>
  
  <p><code>main.py</code>:</p>
  
  <pre><code>import package
  
  print(package.test)
  </code></pre>
  
  <p>When I run <code>main.py</code> I get the following error:</p>
  
  <pre><code> C:\Users\Patrick\Desktop\project&gt;python main.py
  Traceback (most recent call last):
    File "main.py", line 1, in &lt;module&gt;
      import package
    File "C:\Users\Patrick\Desktop\project\package\__init__.py", line 1, in &lt;module&gt;
      from .module import test
    File "C:\Users\Patrick\Desktop\project\package\module.py", line 3, in &lt;module&gt;
      with open("../data/test.csv") as f:
  FileNotFoundError: [Errno 2] No such file or directory: '../data/test.csv'
  </code></pre>
  
  <p>However, if I run <code>module.py</code> from the <code>package</code> directory I get no errors. So it seems that the relative path used in <code>open(...)</code> is only relative to where the originating file is being run from (i.e <code>__name__ == "__main__"</code>)? I don't want to use absolute paths. What are some ways to deal with this?</p>