Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 400063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:54:32+00:00 2026-05-12T16:54:32+00:00

What’s the shortest way to import a module from a distant, relative directory? We’ve

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:54:32+00:00Added an answer on May 12, 2026 at 4:54 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.