Permission Error When Trying to Use PyInstaller
Asked Answered
P

14

13

When trying to deploy a Kivy App using PyInstaller, I am getting Permission Denied errors even when using and Administrator CMD. The folder has all open editing options for every user. Python has firewall access. How to fix this?

PS C:\Users\theguy\Documents\Python\myapp-build> python -m PyInstaller --name "MyApp" "./"
422 INFO: PyInstaller: 3.3.1
422 INFO: Python: 3.6.5
424 INFO: Platform: Windows-10-10.0.17134-SP0
425 INFO: wrote C:\Users\theguy\Documents\Python\myapp-build\myapp.spec
426 INFO: UPX is not available.
427 INFO: Extending PYTHONPATH with paths
['C:\\Users\\theguy\\Documents\\Python',
 'C:\\Users\\theguy\\Documents\\Python\\myapp-build']
428 INFO: checking Analysis
428 INFO: Building Analysis because out00-Analysis.toc is non existent
429 INFO: Initializing module dependency graph...
432 INFO: Initializing module graph hooks...
434 INFO: Analyzing base_library.zip ...
3859 INFO: running Analysis out00-Analysis.toc
3861 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\Anaconda3\python.exe
4398 INFO: Caching module hooks...
4402 INFO: Analyzing C:\Users\btdav\Documents\Python\
Traceback (most recent call last):
  File "C:\Anaconda3\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Anaconda3\lib\site-packages\PyInstaller\__main__.py", line 101, in <module>
    run()
  File "C:\Anaconda3\lib\site-packages\PyInstaller\__main__.py", line 94, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "C:\Anaconda3\lib\site-packages\PyInstaller\__main__.py", line 46, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "C:\Anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 791, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "C:\Anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 737, in build
    exec(text, spec_namespace)
  File "<string>", line 16, in <module>
  File "C:\Anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 213, in __init__
    self.__postinit__()
  File "C:\Anaconda3\lib\site-packages\PyInstaller\building\datastruct.py", line 161, in __postinit__
    self.assemble()
  File "C:\Anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 415, in assemble
    priority_scripts.append(self.graph.run_script(script))
  File "C:\Anaconda3\lib\site-packages\PyInstaller\depend\analysis.py", line 201, in run_script
    self._top_script_node = super(PyiModuleGraph, self).run_script(pathname)
  File "C:\Anaconda3\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1334, in run_script
    with open(pathname, 'rb') as fp:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\theguy\\Documents\\Python\\myapp-build'
Partitive answered 10/8, 2018 at 2:46 Comment(0)
P
9

Trick is to make a new directory outside of the one you are building, and then make the build reference the main.py of the Kivy app like in the code below. This fixes permission errors, even from a regular CMD.

PS C:\Users\theguy\Documents\Python\myapp-build> python -m PyInstaller --name "MyApp" "C:\Users\theguy\Documents\Python\myapp\main.py"
Partitive answered 10/8, 2018 at 15:15 Comment(1)
I've found through trial-and-error that the important thing is not to change it to a different directory per se, but to a different directory that you know requires no special permissions.Discrepant
E
7

I met the same problem!

I figure out it is my antivirus software(Bitdefender) thinks the write out .exe is dangerous, so it block the action.

After I add my work folder to antivirus software Exclusions then reboot computer, it works.

Update:

You need to "Code Signing" your .exe file to make antivirus software think it is less dangerous.

In Windows:

Using OpenSSL to create self-signing, then using Win10 SDK signtool.exe

OpenSSL Command Example:

openssl genpkey -out privkey.pem -algorithm RSA -pkeyopt rsa_keygen_bits:4096
openssl req -new -x509 -key privkey.pem -out cert.pem -days 3650
openssl pkcs12 -inkey privkey.pem -in cert.pem -export -out privkey_cert.pfx -passout pass:[PASSWORD]

signtool Command Example:

signtool sign /debug /f privkey_cert.pfx /p [PASSWORD] [EXECUTE_FILE.exe]

Update 2:

Avoid using command with .spec file

$ pyinstaller FILE.spec

Using Auto PY to EXE to instead of .spec

I don't know WHAT happen with .spec file, but it produces single executable file more problems than .py file do

Update 3:

Make sure every time to clear auto-py-to-exe "Building directory" before executing auto-py-to-exe, you may execute ones to get temp folder path.

for example:

Output

Running auto-py-to-exe v2.9.0
Building directory: C:\Users\John\AppData\Local\Temp\tmpevxvuugz
Provided command: pyinstaller --noconfirm --onefile --windowed ...

Update 4:

I reinstall entire windows os, then run the same thing without clear "Building directory" it turn out is fine! I rethink about it is a virus to infect "Building directory" make all this problem.

Essequibo answered 14/6, 2021 at 5:0 Comment(1)
I just had the same issue, turning off real time protection from windows anti-virus fixed it for me. 谢谢你, 我不知道为什么有人贬低你的帖子Gal
D
3

I had this problem when my current directory was set to C:\Users\[User]\Desktop\[Some-Folder].

I solved it by changing my current directory to G:\[Some-Other-Folder].

Presumably, this worked because the latter directory didn't require as many permissions.

TL;DR try changing your current directory to one that you know requires no special permissions.

Discrepant answered 11/11, 2021 at 23:10 Comment(0)
D
2

