how to add data files with pyinstalled

12,991 views
Skip to first unread message

Owen Walker

unread,
Mar 9, 2017, 8:53:02 PM3/9/17
to PyInstaller
My Python script, myscript.py, uses several .wav files in the subfolder  /wavFiles located in the same directory as myscript.py.

I have tried adding as part creating of my Windows .exe in two ways. Neither seems to work.

First, I have tried the following command in the myscript folder: "pyinstaller --onefile --add-data.wavFiles;wavFiles myscript.py",which fails.  (Oddly enough, the following works on a Mac: "pyinstaller --onefile --add-data wavFiles:wavFiles myscript.py".)  What is the correct command to give?

2. I have tried the command: "pyi-makespec myscript.py" and in the resulting myscript.spec file I have edited the datas[], line to read:

 datas=[('wavFiles.*.WAV', 'wavFiles')], This also doesn't work.

Help would be appreciated.


Owen Walker


 

davecortesi

unread,
Mar 10, 2017, 11:20:51 AM3/10/17
to PyInstaller

> "pyinstaller --onefile --add-data.wavFiles;wavFiles myscript.py",which fails.

Fails how? When you bundle --onefile it is hard to know what is included because the bundle is unpacked into a temp folder at execution. What was bundled and then unpacked? If you are going by the behavior of the program, e.g. it says it can't find its WAV files, then it could be that the files were included, but the program does not look for them correctly. Have you reviewed this section?

    http://pyinstaller.readthedocs.io/en/stable/runtime-information.html

Suggest you try the --onedir option instead, and poke around in the dist/myscript folder to see what actually got bundled. Also review the log or warnings file in build/myscript/


Owen Walker

unread,
Mar 14, 2017, 4:19:47 PM3/14/17
to PyInstaller
When I run pyinstaller --onefile --add-data.wavFiles;wavFiles myscript.py (or the same with --onedir) I get this message: "--add-data: invalid add-data_or_binary_value:'wavFiles:wavFiles."  This leaves me mystified.

I read   http://pyinstaller.readthedocs.io/en/stable/runtime-information.html
but couldn't find anything helpful that I coul understand.

Thanks,

Owen Walker

David Cortesi

unread,
Mar 14, 2017, 7:02:15 PM3/14/17
to pyins...@googlegroups.com
On Tue, Mar 14, 2017 at 1:19 PM, Owen Walker <owenwa...@gmail.com> wrote:
When I run pyinstaller --onefile --add-data.wavFiles;wavFiles myscript.py (or the same with --onedir) I get this message: "--add-data: invalid add-data_or_binary_value:'wavFiles:wavFiles."  This leaves me mystified.

I'm going to use some redundant spaces and explicit monospaced font for clarity. You say you run


 pyinstaller --onefile  --add-data.wavFiles;wavFiles   myscript.py


I don't think that's right, that period looks wrong to me. Also I hope you are on Windows, otherwise the separator is a colon. So try this:

 pyinstaller --onedir  --add-data  wavFiles;wavFiles   myscript.py

Unless you are NOT on Windows, then

 pyinstaller --onedir  --add-data   wavFiles:wavFiles   myscript.py
 
And if PyInstaller completes, as I said, go into the dist/myscript folder and see what got bundled.


I read   http://pyinstaller.readthedocs.io/en/stable/runtime-information.html
but couldn't find anything helpful that I could understand.

I didn't understand where your operation was failing. I assumed the script was executing but not finding the wavFiles.

Owen Walker

unread,
Mar 18, 2017, 12:49:03 PM3/18/17
to PyInstaller
Thank you, Dave.  I have tried: "pyinstaller  --onedir --add-data wavFiles;wavFiles myscript.py" and it does not produce an error.  Further the folder dist/myscript contains not only myscript.exe but also a copy of wavFiles.  But when you move myscript.exe to a new folder and doublclick on it, the only thing that happens is that a small window appears for a fraction of a second.  (I think it's a command line window but I can't really tell.)  Furthermore, my myscript.py is 1951 kb, the same size as myscript.exe obtained by simply running; "pyinstaller myscript.py".  So I'm not sure what is going wrong and wonder if you have or anyone else has any suggestions. 

