Importing Python modules from a remote directory

What's the shortest way to import a module from a remote, relative directory?

We used this code, which is not too bad, except that the current working directory must be the same as the directory with this code or relative path break, which can be error prone and confusing for users.

import sys
sys.path.append('../../../Path/To/Shared/Code')

      

This code (I think) fixes this problem, but is much more suitable for input.

import os,sys
sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), '../../../Path/To/Shared/Code')))

      

Is there a shorter way to add an absolute path? Brevity matters because this should be printed / appeared in many of our files. (We can't rule it out, because then it would be in the shared code and we couldn't get to it. Chicken and egg, bootstrapping, etc.)

Plus it worries me that we are blindly adding sys.path, but that will be even more code. I'm sure something in the standard library could help with this.

This usually shows up in script files that are run from the command line. We are running Python 2.6.2.

Edit: The reason we use relative paths is because we usually have multiple independent copies of the codebase on our computers. It is important that each copy of the codebase uses its own copy of the shared code. Therefore, any solution that only supports one codebase (for example, "Put it in site-packages") will not work for us.

Any suggestions? Thank!

+2


source to share


4 answers


In a comment, you explained why you don't want to install "one directory of sites", but how about inserting site-packages

one tiny module, say jebootstrap.py

:

import os, sys

def relative_dir(apath):
  return os.path.realpath(
      os.path.join(os.path.dirname(apath),
      '../../../Path/To/Shared/Code'))

def addpack(apath):
  relative = relative_dir(apath)
  if relative not in sys.path:
    sys.path.append(relative)

      

Now everywhere in your code you can simply



import jebootstrap
jebootsrap.addpack(__file__)

      

and the rest of your overall code base can remain independent for each installation.

+2


source


Since you don't want to install it in package sites, you must use buildout or virtualenv to create isolated development environments. This solves the problem and means you don't have to mess with sys.path anymore (in fact, since Buildout does exactly that for you).



+4


source


For some reason, you wouldn't want to create your own shared code in site packages? Then you can just import import shared.code.module ...

+1


source


You have several ways to handle imports, all of which are described in the Python Language Guide.

See http://docs.python.org/library/site.html and http://docs.python.org/reference/simple_stmts.html#the-import-statement

  • Put it in site packages and install multiple Python installations. You choose to install using a normal environment variable PATH

    .

  • Place the directory in your environment variable PYTHONPATH

    . This is a per-person setting, so you can manage multiple versions of your codebase this way.

  • Place the directory in .pth

    files in your site packages. You choose to install using a normal environment variable PATH

    .

-1


source







All Articles