当python ttp模块就位时使用pyinstaller exe时出错

ttcibm8c  于 2022-12-05  发布在  Python
关注(0)|答案(1)|浏览(225)

我正在尝试使用pyinstaller将我的.py文件转换为exe文件。.py文件工作正常,但是,在程序转换为.exe文件后,我遇到了一个问题。该问题在下面分享。ttp.lazy_import_functions:无法保存问题,并显示“找不到文件”。
[![在此处输入图像说明][1]][1]
我做了一个搜索在谷歌如果任何类似的错误,它看起来只有一个类似的讨论在github这是不是% 100相同的问题。因为我面临着一个问题时,使用.exe文件。见https://github.com/dmulyalin/ttp/issues/54
但是,我检查了ttp/ttp.py文件,我可以看到后面的lazy_import_函数带有path_to_cache。

```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文件试图ttp.py在_MEIXXXX缓存文件下查找ttp/ www.example.com文件
我实际上已经创建了以下补丁与以下变化在我的ttp.py文件,使.exe文件的工作,但我有几个问题在这里,如果有人解释它,我赞赏它.
我的www.example.com中的更改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/ folder includes www.example.com复制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
2vuwiymt

2vuwiymt1#

从我所看到的,ttp模块试图访问它的文件,并引用了ttp的安装路径,而在pyinstaller捆绑了它之后,它无法使用os模块来访问该路径。
一个比修改模块文件和应用补丁文件更简单的解决方法是使用pyinstaller自己或者手动将ttp模块的安装文件夹复制到bundle输出文件夹中。这样它就可以找到所有在使用pyinstaller进行bundle过程后没有的ttp模块文件。
我也遇到了同样的问题,它帮我解决了。

相关问题