David Cortesi

unread,
Mar 18, 2017, 7:11:38 PM3/18/17
to pyins...@googlegroups.com
On Sat, Mar 18, 2017 at 9:49 AM, Owen Walker <owenwa...@gmail.com> wrote:
I have tried: "pyinstaller  --onedir --add-data wavFiles;wavFiles myscript.py" and it does not produce an error.  Further the folder dist/myscript contains not only myscript.exe but also a copy of wavFiles.

So far, so good. When you then run myscript.exe FROM INSIDE THAT FOLDER, e.g.

    C:\my_source_directory: dist\myscript\myscript.exe

...or alternatively, in file explorer you have a window open on dist\myscript and you doubleclick myscript.exe inside it,

it runs how you expect, yes? Finds its wav files and all? Or no?

I'm going to assume "yes" for the rest of this.
 
But when you move myscript.exe to a new folder and doublclick on it,

OK I don't think it is supported to drag myscript.exe OUT of the myscript folder and launch it somewhere else.

You could make an alias of it and drag the ALIAS to the desktop for example, and that should launch.

But in the one-dir model, there is a ton of support stuff in the dir, and the exe has no way to find it if you separate them.

Relax, I know you would rather have a single .exe. The point of using --onedir is that you can easily see just what is being included, and at what level of folder nesting. And now you have.

OK, if you replace --oneDIR with --oneFILE, what gets built is really a kind of a self-extracting archive which contains EXACTLY what is in the dist/myscript folder after a --oneDIR run. When you launch the --onefile executable, it creates a temp directory in temp disk space, and then it unpacks into that temp dir EXACTLY the contents you can see now in the --onedir output. Then it launches the same myscript.exe that you are playing with now.

This is why --onedir mode is useful even if you don't intend to distribute your app as a folder. You can see what is included, which is normally hidden inside the --onefile executable.

So, if you now get the expected operation by launching dist/myscript/myscript.exe, you should get the same results if you make the single change of --onedir to --onefile, rebuild, and launch dist/myscript.exe
 
BTW just to keep from confusing yourself, you should always include the option --clean in every run. That tells it to throw away residual stuff in the dist directory. Otherwise you could get your experiments confused.

Dave Cortesi


Owen Walker

unread,
Mar 19, 2017, 12:40:52 PM3/19/17
to PyInstaller
Dave,

Thank you again for your help.  You are right that when it run myscript.exe within the dist/myscript/ folder, the program works as it should, with wavFiles working.

But, as you acknowledge, what I am after is a final program called "myscript.exe" that I can give to other people that runs properly, and I'm not clear--sorry to be so dumb--what you are telling me to do to accomplish this.  Using aliases (or Windows shortcuts) doesn't seem to work.  If you click on the shortcut, you just get the myscript folder and have to find myscript.exe and click on it.

Could you further enlighten me?

Thanks,

Owen

Steve Barnes

unread,
Mar 19, 2017, 4:43:21 PM3/19/17
to pyins...@googlegroups.com


On 19/03/2017 16:40, Owen Walker wrote:
> Dave,
>
> Thank you again for your help. You are right that when it run
> myscript.exe within the dist/myscript/ folder, the program works as it
> should, with wavFiles working.
>
> But, as you acknowledge, what I am after is a final program called
> "myscript.exe" that I can give to other people that runs properly, and
> I'm not clear--sorry to be so dumb--what you are telling me to do to
> accomplish this. Using aliases (or Windows shortcuts) doesn't seem to
> work. If you click on the shortcut, you just get the myscript folder
> and have to find myscript.exe and click on it.
>
> Could you further enlighten me?
>
> Thanks,
>
> Owen
>
>
On Windows in file explorer click on myscript.exe, right click and
select copy, go to the desktop, right click and select paste shortcut.
This will create a shortcut to the executable in it's original position,
(with it's support files).

--
Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect
those of my employer.

Owen Walker

unread,
Mar 19, 2017, 5:31:58 PM3/19/17
to PyInstaller, gadge...@live.co.uk


On Sunday, March 19, 2017 at 4:43:21 PM UTC-4, Steve Barnes wrote:
No Steve, That doesn't work.  And even if it did, how would you transfer the program to another computer?  I'm looking for a normal windows exe file that can be transferred to another computer and still work.

Owen 

Steve Barnes

unread,
Mar 20, 2017, 12:10:03 AM3/20/17
to Owen Walker, PyInstaller

On 19/03/2017 21:31, Owen Walker wrote:
>
>
>
> No Steve, That doesn't work. And even if it did, how would you
> transfer the program to another computer? I'm looking for a normal
> windows exe file that can be transferred to another computer and
> still work.
>
>
> Owen
>

Owen,

Please don't top post - bottom posting makes things a lot easier to read.

There are two separate issues:

1/ A program WITH its associated files that you can move/copy to another
computer and
2/ Setting up a short cut that will allow you to run the program from
the desktop, start menu, etc.

From your earlier comments, if you use pyinstaller in directory mode
you get the first but have a problem with the second as when you move
the EXE, without its associated files, it unsurprisingly doesn't work.
But if you move the entire directory and run the exe part from within
that directory it does work. PLEASE CONFIRM THIS!

The second point, assuming what I am saying above is true, is to create
short-cuts that allow you to run the exe and for it to find it's
associated files by clicking on a desktop icon. There are a number of
ways that an install program, or the program itself on first run, can do
that but the most common is to create a desktop short cut that tells
Windows:

- Put this icon, with these words, on the desktop,
- When it is double clicked change directory to THIS location and
execute THIS file.

The second can be done programatically but first we need to be sure of
the first point that you can copy the entire directory output by
pyinstaller to another computer and run the exe from within that directory.

Owen Walker

unread,
Mar 20, 2017, 7:07:59 AM3/20/17
to PyInstaller, owenwa...@gmail.com, gadge...@live.co.uk
Steve,

I have moved the build directory to another location on my computer and run  myscript.exe and it works properly.    (I don't have a second computer to try it on, but I'm sure the result would be the same.) Would very much like to know how to do the other things you mention.

Owen 

Steve Barnes

unread,
Mar 20, 2017, 8:17:59 AM3/20/17
to pyins...@googlegroups.com
> run myscript.exe and it works properly. / / (I don't have a second
> computer to try it on, but I'm sure the result would be the same.) Would
> very much like to know how to do the other things you mention.
>
> Owen
>

Owen,

There is a really nice write up at
https://www.blog.pythonlibrary.org/2010/01/23/using-python-to-create-shortcuts/

Have a look there.

David Cortesi

unread,
Mar 20, 2017, 2:08:46 PM3/20/17
to pyins...@googlegroups.com

OK let's wrap this up with a ribbon. So we verified that this...

    pyinstaller --clean -y --oneDIR --add-data.wavFiles;wavFiles myscript.py

created a folder dist/myscript which contains

* all the support files (there's a shit-ton of them, no?)
* the wavFiles the program expects
* an executable myscript.exe

and the latter, when executed in the context of that folder, runs. Yipee!

You have two options. One, distribute the folder.

You make a zip archive of that myscript folder and distribute that to your users. They expand it to get a folder on their desktop or their Downloads folder or wherever, and  you can instruct them to just run myscript.exe in that folder, and they're off.

Or you can instruct them to right-click myscript.exe, select Make alias (or whatever the term is, I don't have a Windows system in front of me to try), and they can drag that alias wherever they like and use that to start your app.

Downside of this is, somewhat more complicated instructions to your users.

One advantage is that you can manually drop other things in the folder before zipping it, like a README file, or a set of "extras" that you want to distribute. Example files maybe. Usage documentation.

Two, you can distribute a single executable:

   pyinstaller --clean -y --oneFILE --add-data.wavFiles;wavFiles myscript.py

Now you should have a self-contained executable dist/myscript.exe and that should also run, without needing a folder full of support files and wav files. (Because it contains all that and re-creates it in temp storage every time you run it.)

You can zip it up and distribute it; advantage it is a single program, simple explanation for users. Disadvantage, your README and your extras, have to be distributed separately.

Owen Walker

unread,
Mar 20, 2017, 3:28:43 PM3/20/17
to PyInstaller
Thank you, Dave, once again for your help.  But I fear the matter hasn't been wrapped with a bow quite yet.

I like your second suggestion, because it is easier for on the recipient than the first.  The only trouble is that when I use the command:

"pyinstaller --clean -y --onefile --add-data wavFiles;wavFiles myscript.py" and I get a single file myscript.exe in the build folder, and when I click on this folder, myscript runs, but the wave files don't work.

What am I doing wrong?

Owen
 

David Cortesi

unread,
Mar 20, 2017, 3:51:56 PM3/20/17
to pyins...@googlegroups.com
On Mon, Mar 20, 2017 at 12:28 PM, Owen Walker <owenwa...@gmail.com> wrote:

I like your second suggestion, because it is easier for on the recipient than the first.  The only trouble is that when I use the command:

"pyinstaller --clean -y --onefile --add-data wavFiles;wavFiles myscript.py" and I get a single file myscript.exe in the build folder, and when I click on this folder, myscript runs, but the wave files don't work.

What am I doing wrong?


The only cause I can image for this -- and anybody else? puh-leeze feel free to jump in here? -- I'm afraid we have been fooling ourselves about the onedir version "working".

It works, but the script is not actually using the wav files that have been nicely bundled into the folder. It must be using the original wav files wherever they are.

And when the script is running in the context of a folder recreated in temp space, it no longer finds the originals -- for reasons we can speculate about if I'm right.

To find out, re-bundle as one dir:  pyinstaller --clean -y --oneDIR --add-data wavFiles;wavFiles myscript.py
 
and then deliberately go into the dist\myscript folder and rename or delete the copied wav files in there. I'm betting dist\myscrip\myscript.exe will still run, because it is finding the original wavFiles\*.wav wherever they are and never did use the bundled ones.






Owen Walker

unread,
Mar 22, 2017, 9:41:50 AM3/22/17
to PyInstaller

Dave,

Thanks again. 

I did what you suggested, entered "pyinstaller --clean -y --oneDIR --add-data wavFiles;wavFiles myscript.py" and rmoved the folder wavFiles from the dist/myscript folder, and the program did not work.
 
I'm not sure what the consequence of this is.

Owen

David Cortesi

unread,
Mar 22, 2017, 9:56:24 AM3/22/17
to pyins...@googlegroups.com

 
> I'm not sure what the consequence of this is.

Unfortunately the consequence is that PyInstaller is working fine, your --onedir bundle is properly bundling the .wav files and the bundled script is looking for them in the correct place, the bundle folder. When you move them, it fails. So it was looking there, and there they were, all correct.

Which makes it (to me)  inexplicable why the --onefile bundle does not work. All it does is reproduce that bundle folder in temp disk space and run from there.

Just for fun, re-make that onedir bundle (or just put the wav files back); verify it works; and then drag that folder (dist\myscript) out to the desktop or somewhere else. And verify the script in it still runs when the folder is somewhere else than the dist folder. Obviously it should.

ANYBODY ELSE WANT TO MAKE A SUGGESTION HERE?


Raoul Snyman

unread,
Mar 22, 2017, 1:47:12 PM3/22/17
to pyins...@googlegroups.com

Where is the application looking for the data files?


--
You received this message because you are subscribed to the Google Groups "PyInstaller" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyinstaller...@googlegroups.com.
To post to this group, send email to pyins...@googlegroups.com.
Visit this group at https://groups.google.com/group/pyinstaller.
For more options, visit https://groups.google.com/d/optout.
--

--
Raoul Snyman
082 550 3754
ra...@snyman.info

Reply all
Reply to author
Forward
0 new messages