想用distutils安装我的Python应用并生成“一键”启动文件

0 投票
1 回答
958 浏览
提问于 2025-04-16 14:00

我正在尝试让我的程序可以通过distutils安装。我的最终目标是为Ubuntu用户制作一个.deb安装包。现在主要的问题是如何让一个“一键启动”的文件正常工作。

我的程序使用pygtk和sqlite3来处理图形界面和数据库。我用glade来帮助构建图形界面,所以我的程序和几个glade文件有关联。同时,我还把数据存储在一个.sqlite3文件里。以下是我目前的包结构……

root/
    |_ src/
    |     |_ RTcore/
    |              |_ data/
    |              |      |_ data.sqlite3
    |              |_ ui/
    |              |    |_ main.glade
    |              |    |_ addRecipe.glade
    |              |_ __init__.py
    |              |_ main.py #this is where I store the meat of my program
    |              |_ module.py #recipetrack is supposed to run this module which ties into the main.py
    |_ setup.py
    |_ manifest.in
    |_ README
    |_ recipetrack #this is my launcher script

这是我现在的setup.py文件……

#!/usr/bin/env python

from distutils.core import setup
files = ["Data/*", "ui/*"]
setup(
    name = "RecipeTrack",
    version = "0.6",
    description = "Store cooking recipes and make shopping lists",
    author = "Heber Madsen",
    author_email = "mad.programs@gmail.com",
    url = "none at this time",
    packages = ["RTcore", "RTcore/Data","RTcore/ui"],
    package_data = {"RTcore": files},
    scripts = ["recipetrack"],
    long_description = """Something long and descriptive""",
    )

我的“recipetrack”脚本的代码是……

#!/usr/bin/env python #it appears that if I don't add this then following 2 lines won't work.
#the guide I was following did not use that first line which I found odd.
import RTcore.module

RTcore.module.start()

所以,recipetrack被安装在根目录之外,并且它的权限被改为755,这样系统上的所有用户都可以启动这个文件。一旦启动,recipetrack应该会启动一个在根文件夹里的模块,然后从那里运行main.py,之后一切应该正常。但实际上并不是这样。“recipetrack”确实启动了模块,然后导入了main.py的类,但在这个时候,程序尝试加载数据文件(比如data.sqlite3、main.glad或addRecipe.glad),然后就卡住了,无法找到这些文件。

如果我进入程序的根目录并运行“recipetrack”,程序就能正常运行。但我希望能在系统的任何位置都能运行“recipetrack”。

我认为问题出在setup.py文件的package_data那一行。我尝试使用data_files,但这也不行,安装时卡住了,无法找到数据文件。

希望我说得清楚,有人能帮帮我。

谢谢,
Heber

修改后的setup.py文件……

setup(
      packages = ["RTcore"], 
      package_dir = {"src": "RTcore"}, 
      package_data = {"RTcore": ["Rui/*"]}, 
      data_files = [("Data", ["data.sqlite3"])],
     )

但现在setup没有安装我的data.sqlite3文件。

1 个回答

0

我解决了我之前遇到的主要问题。整体的问题是我的数据文件没有被包含进来。在setup.py文件中,我需要设置以下内容...

setup(
  packages = ["RTcore"], 
  package_dir = {"RTcore": "src/RTcore"}, 
  package_data = {"RTcore": ["ui/*"]}, 
  data_files = [("Data", ["/full/path/data.sqlite3"])],
 )

用这种方式写setup.py,所有东西都安装得很顺利。接下来要解决的难题是,当任何用户运行程序时,如何从系统的任何位置调用数据文件。我用以下命令来实现这个...

dir_path = os.path.dirname(os.path.abspath(__file__))
os.chdir(dir_path)

我现在面临的最后一个问题是,如何为data.sqlite3文件设置全局权限。在Ubuntu 10.10中,distutils会把我的数据文件安装到/usr/local/Data/这个位置。在这个位置,我没有权限去写这个文件。所以我想解决办法是把数据文件安装到用户的主目录。我还在寻找一个跨平台的解决方案来解决这个问题。

撰写回答