使用cx_Freeze冻结成可执行文件后如何知道当前文件路径?
How to know the current file path after being frozen into an executable using cx_Freeze?
我在 Python 制作游戏。由于我使用的是 Python3,因此我必须将我的脚本 cx_Freeze 转换为可执行文件,以让其他没有 Python3 运行 游戏的人使用。我已将资源(纹理、音频、文件等)放在与脚本路径相关的文件夹中。当我没有cx_Freeze脚本时,我使用__file__
定位脚本并使用它来定位资源并使用它们。但是一旦用cx_Freeze冻结,这个方法就不行了。
我的问题是,使用cx_Freeze冻结后如何找到脚本的文件路径?
我试过 __file__
、os 模块和任何其他与文件路径相关的模块。
path = str(__file__).split("/")
path.remove("My executable name")
path.remove("MacOS")
path = "/".join(path) + "/Resources/"
我希望 return 我的 Resources 文件夹,但是 __file__
只有 return 我的主目录所以这会导致回溯。
cx_Freeze 文档中有一节 Using data files 专门讨论这个问题,说:
Applications often need data files besides the code, such as icons. Using a setup script, you can list data files or directories in the include_files
option to build_exe
. They’ll be copied to the build directory alongside the executable. Then to find them, use code like this:
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
An alternative is to embed data in code, for example by using Qt’s resource system.
如果您使用的是 cx_Freeze 版本 5.1.0 或 5.1.1,还有一点需要注意:程序包将包含在冻结应用程序构建目录的子目录 lib
中,而它们位于解冻应用程序的主目录或 Python 安装的 site-packages
目录中。如果您从主应用程序引用位于包目录中的数据文件,则需要使用例如将此附加 lib
添加到相对路径os.path.join()
(或者反方向删除)。
然而,在你的情况下,我猜 Resources
只是一个数据目录,而不是带有 Python 代码的包。那么你只需要在设置脚本中将 'Resources'
添加到 include_files
列表中。要在 Resources
目录中获取名为 filename
的文件,像这样修改的示例代码应该可以完成工作:
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
datadir = os.path.dirname(__file__)
# The following line has been changed to match where you store your data files:
return os.path.join(datadir, 'Resources', filename)
我在 Python 制作游戏。由于我使用的是 Python3,因此我必须将我的脚本 cx_Freeze 转换为可执行文件,以让其他没有 Python3 运行 游戏的人使用。我已将资源(纹理、音频、文件等)放在与脚本路径相关的文件夹中。当我没有cx_Freeze脚本时,我使用__file__
定位脚本并使用它来定位资源并使用它们。但是一旦用cx_Freeze冻结,这个方法就不行了。
我的问题是,使用cx_Freeze冻结后如何找到脚本的文件路径?
我试过 __file__
、os 模块和任何其他与文件路径相关的模块。
path = str(__file__).split("/")
path.remove("My executable name")
path.remove("MacOS")
path = "/".join(path) + "/Resources/"
我希望 return 我的 Resources 文件夹,但是 __file__
只有 return 我的主目录所以这会导致回溯。
cx_Freeze 文档中有一节 Using data files 专门讨论这个问题,说:
Applications often need data files besides the code, such as icons. Using a setup script, you can list data files or directories in the
include_files
option tobuild_exe
. They’ll be copied to the build directory alongside the executable. Then to find them, use code like this:def find_data_file(filename): if getattr(sys, 'frozen', False): # The application is frozen datadir = os.path.dirname(sys.executable) else: # The application is not frozen # Change this bit to match where you store your data files: datadir = os.path.dirname(__file__) return os.path.join(datadir, filename)
An alternative is to embed data in code, for example by using Qt’s resource system.
如果您使用的是 cx_Freeze 版本 5.1.0 或 5.1.1,还有一点需要注意:程序包将包含在冻结应用程序构建目录的子目录 lib
中,而它们位于解冻应用程序的主目录或 Python 安装的 site-packages
目录中。如果您从主应用程序引用位于包目录中的数据文件,则需要使用例如将此附加 lib
添加到相对路径os.path.join()
(或者反方向删除)。
然而,在你的情况下,我猜 Resources
只是一个数据目录,而不是带有 Python 代码的包。那么你只需要在设置脚本中将 'Resources'
添加到 include_files
列表中。要在 Resources
目录中获取名为 filename
的文件,像这样修改的示例代码应该可以完成工作:
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
datadir = os.path.dirname(__file__)
# The following line has been changed to match where you store your data files:
return os.path.join(datadir, 'Resources', filename)