python - 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')
解决方案
不得不改变
BASE_DIR = Path(__file__).resolve().parent.parent
to
BASE_DIR = os.getcwd()
为了让它从工作目录开始读取,而不管 py 文件的位置或运行程序的 PC。
推荐阅读
- powershell - 如何不显示选定的列?
- reactjs - 将两个json数组合并为一个json数组,其中数据将在react js中完全合并为一个Json
- excel - Excel 表之间的组合的 Power Query 是什么?
- javascript - 使用 jquery 验证器插件仅为 2 个十进制数字 + 点 + 逗号设置正则表达式
- css - 为什么转换属性在绝对位置上设置为“自动”的顶部/底部/左侧/右侧不起作用?
- android - Google Play 控制台 apk 太大
- javascript - 在移动设备上向左/向右滑动事件,例如“点击”
- c# - CosmosDB SQL Api 不持久化枚举值 0
- python - 撞墙后如何翻转图像
- c - 我正在尝试用 C 语言创建一个简单的计算器,它在 scanf 中接受 3 个参数。我无法让计算器使用浮点数