Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing Python modules from a distant directory

Tags:

python

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

We've been using this code which isn't too bad except your current working directory has to be the same as the directory as this code's or the relative path breaks, which can be error prone and confusing to users.

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

This code (I think) fixes that problem but is a lot more to type.

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 append the absolute path? The brevity matters because this is going to have to be typed/appear in a lot of our files. (We can't factor it out because then it would be in the shared code and we couldn't get to it. Chicken & egg, bootstrapping, etc.)

Plus it bothers me that we keep blindly appending to sys.path but that would be even more code. I sure wish something in the standard library could help with this.

This will typically appear in script files which are run from the command line. We're running Python 2.6.2.

Edit: The reason we're using relative paths is that we typically have multiple, independent copies of the codebase on our computers. It's important that each copy of the codebase use its own copy of the shared code. So any solution which supports only a single code base (e.g., 'Put it in site-packages.') won't work for us.

Any suggestions? Thank you!

like image 892
Jon-Eric Avatar asked Jun 28 '26 01:06

Jon-Eric


2 Answers

Since you don't want to install it in site-packages, you should use buildout or virtualenv to create isolated development environments. That solves the problem, and means you don't have to fiddle with sys.path anymore (in fact, because Buildout does exactly that for you).

like image 194
Lennart Regebro Avatar answered Jun 29 '26 14:06

Lennart Regebro


You've explained in a comment why you don't want to install "a single site-packages directory", but what about putting in site-packages a single, 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 just have

import jebootstrap
jebootsrap.addpack(__file__)

and all the rest of your shared codebase can remain independent per-installation.

like image 22
Alex Martelli Avatar answered Jun 29 '26 16:06

Alex Martelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!