[英]error using pyinstaller exe when python ttp module is in place
I am trying convert my.py file to an exe file using pyinstaller.我正在尝试使用 pyinstaller 将 my.py 文件转换为 exe 文件。 The.py file perfectly work fine, however, I am facing an issue after the program is converted to.exe file. .py 文件完全可以正常工作,但是,在程序转换为 .exe 文件后,我遇到了一个问题。 The problem is shared right below.问题在下面共享。 ttp.lazy_import_functions: failed to save problem with File not found indication. ttp.lazy_import_functions:无法保存文件未找到指示的问题。
I did a search in google if any similar error, it looks there is only one similar discussion in github which is not the %100 same problem.如果有任何类似的错误,我在谷歌中进行了搜索,它看起来在 github 中只有一个类似的讨论,这不是 %100 相同的问题。 Because I am facing an issue when using.exe file.因为我在使用 .exe 文件时遇到问题。 See https://github.com/dmulyalin/ttp/issues/54参见https://github.com/dmulyalin/ttp/issues/54
However, I have checked ttp/ttp.py file, I can see following lazy_import_functions with the path_to_cache.但是,我检查了 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")
As it is also shown above picture, it looks that.exe file trying to find ttp/ttp.py file under _MEIXXXX cache file.正如上图所示,看起来 .exe 文件试图在 _MEIXXXX 缓存文件下找到 ttp/ttp.py 文件。
I have actually created a the following patch with the following changes in my ttp.py file to make.exe file work, however I have a few concerns here if someone explain it, I appricated it.我实际上已经创建了一个以下补丁,在我的 ttp.py 文件中进行了以下更改以使 make.exe 文件工作,但是如果有人解释它,我在这里有一些担忧,我应用了它。
Changes in my ttp.py:我的 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")
With this patch file I am copying ttp/ folder includes ttp.py into _IMEXXXX cache file, so that.exe file finds the path, and worked fine, thankfully.使用此补丁文件,我将 ttp/ 文件夹(包括 ttp.py)复制到 _IMEXXXX 缓存文件中,以便 .exe 文件找到路径,并且工作正常,谢天谢地。
import os.path
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 queries here are that:我在这里的查询是:
From what i see, the ttp module tries to access its files and has references to the installation path for ttp which it cannot get to using the os module after its bundled by pyinstaller.据我所知,ttp 模块尝试访问它的文件并引用了 ttp 的安装路径,在它被 pyinstaller 捆绑后无法使用 os 模块。
One simpler workaround than changing the module files and applying the patch file that you did, would be to just copy the installation folders of the ttp module to the bundle output folder using pyinstaller itself or do it manually.一种比更改模块文件和应用您所做的补丁文件更简单的解决方法是使用 pyinstaller 本身将 ttp 模块的安装文件夹复制到 bundle output 文件夹或手动执行。 This way it would find all the ttp module files which it was not after the bundle process using pyinstaller.这样它会找到所有在使用 pyinstaller 的捆绑过程之后不存在的 ttp 模块文件。
I was facing the same issue and it fixed it for me.我遇到了同样的问题,它为我解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.