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 6535681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:24:06+00:00 2026-05-25T10:24:06+00:00

I have two directories in my project: project/ src/ scripts/ src contains my polished

  • 0

I have two directories in my project:

project/
  src/
  scripts/

“src” contains my polished code, and “scripts” contains one-off Python scripts.

I would like all the scripts to have “../src” added to their sys.path, so that they can access the modules under the “src” tree. One way to do this is to write a scripts/__init__.py file, with the contents:

scripts/__init__.py:
  import sys
  sys.path.append("../src")

This works, but has the unwanted side-effect of putting all of my scripts in a package called “scripts”. Is there some other way to get all my scripts to automatically call the above initialization code?

I could just edit the PYTHONPATH environment variable in my .bashrc, but I want my scripts to work out-of-the-box, without requiring the user to fiddle with PYTHONPATH. Also, I don’t like having to make account-wide changes just to accommodate this one project.

  • 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-25T10:24:07+00:00Added an answer on May 25, 2026 at 10:24 am

    Even if you have other plans for distribution, it might be worth putting together a basic setup.py in your src folder. That way, you can run setup.py develop to have distutils put a link to your code onto your default path (meaning any changes you make will be reflected in-place without having to “reinstall”, and all modules will “just work,” no matter where your scripts are). It’d be a one-time step, but that’s still one more step than zero, so it depends on whether that’s more trouble than updating .bashrc. If you use pip, the equivalent would be pip install -e /path/to/src.

    The more-robust solution–especially if you’re going to be mirroring/versioning these scripts on several developers’ machines–is to do your development work inside a controlled virtual environment. It turns out virtualenv even has built-in support for making your own bootstrap customizations. It seems like you’d just need an after_install() hook to either tweak sitecustomize, run pip install -e, or add a plain .pth file to site-packages. The custom bootstrap could live in your source control along with the other scripts, and would need to be run once for each developer’s setup. You’d also have the normal benefits of using virtualenv (explicit dependency versioning, isolation from system-wide configuration, and standardization between disparate machines, to name a few).

    If you really don’t want to have any setup steps whatsoever and are willing to only run these scripts from inside the ‘project’ directory, then you could plop in an __init__.py as such:

    project/
        src/
            some_module.py
        scripts/
            __init__.py # special "magic"
            some_script.py
    

    And these are what your files could look like:

    # file: project/src/some_module.py
    print("importing %r" % __name__)
    
    def some_function():
        print("called some_function() inside %s" % __name__)
    --------------------------------------------------------
    # file: project/scripts/some_script.py
    import some_module
    
    if __name__ == '__main__':
        some_module.some_function()
    --------------------------------------------------------
    # file: project/scripts/__init__.py
    import sys
    from os.path import dirname, abspath, join
    
    print("doing magic!")
    sys.path.insert(0, join(dirname(dirname(abspath(__file__))), 'src'))
    

    Then you’d have to run your scripts like so:

    [~/project] $ python -m scripts.some_script
    doing magic!
    importing 'some_module'
    called some_function() inside some_module
    

    Beware! The scripts can only be called like this from inside project/:

    [~/otherdir] $ python -m scripts.some_script
    ImportError: no module named scripts
    

    To enable that, you’re back to editing .bashrc, or using one of the options above. The last option should really be a last resort; as @Simon said, you’re really fighting the language at that point.

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

Sidebar

Related Questions

I have a directory called project . It contains two sub-directories called client and
I have two directories containing source files to a project I've inherited with little
Suppose I have two branches of a project IMClient-MacOS and IMClient-Windows, and their code
I have a new MVC3 project with one Controller called PublicController.cs which contains 4
I have a subdirectory structure in my project with two cmake files. One of
I have a Maven project (in Eclipse using the m2eclipse plugin) that contains code
I have two virtual directories, one hosts an application the other hosts a web
I have two directories in the same parent directory. Call the parent directory base
Background: We have two directories of Crystal Reports for both the web side of
Is it possible to have two virtual directories under the same website in IIS

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.