Py installer, cannot add .txt files
<p>I'm fairly new with programming (and so with python) and the stackoverflow question/response system allowed me to resolve all my problems until now. However, I didn't find any post addressing directly my current issue, but have to admit that I don't really know what's wrong. Let me explain.</p>

<p>I'm trying to make an executable file of a .py file using pyinstaller. There's no problem doing it with a simple .py program (using --onefile), but it does not work when it comes to a more complex program that uses other .py and .txt files. I know that I need to modify the spec file and tried many alternatives; adding hidden files for instance.</p>

<p>Here's the files :</p>

<ul>
<li><strong>UpdatingStrategy.py</strong> (the target file to transform in executable)</li>
<li>LPRfunctions.py (UpdatingStrategy.py imports functions from this file)</li>
</ul>

<p>The following .txt files are read by UpdatingStrategy.py</p>

<ul>
<li>Strategy_Observ.txt</li>
<li>Strategy_Problems.txt</li>
<li>Updating_Observ1.txt</li>
<li>Updating_Observ2.txt</li>
<li>Updating_Problems.txt</li>
</ul>

<p>Just to let you know, I'm using python3.5 and windows 10. Tell me if you need some extra information!</p>

<p>Can someone tell me how to modify the spec file in order to make an executable file of UpdatingStrategy.py? And how to actually use the spec file properly? I have read the py installer documentation before but I lack so many key principles that I couldn't make it works.</p>

<p>Thank you so much!</p>

<p>after the line</p>

<pre><code>a = Analysis( ... )
</code></pre>

<p>add </p>

<pre><code>a.datas += [
    ("/absolute/path/to/some.txt","txt_files/some.txt","DATA"),
    ("/absolute/path/to/some2.txt","txt_files/some2.txt","DATA"),
    ("/absolute/path/to/some3.txt","txt_files/some3.txt","DATA"),
]
</code></pre>

<p>then in your program use the following to get the resource path of your txt files</p>

<pre><code>def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.environ.get("_MEIPASS2",os.path.abspath("."))

    return os.path.join(base_path, relative_path)

 ... 


 txt_data = open(resource_path("txt_files/some.txt")).read()
</code></pre>

<p>make sure you build it like <code>python -m PyInstaller my_target.spec</code> ... do not call pyinstaller directly against your .py file after you have edited your spec file or it will overwrite your edited file...</p>

python python-3.x pyinstaller