Permanently add a directory to PYTHONPATH

Permanently add a directory to PYTHONPATH

Asked on November 10, 2018 in Python.
Add Comment


  • 3 Answer(s)

         Here add feature to your new directory to the surroundings variable PYTHONPATH, separated by a colon from previous contents therefrom. In any variety of UNIX operating system, you’ll be able to do this in an exceedingly startup script applicable to no matter shell you are victimisation (.profile or no matter, counting on your favorite shell) with a command that, again, depends on the shell in question; in Windows, you’ll be able to bed through the system GUI for the aim.

         superuser.com could also be an improved place to raise any, i.e. for a lot of details if you would like specifics regarding the way to enrich associate degree surroundings variable in your chosen platform and shell, since it is not extremely a programming question intrinsically.

    Answered on November 10, 2018.
    Add Comment

    If you are victimisation bash (on a waterproof or GNU/Linux distro), add this to your ~/.bashrc

    export PYTHONPATH="$:/my/other/path"
    
    Answered on November 10, 2018.
    Add Comment

    Rather of manipulating PYTHONPATH you’ll be able to conjointly produce a path configuration file. initial establish within which directory Python searches for this information:

    python -m website --user-site
    

    For some reason this does not appear to figure in Python 2.7. There you’ll be able to use:

    python -c 'import website; site._script()' --user-site
    

    Then produce a .pth enter that directory containing the trail you would like to feature (create the directory if it does not exist).
    For example:

    # find directory
    SITEDIR=$(python -m website --user-site)
     
    # create if it does not exist
    mkdir -p "$SITEDIR"
     
    # create new .pth file with our path
    echo "$HOME/foo/bar" > "$SITEDIR/somelib.pth
    
    Answered on November 10, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.