title: What does os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)) mean? python
body: <p>In several SO's question there is these lines to access the parent directory of the code, e.g. <a href="https://stackoverflow.com/questions/10953540/os-path-joinos-path-dirname-file-returns-nothing">os.path.join(os.path.dirname(__file__)) returns nothing</a> and <a href="https://stackoverflow.com/questions/10953540/os-path-joinos-path-dirname-file-returns-nothing">os.path.join(os.path.dirname(__file__)) returns nothing</a></p>
  
  <pre><code>import os, sys
  parentddir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
  sys.path.append(parentddir)
  </code></pre>
  
  <p>I understand that <code>os.path.abspath()</code> returns absolute path of something and <code>sys.path.append()</code> adds the path for the code to access. but what is this cryptic line below, <strong>what does it really mean?</strong></p>
  
  <pre><code>os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
  </code></pre>
  
  <p>Is there another way to achieve the same purpose of appending the parent directory of the where the code?</p>
  
  <p>This problem happens because I am calling functions across directories and sometimes they share the same file name, e.g. <code>script1/utils.py</code> and <code>script2/utils.py</code>. I am calling a function from <code>script1/test.py</code> which calls <code>script2/something.py</code> contains a function that calls <code>script2/utils.py</code> and the following code</p>
  
  <pre><code>script1/
          utils.py
          src/
              test.py
  
  script2/
          utils.py
          code/
              something.py
  </code></pre>
  
  <p><strong>test.py</strong></p>
  
  <pre><code>from script2.code import something
  import sys
  sys.path.append('../')
  import utils
  
  something.foobar()
  </code></pre>
  
  <p><strong>something.py</strong></p>
  
  <pre><code>import os, sys
  parentddir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
  sys.path.append(parentddir)
  import utils
  
  def foobar():
    utils.somefunc()
  </code></pre>