简体   繁体   English

打包脚本时,Python如何获取图像目录的路径?

[英]How can Python get the path to a directory of images when packaging a script?

Here is my script: 这是我的脚本:

current_directory=os.path.abspath(os.curdir)
self.image_array = glob.glob(os.path.join(current_directory,"./data/world_flag_game/flags/*.png")

This works fine when I run it (using quickly) from the terminal but when I package it (using quickly package) it seems that the path is not working. 当我从终端运行(快速使用)它时,效果很好,但是当我打包(使用快速打包)时,该路径似乎不起作用。

If the path is not working, it probably isn't the path where the .png images are located. 如果该路径无效,则可能不是.png图像所在的路径。 Try printing the path from the terminal as well as from your packaged application. 尝试从终端以及打包的应用程序中打印路径。 Something like the following will help you to see why the images are not found. 类似以下内容将帮助您了解为什么找不到图像。

print("%r" % current_directory)

It's not surprising that the application's build directory would be different than its development directory. 毫不奇怪,该应用程序的构建目录与其开发目录会有所不同。 So you may want to replace your os.path.join(...) with an absolute directory to verify that your application works when packaged. 因此,您可能需要用绝对目录替换os.path.join(...)来验证您的应用程序在打包后是否可以正常工作。 Then it's a matter of storing your images into the build directory and then pointing to that location correctly. 然后,只需将图像存储到构建目录中,然后正确指向该位置即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM