编辑:
在评论后完全重写问题。
如果我从解释器运行以下代码,则该代码运行正常,但如果运行与pyinstaller生成的.exe文件相同的程序,则该代码将失败并显示
[Errno 13] Permission denied: 'c:/test/test.docx'
。另外,如果我从 admin cmd窗口运行相同的exe,则会收到相同错误。
这让我发疯了好几个小时。关于如何调试的任何想法? (这是在装有python 3.6的Windows 10企业PC上。)
"""
test.py
"""
import os
import zipfile
file = r'c:/test/test.docx'
print(f"{file} os.path.exists? {os.path.exists(file)}")
print(f"{file} os.path.isfile? {os.path.isfile(file)}")
print(f"open {file}")
with open(file, "rb") as f:
print(f"fread & print first 20 chars:")
print(f.read()[0:20])
如果我使用python运行此代码:
C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\Scripts>..\python test.py
c:/test/test.docx os.path.exists? True
c:/test/test.docx os.path.isfile? True
open c:/test/test.docx
fread & print first 20 chars:
b'PK\x03\x04\x14\x00\x06\x00\x08\x00\x00\x00!\x00\xecq\x1c\xa0\xf0\x01'
现在我生成一个exe文件:
C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\Scripts>pyinstaller --onefile test.py
338 INFO: PyInstaller: 3.6
339 INFO: Python: 3.6.8
340 INFO: Platform: Windows-10-10.0.18362-SP0
344 INFO: wrote C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\Scripts\test.spec
352 INFO: UPX is not available.
358 INFO: Extending PYTHONPATH with paths
['C:\\Users\\<user>\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts',
'C:\\Users\\<user>\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts']
359 INFO: checking Analysis
367 INFO: Building because C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\Scripts\test.py changed
368 INFO: Initializing module dependency graph...
387 INFO: Caching module graph hooks...
427 INFO: Analyzing base_library.zip ...
7130 INFO: Caching module dependency graph...
7305 INFO: running Analysis Analysis-00.toc
7352 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\python.exe
7508 INFO: Analyzing C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\Scripts\test.py
7516 INFO: Processing module hooks...
7517 INFO: Loading module hook "hook-encodings.py"...
7998 INFO: Loading module hook "hook-pydoc.py"...
8008 INFO: Loading module hook "hook-xml.py"...
8519 INFO: Looking for ctypes DLLs
8519 INFO: Analyzing run-time hooks ...
8531 INFO: Looking for dynamic libraries
8763 INFO: Looking for eggs
8764 INFO: Using Python library C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\python36.dll
8765 INFO: Found binding redirects:
[]
8778 INFO: Warnings written to C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\Scripts\build\test\warn-test.txt
8849 INFO: Graph cross-reference written to C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\Scripts\build\test\xref-test.html
8884 INFO: checking PYZ
8890 INFO: Building because toc changed
8890 INFO: Building PYZ (ZlibArchive) C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\Scripts\build\test\PYZ-00.pyz
9629 INFO: Building PYZ (ZlibArchive) C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\Scripts\build\test\PYZ-00.pyz completed successfully.
9664 INFO: checking PKG
9673 INFO: Building because toc changed
9674 INFO: Building PKG (CArchive) PKG-00.pkg
11417 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
11427 INFO: Bootloader C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\lib\site-packages\PyInstaller\bootloader\Windows-32bit\run.exe
11427 INFO: checking EXE
11437 INFO: Building because toc changed
11438 INFO: Building EXE from EXE-00.toc
11445 INFO: Appending archive to EXE C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\Scripts\dist\test.exe
11498 INFO: Building EXE from EXE-00.toc completed successfully.
如果我现在运行.exe:
C:\Users\<user>\AppData\Local\Programs\Python\Python36-32\Scripts>dist\test.exe
c:/test/test.docx os.path.exists? True
c:/test/test.docx os.path.isfile? True
open c:/test/test.docx
Traceback (most recent call last):
File "test.py", line 7, in <module>
with open(file, "rb") as f:
PermissionError: [Errno 13] Permission denied: 'c:/test/test.docx' # <<<<<<<<
[3640] Failed to execute script test
编辑2: 经过更多调查,这种情况发生在使用域级帐户通过“ Azure信息保护”对文件进行加密时。例如,此DLP程序是一种DRM,用于保护公司PC上的Office文件。奇怪的是,运行上面的代码会根据启动方式给出不同的结果。我本以为使用python启动.py并运行coresponding .exe将使用与用户相同的凭据,从而产生相同的结果,但结果不同。
已加密的文件已将FILE_ATTRIBUTE_ENCRYPTED位置1,可以通过以下方式检测到:
def is_encrypted(path):
return os.stat(path).st_file_attributes & stat.FILE_ATTRIBUTE_ENCRYPTED
在我的情况下,用于加密的身份是链接到域的某种东西(因为我找不到关于它的文档,所以还不知道)。