如何使用Pyinstaller - cx_Freeze可执行文件sys.MEIPASS / sys.executable

时间:2016-06-30 01:54:01

标签: python executable pyinstaller cx-freeze

我一直在努力为OSX El Capitan提供一个python可执行文件,并且我成功地使用Pyinstaller和cx_Freeze构建了可执行文件,当我在另一个mac上实际运行可执行文件时出现问题。我得到的错误是无法找到我的主脚本中引用的.db文件,所以我查看了两个程序的文档,并遇到了sys.MEIPASS(Pyinstaller)和sys.executable(cx_Freeze)以包含数据文件--onefile应用程序。这是我在主脚本中使用的代码:

def find_data_file(filename):
    if getattr(sys, 'frozen', False):
        # The application is frozen
        datadir = os.path.dirname(sys._MEIPASS) #in cx_Freeze this is sys.executable     
    else:
        # The application is not frozen
        # Change this bit to match where you store your data files:
        datadir = ospath.abspath(os.pardir)

    return os.path.join(datadir, filename)

#This is how im using the "find_data_file" function in my code. 
dbpath = find_data_file('PubData.db')
conn = lite.connect(dbpath)

我在else语句中更改了一下以匹配我的项目目录的布局,并且在运行未冻结的应用程序时它完全正常。 但是,当我尝试使用构建的可执行文件运行时,它给我一个错误,无法找到.db文件,我认为引用sys.MEIPASS或sys.executable将修复。

错误:

Traceback (most recent call last):
  File "interface/GUI.py", line 673, in <module>
  File "interface/GUI.py", line 82, in __init__
  File "interface/GUI.py", line 212, in getServerNames
sqlite3.OperationalError: no such table: servernames

这就是我的文件树的外观:

PubData-master ##(Project Root Directory)
   Interface ##(Directory)
      GUI.py ##(Main Script, this is where i reference 'PubData.db')
      GUI.spec ##(Pyinstaller spec file)
   PubData.db  ## This is my database file, in the PubData-master Directory

如果有人能告诉我我做错了什么,或者给我一个解决方案,我将非常感激!

0 个答案:

没有答案
相关问题