首页 > 解决方案 > Python程序在终端中工作,但在使用PyInstaller制作成EXE时不工作

问题描述

我制作了一个程序,它将为各种基于 HTML 的网站创建一个预制文件和文件夹的目录。

Python 程序在从 VS Code 启动时运行良好,但是当我使用 pyinstaller 将其转换为 .exe 时,它​​说找不到模板“我打印的错误表达式”。

它会创建文件夹结构,但不会将 .css、.html 或 .js 文件创建到分配的文件夹中。我想我已经把它缩小到它是我的文件路径在创建 .exe 时以某种方式改变的东西。

我认为问题出在某个地方:

    BASE_DIR = Path(__file__).resolve().parent.parent
    template_path = os.path.join(BASE_DIR, 'FileRes') 

任何和所有的建议将不胜感激。

def createBasicStructure(self, projName):
        # create our dir structure
        try:
            os.makedirs(self.location.get() + '/' + projName)
            print("Directory " , projName ,  " Created ")
        except FileExistsError:
            print("Directory " , projName ,  " already exists")

        if not os.path.exists(self.location.get() + '/' + projName):
            print("Directory " , projName ,  " no such directory")
        else:    
            os.makedirs(self.location.get() + '/' + projName + '/assets/images')
            os.makedirs(self.location.get() + '/' + projName + '/css')
            os.makedirs(self.location.get() + '/' + projName + '/scripts')
            os.makedirs(self.location.get() + '/' + projName + '/res')

        input_files = ['basic.html', 'css/basic.css', 'scripts/basic.js']
        BASE_DIR = Path(__file__).resolve().parent.parent
        template_path = os.path.join(BASE_DIR, 'FileRes')
        
        for file in input_files:
            try:
                # Copy sample file
                input_file = open(template_path + '/' + file, 'r')
                content = input_file.read()
                input_file.close()
                # Write content
                try:
                    path = self.location.get() + '/' + projName + '/' + file
                    print(path)
                    output_file = open(path, 'w')
                    output_file.write(content)
                    output_file.close()                   
                    # print('\nfile:', path, '\nsuccessfully created')
                except:
                    print('\nFile could not be written')
            except:
                # new Err
                print('\nTemplate file could not be found')

标签: pythonpyinstaller

解决方案


不得不改变

       BASE_DIR = Path(__file__).resolve().parent.parent
                          
                           to 

       BASE_DIR = os.getcwd()

为了让它从工作目录开始读取,而不管 py 文件的位置或运行程序的 PC。


推荐阅读