1

所以我有一个这样的目录树:

/
  setup.py
  src/
    ui.py
    load.py
    search.py
    parser.py
    img.py
    obj.py
    #..etc
  res/
    img.py

Setup.py 看起来像这样:

#!/usr/bin/env python

import os
import shutil
import sys
import subprocess
import copy

from distutils.core import setup

if "py2app" in sys.argv:
    ## Move the setup script to the src/ folder, then re-run it
    shutil.copy("setup.py", "src")
    newArgv = copy.copy(sys.argv)
    newArgv[newArgv.index("py2app")] = "rerun"
    os.chdir("src")
    subprocess.call(["python"] + newArgv)
elif "rerun" in sys.argv:
    import py2app
    sys.argv[sys.argv.index("rerun")] = "py2app"

    setup(
        options={
            "py2app": {
                "packages": "wx",
                "site_packages": True
            },
        },
        app=["ui.py"],
        package_dir={"": "."})

    ## Go back up a directory level and copy the build and dist folders up as well
    os.chdir('..')
    for fldr in ["build", "dist"]:
        if os.path.exists(fldr):
            shutil.rmtree(fldr)
        shutil.move(os.path.join("src", fldr), ".")

    ## Copy the necessary data directories to their relevant locations in the app file
    for fldr in ["xml", "templates", "data"]:
        print "Copying %s..." % fldr
        shutil.copytree(fldr, os.path.join("dist/ui.app/Contents/", fldr))
    os.remove(os.path.join("src", sys.argv[0]))

(我做奇怪的 subprocess.call forky 事情的原因是因为需要在树的根目录中有 setup.py,但所有源代码都在 src/ 中)

当我运行时python setup.py py2app,它完全完成,但是当我打开 ui.app 时,它给出了以下错误:

Traceback (most recent call last):
  File "/Users/smith/Code/digital-directory/dist/ui.app/Contents/Resources/__boot__.py", line 127, in <module>
    _run()
  File "/Users/smith/Code/digital-directory/dist/ui.app/Contents/Resources/__boot__.py", line 122, in _run
    exec(compile(source, path, 'exec'), globals(), globals())
  File "/Users/smith/Code/digital-directory/dist/ui.app/Contents/Resources/ui.py", line 14, in <module>
    import load
ImportError: No module named load
4

1 回答 1

0

不要把你的 src 放在src. 使用更标准的 Python 项目布局,不要玩弄“subprocess.call forky 东西”。

例如,遵循http://kennethreitz.com/repository-structure-and-python.html上的指南。

于 2012-12-18T23:19:55.713 回答