'Permission Error When Trying to Use PyInstaller
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'
Solution 1:[1]
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.
Solution 2:[2]
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.
Solution 3:[3]
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)
Solution 4:[4]
I ran across this same error and found that when I built it in PowerShell with admin rights it worked.
Solution 5:[5]
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.
Solution 6:[6]
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.
Solution 7:[7]
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
Solution 8:[8]
The error message show "INFO: UPX is not available", you need to import UPX.exe, this problem will be solved.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Asker |
| Solution 3 | |
| Solution 4 | leo |
| Solution 5 | qinyao |
| Solution 6 | Avinash Kumar |
| Solution 7 | betelgeuse |
| Solution 8 | Steven02120 |
