English 中文(简体)
Python app distribution cross-platform
原标题:

I want to distribute my app on OSX (using py2app) and as a Debian package.

The structure of my app is like:

app/
     debian/
            <lots of debian related stuff>
     scripts/
             app
     app/
         __init__.py
         app.py
         mod1/
              __init__.py
              a.py
         mod2/
              __init__.py
              b.py

My setup.py looks something like:

from setuptools import setup
import os
import os.path

osname = os.uname()[0]

if osname ==  Darwin :
    APP = [ app/app.py ]
    DATA_FILES = []
    OPTIONS = { argv_emulation : True}

    setup(
        app=APP,
        data_files=DATA_FILES,
        options={ py2app : OPTIONS},
        setup_requires=[ py2app ],
    )
elif osname ==  Linux :
        setup(
        name = "app",
        version = "0.0.1",
        description = "foo bar",
        packages = ["app", "app.mod1", "app.mod2"],
        scripts = ["scripts/app"],
        data_files = [
            ("/usr/bin", ["scripts/app"]),
       ]
    )

Then, in b.py (this is on OSX):

from app.mod2.b import *

I get:

ImportError: No module named mod2.b

So basically, mod2 can t acccess mod1. On Linux there s no problem, because the python module app is installed globally in /usr/shared/pyshared. But on OSX the app will obviously be a self-contained .app thing built by py2app. I wonder if I approached this totally wrong, are there any best practices when distributing Python apps on OSX?

Edit: I also tried a hack like this in b.py:

from ..mod2.b import *

ValueError: Attempted relative import beyond toplevel package

Edit2: Seems to be related to this How to do relative imports in Python?

最佳回答

I m not sure if this is the best practice or not (I ve not put much python software into proper distribution), but I would just make sure that the top-level app package was in sys.path. Something like putting the following into the top-level __init__.py:

try:
    import myapp
except ImportError:
    import sys
    from os.path import abspath, dirname, split
    parent_dir = split(dirname(abspath(__file__)))[0]
    sys.path.append(parent_dir)

I think that should do the right thing in a cross platform way.

EDIT: As kaizer.se points out this might not work in the __init__.py file, depending on how the code you re invoking is getting executed. It would only work if that file is evaluated. The key is to make sure that the top-level package is in sys.path from some the code that actually is running.

Often times, so that I an execute individual files inside of a package directly (for testing with the if __name__ eq __main__ idiom), I ll do something like place a statement:

import _setup

At the top of the individual file in question, and then create a file _setup.py which does the path munging as necessary. So, something like:

package/
    __init__.py
    _setup.py
    mod1/
        __init__.py
        _setup.py
        somemodule.py

If you import _setup from somemodule.py, that setup file can ensure that the top level package is in sys.path before the rest of the code in somemodule.py is evaluated.

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签