I'm giving this answer in case anyone encounters my scenario. I spent a good deal of time trying other proposed solutions presented here. However, none resolved my file permission errors. Also, while my solution below works for me, I still don't understand why I'm getting a permission error in the first place.

Note that I use pyenv to manage several distributions, though I don't think that is relevant to the issue. Additionally, Pyinstaller failed in the same way with several different python versions I used it on.

Also, note that I know my answer to modify the package source is the "wrong way to do it", but it worked for my edge case and allowed me to continue forward.

When trying to build a single file executable, I kept getting the PermissionError: [Errno 13] Permission denied issue when PyInstaller attempted to access the created .exe to add the final payload data. As mentioned, I tried all the permission/admin/disable-defender tricks I found in other answers here, but the majority of the time, the build process kept failing with a file permissions error. I did notice, however, that sometimes, every once in a rare while, PyInstaller would build the executable correctly, maybe once out of every twenty times, which I couldn't explain. Finally, on a whim, I decided to modify PyInstaller directly and have the build process try the final append a few extra times.

I modified a PyInstaller source file, api.py, to try the final package append up to ten times. After this change, the build process succeeds on the second or third time. With this change, the first or second append attempt usually fails with the same permissions error, but the second or third works, and the executable package is built correctly.

I changed PyInstallers api.py at line 737:
FROM

else:
    # Fall back to just appending data at the end of the file
    logger.info("Appending %s to EXE", append_type)
    with open(self.name, 'ab') as outf:
        with open(append_file, 'rb') as inf:
            shutil.copyfileobj(inf, outf, length=64 * 1024)

TO

else:
    desparation = 10
    while desparation:
        desparation -= 1
        try:
            # Fall back to just appending data at the end of the file
            logger.info("Appending %s to EXE", append_type)
            with open(self.name, 'ab') as outf:
                with open(append_file, 'rb') as inf:
                    shutil.copyfileobj(inf, outf, length=64 * 1024)
            desparation = 0
        except PermissionError as e:
            logger.error(e)
Dirham answered 14/1, 2022 at 0:29 Comment(2)
This worked for me also! Desperate times require desperate measures ;)Rowena
Thanks. I ended up using this method. I used a virtual environment so I could edit the file and after I'm done I deleted the environment. The source file location if anyone is wondering: .\env\Lib\site-packages\PyInstaller\building\api.pyCordle
G
1

I ran across this same error and found that when I built it in PowerShell with admin rights it worked.

Geibel answered 18/3, 2021 at 2:41 Comment(0)
A
1

One good solution is to uninstall pyinstaller and install it again :

Step I

python.exe -m pip uninstall pyinstaller

Step II

TRY TO GET AN OLDER VERSION of pyinstaller and its dependencies

python.exe pip install pefile==2019.4.18
python.exe -m pip install pyinstaller==3.6

Step III

try to put the files that you are trying to use with pyinstaller into a new folder for example :

C:\Projects

Step IV

Make an executable in one file

C:\Projects>C:\Python27\Scripts\pyinstaller.exe example_v3.py --onefile
Actuary answered 23/2, 2022 at 21:11 Comment(0)
L
1

I encountered this error and solved it by using different version, e.g.

pip install pyinstaller==3.6

That solved my problem.

Lotetgaronne answered 18/3, 2023 at 2:48 Comment(0)
S
0

My environment is pycharm uses the venv environment, and there is always a problem with the last open permission. I change the python environment of pycharm to the system environment and run normally.

Strickland answered 31/7, 2021 at 8:42 Comment(0)
A
0

Use --clean parameter to clean PyInstaller cache and remove temporary files before building.

See : https://pyinstaller.readthedocs.io/en/stable/usage.html#cmdoption-clean

python -m pyinstaller --name "MyApp" "./" --clean

This solved the error for me.

Anachronous answered 4/10, 2021 at 8:2 Comment(0)
E
0

My issue was I was trying to build pyinstaller using a __main__.py inside a folder and invoking pyinstaller on the folder.

Renaming __main__.py to main.py and running pyinstaller on the new main file solved the problem.

Hope this helps.

Extravert answered 12/9, 2022 at 13:10 Comment(0)
M
0

I had the same issue. putting pyinstaller.exe(located in C:\Users\hp\AppData\Roaming\Python\Python311\Scripts) in system variables instead of user variables solved mine.

Machiavelli answered 11/7, 2023 at 23:36 Comment(0)
M
0

I had a file (model.pt) that I had this issue with. I realized I made a mistake during building the app using pyinstaller - I was referencing a file instead of a folder using the --add-data command. Example:

This does not work: pyinstaller --add-data "models/model.pt;models/model.pt" main.py

This does work: pyinstaller --add-data "models;models" main.py (use the folder, not the file)

Mcneese answered 18/3 at 14:54 Comment(0)
C
0

I was having a similar problem. If you are running an antivirus software (AVG in my case), disable all shields momentarily. It worked for me.

Corliss answered 19/4 at 13:59 Comment(0)
H
-2

The error message show "INFO: UPX is not available", you need to import UPX.exe, this problem will be solved.

enter image description here

Hierophant answered 21/5, 2021 at 13:11 Comment(1)
Please try to include all information as text/code and avoid images as far as possible.Lamellar

© 2022 - 2024 — McMap. All rights reserved.