Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatically patching PYTHONPATH (when using Fabric tasks)
    primarykey
    data
    text
    <p>I wrap function calls within my initialization module with a decorator that patches PYTHONPATH to add current directory to it so I can use relative imports within my modules without worrying about adding the package to PYTHONPATH explicitly (edited based on @abarnert's comments)</p> <pre><code>def patch_python_path(f): @wraps(f) def wrap(*args, **kwargs): ROOT = os.pathsep.join([os.path.abspath(os.path.dirname(__file__))]) if not os.environ.has_key("PYTHONPATH"): os.environ["PYTHONPATH"] = "" if not (ROOT in os.environ["PYTHONPATH"].split(":")): os.environ["PYTHONPATH"] = "%s:%s" % (os.environ["PYTHONPATH"], ROOT) if not ROOT in sys.path: sys.path.append(ROOT) return f(*args, **kwargs) return wrap </code></pre> <p>Here's how I use it:</p> <pre><code>@patch_python_path def initialize(): #at this point any code being run has access to local modules through relative imports pass </code></pre> <p>Are there any major issues with this approach that I am not aware of?</p> <hr> <p>My objectives with this are the following:</p> <ul> <li>i want to have a self contained bootstrap that a user can immediately use without any additional env manipulations</li> <li>i also assume that a user can change the name of the bootstrap package so it's imperative that all the imports remain relative </li> </ul> <p>[EDIT] I'm actually realizing that the problem I am facing has more to do with the way Fabric runs tasks as opposed to pure Python module importing. If I attempt to run a task from a python shell (as opposed to fab task1) all the imports are resolved correctly without any patching required. Running a task through fab causes import errors </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload