Open a file inside the package directory instead of opening from current directory
<p>I have created a Python package and installed locally. With using the command <code>pip install .</code> .In my package it's necessary to open a file like this.</p>

<pre><code>open('abc.txt','r+')
</code></pre>

<p>But my problem is it's try to open the file in the working directory instead of package installed directory.I think absolute path not going to solve my problem.   </p>

<p>So my question is, How open a file inside the package ? </p>

<p><strong>NB</strong>: While i searched about it saw that <code>os.sys.path</code> may help. But i didn't get any clear solution.</p>

<p>Thank you,</p>

<p>Try: </p>

<pre><code>import os
import inspect  

def open_file(filename):
  pkg_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
  return open(pkg_dir + "/" + filename,'r+')
</code></pre>

python python-packaging