【发布时间】:2022-12-04 22:03:42
【问题描述】:
我正在尝试使用 pyinstaller 将我的 .py 文件转换为 exe 文件。 .py 文件工作正常,但是,在程序转换为 .exe 文件后我遇到了问题。问题在下面共享。 ttp.lazy_import_functions:无法保存文件未找到指示的问题。
[![在此处输入图片描述][1]][1]
如果有任何类似的错误,我在 google 中进行了搜索,看起来 github 中只有一个类似的讨论,这不是 %100 相同的问题。因为我在使用 .exe 文件时遇到问题。见https://github.com/dmulyalin/ttp/issues/54
但是,我检查了 ttp/ttp.py 文件,我可以看到以下带有 path_to_cache 的 lazy_import_functions。
```log.info("ttp.lazy_import_functions: starting functions lazy import")
# try to load previously pickled/cached _ttp_ dictionary
path_to_cache = os.getenv("TTPCACHEFOLDER", os.path.dirname(__file__))
cache_file = os.path.join(path_to_cache, "ttp_dict_cache.pickle")```
正如上图所示,看起来 .exe 文件试图在 _MEIXXXX 缓存文件下找到 ttp/ttp.py 文件。
我实际上已经创建了一个以下补丁,在我的 ttp.py 文件中进行了以下更改以使 .exe 文件工作,但是如果有人解释它,我在这里有一些担忧,我应用了它。
我的 ttp.py 的变化:
print(path_to_python_3x)
if path_to_python_3x:
os.startfile(f"{path_to_python_3x}\\patch.py")
def lazy_import_functions():
"""function to collect a list of all files/directories within ttp module,
parse .py files using ast and extract information about all functions
to cache them within _ttp_ dictionary
"""
_ttp_ = {
"macro": {},
"python_major_version": version_info.major,
"global_vars": {},
"template_obj": {},
"vars": {},
}
log.info("ttp.lazy_import_functions: starting functions lazy import")
# try to load previously pickled/cached _ttp_ dictionary
path_to_temp_file = tempfile.gettempdir()
_MEI_regex = "_MEI.*"
for temp_file in os.listdir(path_to_temp_file):
if re.search(_MEI_regex, temp_file):
path_to_temp_mei = path_to_temp_file +f"\\{temp_file}"
path_to_temp_ttp = f"{path_to_temp_mei}" + "\\ttp"
path_to_cache = os.getenv("TTPCACHEFOLDER", path_to_temp_ttp)
cache_file = os.path.join(path_to_cache, "ttp_dict_cache.pickle")
else:
path_to_cache = os.getenv("TTPCACHEFOLDER", os.path.dirname(__file__))
#print(path_to_cache)
cache_file = os.path.join(path_to_cache, "ttp_dict_cache.pickle")
使用此补丁文件,我将 ttp/ 文件夹(包括 ttp.py)复制到 _IMEXXXX 缓存文件中,以便 .exe 文件找到路径,并且工作正常,谢天谢地。
import os
import sys
import tempfile
import shutil
import re
path_to_python_3x = os.path.dirname(sys.executable)
# print(path_to_python_3x)
# print(os.getcwd())
path_to_site_packages = path_to_python_3x + "\\Lib\\site-packages"
#print(path_to_site_packages)
path_to_site_ttp = path_to_site_packages +"\\ttp"
#print(path_to_site_ttp)
_MEI_regex = "_MEI.*"
_MEI_regex_a_list = []
while True:
path_to_temp_file = tempfile.gettempdir()
for temp_file in os.listdir(path_to_temp_file):
if re.search(_MEI_regex, temp_file):
path_to_temp_mei = path_to_temp_file +f"\\{temp_file}"
_MEI_regex_a_list.append(path_to_temp_mei)
path_to_temp_ttp = os.path.join(path_to_temp_mei, "ttp")
try:
if "ttp" not in os.listdir(path_to_temp_mei):
shutil.copytree(path_to_site_ttp, path_to_temp_ttp)
except Exception as e:
print(e)```
My queires here is that:
1. Why the program does not work when installing with pyinstaller?
2. Why it checks /ttp/ttp.py file under under Temp?
3. Any way to change cache directory when converting with pyinstaller?
4. As you can see, I have a workaround for now. However, I won't work if cache file started to be kept other than Temp/_IMEXXXX. Because my regex string chooses the files startswidth _IME. Not sure if any possiblity here as well.
Thanks in advance!
[1]: https://i.stack.imgur.com/n0H3j.png
【问题讨论】:
-
不确定安装图片时出了什么问题。
标签: python python-3.x pyinstaller exe